Built-in project · Arduino Uno R3

Wireless robot arm with nRF24L01 on an Arduino Uno

Two Unos and two nRF24L01 radios: joysticks on one board steer a four-servo arm on the other, with a failsafe that holds the arm when the link drops. The whole build, an Arduino Uno and 5 more parts, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.

Teaching? Assign it to your class in one click.

Advanced About 60 minutes to build and run. Runs in your browser. Free, and no account needed.

The parts retail for about $85. On Mokxi they're free.

The circuit runs here once the page has loaded.

What you'll learn

  • Sending data between two boards with an nRF24L01 radio
  • Packing several readings into one struct packet
  • Using joysticks as rate controls with a dead band
  • Failsafe: stopping safely when the signal drops
  • Why both radios must share settings and a pipe address
  • Difficulty: Advanced
  • Time: About 60 minutes
  • Board: Arduino Uno R3
  • Parts: 9

How it works

Two Unos talk over nRF24L01 radios. The controller reads four joystick axes on A0 to A3 about fifty times a second. It sends them as one 8-byte packet to the address ARM01.

The arm board listens for packets. Each stick works as a rate control: push it past the dead band and that joint keeps moving, faster the further you push. Left stick drives the base and shoulder, right stick the elbow and claw.

If no packet arrives for half a second, the arm decides the link is lost. It stops every joint where it is and reports signal lost. When packets return, it says link up and carries on. The controller counts packets that were not answered.

Parts list

9 parts, plus the jumper wires. They retail for about $85; on Mokxi they're free.

Wiring table

Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.

Part and pinConnects to
2.4 GHz radio, nRF24L01+ (1) pin GNDArduino Uno R3 (1) pin GND
2.4 GHz radio, nRF24L01+ (1) pin SCKArduino Uno R3 (1) pin 13
2.4 GHz radio, nRF24L01+ (1) pin MISOArduino Uno R3 (1) pin 12
2.4 GHz radio, nRF24L01+ (1) pin MOSIArduino Uno R3 (1) pin 11
2.4 GHz radio, nRF24L01+ (1) pin CSNArduino Uno R3 (1) pin 8
2.4 GHz radio, nRF24L01+ (1) pin CEArduino Uno R3 (1) pin 7
2.4 GHz radio, nRF24L01+ (1) pin VCCArduino Uno R3 (1) pin 3V3
Thumb stick (1) pin VCCArduino Uno R3 (1) pin 5V
Thumb stick (1) pin VRXArduino Uno R3 (1) pin A0
Thumb stick (1) pin VRYArduino Uno R3 (1) pin A1
Thumb stick (2) pin VRXArduino Uno R3 (1) pin A2
Thumb stick (2) pin VRYArduino Uno R3 (1) pin A3
2.4 GHz radio, nRF24L01+ (2) pin GNDArduino Uno R3 (2) pin GND
2.4 GHz radio, nRF24L01+ (2) pin SCKArduino Uno R3 (2) pin 13
2.4 GHz radio, nRF24L01+ (2) pin MISOArduino Uno R3 (2) pin 12
2.4 GHz radio, nRF24L01+ (2) pin MOSIArduino Uno R3 (2) pin 11
Robot arm, 4 servos pin GRIPArduino Uno R3 (2) pin 9
2.4 GHz radio, nRF24L01+ (2) pin CSNArduino Uno R3 (2) pin 8
2.4 GHz radio, nRF24L01+ (2) pin CEArduino Uno R3 (2) pin 7
Robot arm, 4 servos pin ELBOWArduino Uno R3 (2) pin 6
Robot arm, 4 servos pin SHOULDERArduino Uno R3 (2) pin 5
Robot arm, 4 servos pin BASEArduino Uno R3 (2) pin 3
2.4 GHz radio, nRF24L01+ (2) pin VCCArduino Uno R3 (2) pin 3V3
Arduino Uno R3 (2) pin GNDGround
Robot arm, 4 servos pin VCC5 V
Robot arm, 4 servos pin GNDGround
Thumb stick (1) pin GNDGround
Thumb stick (2) pin GNDGround
Thumb stick (2) pin VCC5 V

The code

The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.

sketch.ino · Arduino Uno R3
// Wireless robot arm, the controller: two joysticks on this Uno, sent over
// an nRF24L01 radio to the Uno that drives the arm.
//
//   left stick   VRX A0 (base), VRY A1 (shoulder)
//   right stick  VRX A2 (elbow), VRY A3 (claw)
//   radio        CE on 7, CSN on 8, SCK 13, MOSI 11, MISO 12, VCC on 3.3V
//
// About fifty times a second this board reads the four stick positions
// and sends them as one 8-byte packet. The radio on the arm answers every
// packet in hardware, so radio.write() says whether the packet got there. The
// controller counts the packets that were not answered, which is how a
// real transmitter knows it is losing the link.
//
// Both radios have to agree on the channel, the data rate, the address and
// the payload size. Change one on this side only and every packet is lost.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>

RF24 radio(7, 8);  // CE on 7, CSN on 8
const byte address[6] = "ARM01";

// What goes over the air: the four sticks, 0 to 1023 each. A struct is
// sent byte for byte, so both boards must declare it the same way.
struct Packet {
  int16_t sticks[4];
};

const int STICKS[4] = {A0, A1, A2, A3};

unsigned long sent = 0, lost = 0;
unsigned long lastReport = 0;

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setPayloadSize(sizeof(Packet));
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address);
  radio.stopListening();
  Serial.println("arm controller: move the sticks");
}

void loop() {
  Packet packet;
  for (int i = 0; i < 4; i++) packet.sticks[i] = analogRead(STICKS[i]);

  bool heard = radio.write(&packet, sizeof(packet));
  sent++;
  if (!heard) lost++;

  // Once a second, how the link is doing.
  if (millis() - lastReport >= 1000) {
    lastReport = millis();
    Serial.print("sent ");
    Serial.print(sent);
    Serial.print("  lost ");
    Serial.println(lost);
  }
  delay(20);
}
sketch.ino · Arduino Uno R3
// Wireless robot arm, the arm: this Uno drives a four-servo arm from the
// stick positions the other Uno sends over an nRF24L01 radio.
//
//   arm      BASE pin 3, SHOULDER pin 5, ELBOW pin 6, GRIP pin 9
//            VCC and GND from a separate 5 V supply, GND joined to the Uno's
//   radio    CE on 7, CSN on 8, SCK 13, MOSI 11, MISO 12, VCC on 3.3V
//
// The sticks are rate controls, as on the wired arm: push a stick and its
// joint keeps moving while you hold it, faster the further you push.
//
// FAILSAFE. A radio link can drop at any moment: out of range, a flat
// battery, the controller switched off. The arm then gets no more packets,
// and the worst thing it could do is keep moving on the last ones it heard.
// So it notes when each packet arrives, and after half a second of silence
// it stops every joint where it is and says so, until the link comes back.
// Every RC receiver has the same idea, called failsafe.

#include <SPI.h>
#include <nRF24L01.h>
#include <RF24.h>
#include <Servo.h>

RF24 radio(7, 8);  // CE on 7, CSN on 8
const byte address[6] = "ARM01";

// Must match the controller's struct exactly.
struct Packet {
  int16_t sticks[4];
};

Servo base, shoulder, elbow, claw;
Servo *joints[4] = {&base, &shoulder, &elbow, &claw};
const int SERVO_PINS[4] = {3, 5, 6, 9};

int angle[4] = {90, 90, 90, 70};
const int LOW_END[4] = {0, 40, 40, 20};
const int HIGH_END[4] = {180, 160, 160, 120};
const int DEAD_BAND = 60;  // of 1023, either side of the middle

const unsigned long FAILSAFE_MS = 500;
Packet last = {{512, 512, 512, 512}};  // sticks centered: nothing moves
unsigned long lastHeard = 0;
bool linked = false;

void setup() {
  Serial.begin(115200);
  for (int i = 0; i < 4; i++) {
    joints[i]->attach(SERVO_PINS[i]);
    joints[i]->write(angle[i]);
  }
  radio.begin();
  radio.setPayloadSize(sizeof(Packet));
  radio.setPALevel(RF24_PA_LOW);
  radio.openReadingPipe(1, address);
  radio.startListening();
  Serial.println("arm: waiting for the controller");
}

void loop() {
  while (radio.available()) {
    radio.read(&last, sizeof(last));
    lastHeard = millis();
    if (!linked) {
      linked = true;
      Serial.println("link up");
    }
  }
  if (linked && millis() - lastHeard > FAILSAFE_MS) {
    linked = false;
    Serial.println("signal lost: failsafe, every joint holds");
  }

  if (linked) {
    for (int i = 0; i < 4; i++) {
      int offset = last.sticks[i] - 512;
      if (offset > DEAD_BAND || offset < -DEAD_BAND) {
        angle[i] = constrain(angle[i] + offset / 170, LOW_END[i], HIGH_END[i]);
      }
    }
  }
  for (int i = 0; i < 4; i++) joints[i]->write(angle[i]);
  delay(20);
}

Code walkthrough

The key functions of the sketch, and what each one does.

setup sketch.ino

On the controller, this starts the radio, sets the payload size to the Packet struct, opens the writing pipe to address, and stops listening. On the arm, it attaches the four servos and instead opens a reading pipe and starts listening. Both sides use the same payload size and power level.

void setup() {
  Serial.begin(115200);
  radio.begin();
  radio.setPayloadSize(sizeof(Packet));
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address);
  radio.stopListening();
  Serial.println("arm controller: move the sticks");
}

loop sketch.ino

On the controller, each pass reads the four sticks into a packet and sends it with radio.write. The result says whether the arm answered, and the controller counts the misses as lost. On the arm, loop reads waiting packets, runs the failsafe check against FAILSAFE_MS, and nudges each angle by the stick offset divided by 170, but only past DEAD_BAND.

void loop() {
  Packet packet;
  for (int i = 0; i < 4; i++) packet.sticks[i] = analogRead(STICKS[i]);

  bool heard = radio.write(&packet, sizeof(packet));
  sent++;
  if (!heard) lost++;

  // Once a second, how the link is doing.
  if (millis() - lastReport >= 1000) {
    lastReport = millis();
    Serial.print("sent ");
    Serial.print(sent);
    Serial.print("  lost ");
    Serial.println(lost);
  }
  delay(20);
}

Troubleshooting

The usual mistakes with this circuit, and the fix for each.

  • The controller reports every packet lost This project
    Both radios must use the same address, payload size, channel and rate. Check that the Packet struct and address match on both boards.
  • A radio does not start or acts erratic This project
    The nRF24L01 runs on 3.3 V, not 5 V. Wire VCC to the 3V3 pin.
  • The arm drifts when the sticks are centered This project
    Sticks within DEAD_BAND of the middle are ignored. If your stick rests further off center, increase it.
  • The module resets, drops packets or never works on 5 V. 2.4 GHz radio
    Power it from 3.3 V, never 5 V. Add a 10 uF capacitor across VCC and GND close to the module, since the Uno 3.3 V pin is weak.
  • Two radios do not hear each other. 2.4 GHz radio
    Both need the same channel, address, data rate and payload size. One writes, the other listens. Check CE and CSN pins and the SPI wires (SCK, MOSI, MISO).
  • The arm jerks, resets the board or moves weakly when several joints move at once. Robot arm
    Four SG90 servos can pull an amp or more together. Power them from a separate 5 V supply with a 470 uF or larger capacitor. Join its ground to the board's ground.
  • A joint hits its end stop and buzzes, or the claw crushes. Robot arm
    Limit each servo to a safe angle range in code and move gradually in small steps. Find the open and closed angle of the claw by testing, and never command beyond them.
  • The axis reads about half scale at rest instead of exactly zero, and the values drift a little, so the cursor or motor creeps. Thumb stick
    Center is about half of the ADC range (about 512 on a 10 bit Arduino). Read it at startup and subtract that, then add a dead zone of a few counts.

Change it and keep it

Open it in the editor, change the circuit or the code, and keep your version in a free account.