Try Embedded

Wireless button (two nRF24L01s)

A button on one Arduino lights an LED on another over 2.4 GHz radio, and the sender knows each packet arrived from its acknowledgement.

Communication · Intermediate

In a real product: Wireless doorbell (Smart home): The button and the chime talk over 2.4 GHz radio.

Two Unos, each with an nRF24L01 radio and no wires between them. The first sends its button's state whenever it changes and lights a green LED when the packet is acknowledged; the second lights its LED to match. Each board runs its own sketch: sketch.ino on the first, uno2.ino on the second.

What you'll learn

  • Addresses and pipes: the sender writes to an address, the receiver listens on a pipe with that address
  • Auto-acknowledgement: radio.write() returns whether the other radio confirmed the packet
  • Why both sides must agree on the channel, data rate and payload size

How it works

Both radios start with radio.begin() on the default channel (76), 1 Mbit/s and 32-byte payloads. The sender opens a writing pipe to "00001" and calls stopListening(); the receiver opens reading pipe 1 with the same address and calls startListening(). When the button changes, the sender writes one byte; the receiver's radio checks the address and the CRC, stores it and answers with an ACK, and the sender's write() returns true. With no answer after 15 retries, write() returns false and the red LED lights.

Try this

  • Press the button on the first board: the second board's LED follows.
  • Set the second radio's range to out and press again: the first board's red LED shows the failure.
  • Change the channel on one side only (radio.setChannel(90)): nothing gets through.

sketch.ino

// Sender: the button on pin 2 is sent (one byte, 1 pressed) over an nRF24L01 (CE 9, CSN 10) whenever it changes. The
// green LED (pin 5) shows the last packet was acknowledged, the red one (pin 6) that it was not.
#include <SPI.h>
#include <RF24.h>

RF24 radio(9, 10);
const byte address[6] = "00001";
bool last = false;

void setup() {
  Serial.begin(9600);
  pinMode(2, INPUT_PULLUP);
  pinMode(5, OUTPUT);
  pinMode(6, OUTPUT);
  if (!radio.begin()) Serial.println("No radio: check the wiring and its 3.3 V supply");
  radio.openWritingPipe(address);
  radio.stopListening();
}

void loop() {
  bool pressed = digitalRead(2) == LOW;
  if (pressed == last) return;
  last = pressed;
  byte state = pressed;
  bool acked = radio.write(&state, 1);
  digitalWrite(5, acked);
  digitalWrite(6, !acked);
  Serial.println(acked ? "sent, acknowledged" : "sent, no acknowledgement");
  delay(20);
}

uno2.ino

// Receiver: an nRF24L01 (CE 9, CSN 10) listens on address "00001"; the LED on pin 5 shows the button on the other
// board.
#include <SPI.h>
#include <RF24.h>

RF24 radio(9, 10);
const byte address[6] = "00001";

void setup() {
  Serial.begin(9600);
  pinMode(5, OUTPUT);
  if (!radio.begin()) Serial.println("No radio: check the wiring and its 3.3 V supply");
  radio.openReadingPipe(1, address);
  radio.startListening();
  Serial.println("Listening");
}

void loop() {
  if (!radio.available()) return;
  byte state;
  radio.read(&state, 1);
  digitalWrite(5, state);
  Serial.println(state ? "button pressed" : "button released");
}

Lessons behind it