Built-in project · Arduino Uno R3

Send a heartbeat on an Arduino Uno

Two Unos, each with an nRF24L01 radio, a button and a red LED: press the button on one and the heart on the other beats lub-dub, and the sender glows when it arrives. The whole build, an Arduino Uno and 8 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 45 minutes to build and run. Runs in your browser. Free, and no account needed.

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

Send a heartbeat · Arduino Uno R3live0.000 s 0.00x
Press the button on the left board, then the one on the right
The circuit itself, running here on the simulator. Press what can be pressed; click anything else to open it in the editor.

What you'll learn

  • Wireless messages between two microcontrollers with an nRF24L01
  • The SPI bus: SCK, MISO, MOSI plus CE and CSN pins
  • Why one shared address lets either board send or receive
  • Fading an LED with PWM using analogWrite
  • Edge detection: acting once when a button goes down
  • Difficulty: Advanced
  • Time: About 45 minutes
  • Board: Arduino Uno R3
  • Parts: 11

How it works

You build two identical boards. Each has an nRF24L01 radio, a button on pin 2 and a red LED on pin 5. Both run the same sketch and listen all the time.

Press the button on one board. It stops listening for a moment and sends the word beat over the radio. The other radio answers in hardware, and that answer makes radio.write return true. The sender then glows its LED softly to say it arrived.

The board that hears the message prints a note and plays a lub-dub on its LED: a strong fade, a short pause, then a lighter one. If no board answers, the sender prints that nobody answered.

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
Resistor, 220 Ω (1) pin 1Arduino Uno R3 (1) pin 5
Pushbutton (1) pin 1aArduino Uno R3 (1) pin 2
2.4 GHz radio, nRF24L01+ (1) pin VCCArduino Uno R3 (1) pin 3V3
Arduino Uno R3 (2) pin GNDArduino Uno R3 (1) pin GND
Pushbutton (1) pin 2aArduino Uno R3 (1) pin GND
LED, red (1) pin CArduino Uno R3 (1) pin GND
Pushbutton (2) pin 2aArduino Uno R3 (1) pin GND
LED, red (2) pin CArduino Uno R3 (1) 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
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
Resistor, 220 Ω (2) pin 1Arduino Uno R3 (2) pin 5
Pushbutton (2) pin 1aArduino Uno R3 (2) pin 2
2.4 GHz radio, nRF24L01+ (2) pin VCCArduino Uno R3 (2) pin 3V3
2.4 GHz radio, nRF24L01+ (2) pin GNDArduino Uno R3 (2) pin GND
LED, red (1) pin AResistor, 220 Ω (1) pin 2
LED, red (2) pin AResistor, 220 Ω (2) pin 2

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
// Send a heartbeat: a pair of these, one for you and one for someone you
// love. Press the button on yours and the heart on theirs beats twice, lub-dub,
// and yours glows once to say it got there.
//
//   radio     nRF24L01: CE -> pin 7, CSN -> pin 8, SCK -> 13, MOSI -> 11,
//             MISO -> 12, VCC -> 3V3 (never 5 V), GND -> GND
//   button    pin 2 -> button -> GND        (INPUT_PULLUP, pressed reads LOW)
//   heart     pin 5 -> 220 ohms -> red LED -> GND
//
// Both boards run this same sketch. Each one listens all the time, and only
// stops listening for the moment it sends. They share one address, so either
// can start, and the radio that hears the packet answers it in hardware:
// radio.write() returns true only when that answer comes back. That one bool
// is how the sender knows the heartbeat arrived.
//
// The radios reach across a house, not across a city. For two homes far
// apart the same idea runs over Wi-Fi on an ESP32, but the heartbeat and the
// lub-dub are the same.
//
// Things to try. Press the button on the left board, then on the right one.
// Delete one board and press again: the sender says nobody answered.

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

const int BUTTON_PIN = 2;
const int HEART_PIN = 5;

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

bool wasPressed = false;
int sent = 0;

void fade(int peak, int ms) {
  for (int v = 0; v <= peak; v += 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  for (int v = peak; v >= 0; v -= 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  digitalWrite(HEART_PIN, LOW);
}

void beat() {
  fade(255, 250);   // lub
  delay(80);
  fade(150, 200);   // dub
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(HEART_PIN, OUTPUT);
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address);
  radio.openReadingPipe(1, address);
  radio.startListening();
  Serial.println("Heartbeat ready: press the button to send one");
}

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;
  if (pressed && !wasPressed) {
    sent++;
    char message[8] = "beat";
    radio.stopListening();
    bool heard = radio.write(&message, sizeof(message));
    radio.startListening();
    Serial.print("Heartbeat ");
    Serial.print(sent);
    if (heard) {
      Serial.println(" sent and received");
      fade(60, 300);   // a soft glow: it got there
    } else {
      Serial.println(": nobody answered");
    }
    delay(50);
  }
  wasPressed = pressed;

  if (radio.available()) {
    char message[8] = "";
    radio.read(&message, sizeof(message));
    Serial.println("A heartbeat for you");
    beat();
  }
}
sketch.ino · Arduino Uno R3
// Send a heartbeat: a pair of these, one for you and one for someone you
// love. Press the button on yours and the heart on theirs beats twice, lub-dub,
// and yours glows once to say it got there.
//
//   radio     nRF24L01: CE -> pin 7, CSN -> pin 8, SCK -> 13, MOSI -> 11,
//             MISO -> 12, VCC -> 3V3 (never 5 V), GND -> GND
//   button    pin 2 -> button -> GND        (INPUT_PULLUP, pressed reads LOW)
//   heart     pin 5 -> 220 ohms -> red LED -> GND
//
// Both boards run this same sketch. Each one listens all the time, and only
// stops listening for the moment it sends. They share one address, so either
// can start, and the radio that hears the packet answers it in hardware:
// radio.write() returns true only when that answer comes back. That one bool
// is how the sender knows the heartbeat arrived.
//
// The radios reach across a house, not across a city. For two homes far
// apart the same idea runs over Wi-Fi on an ESP32, but the heartbeat and the
// lub-dub are the same.
//
// Things to try. Press the button on the left board, then on the right one.
// Delete one board and press again: the sender says nobody answered.

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

const int BUTTON_PIN = 2;
const int HEART_PIN = 5;

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

bool wasPressed = false;
int sent = 0;

void fade(int peak, int ms) {
  for (int v = 0; v <= peak; v += 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  for (int v = peak; v >= 0; v -= 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  digitalWrite(HEART_PIN, LOW);
}

void beat() {
  fade(255, 250);   // lub
  delay(80);
  fade(150, 200);   // dub
}

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(HEART_PIN, OUTPUT);
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address);
  radio.openReadingPipe(1, address);
  radio.startListening();
  Serial.println("Heartbeat ready: press the button to send one");
}

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;
  if (pressed && !wasPressed) {
    sent++;
    char message[8] = "beat";
    radio.stopListening();
    bool heard = radio.write(&message, sizeof(message));
    radio.startListening();
    Serial.print("Heartbeat ");
    Serial.print(sent);
    if (heard) {
      Serial.println(" sent and received");
      fade(60, 300);   // a soft glow: it got there
    } else {
      Serial.println(": nobody answered");
    }
    delay(50);
  }
  wasPressed = pressed;

  if (radio.available()) {
    char message[8] = "";
    radio.read(&message, sizeof(message));
    Serial.println("A heartbeat for you");
    beat();
  }
}

Code walkthrough

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

fade sketch.ino

Ramps the LED up to a peak brightness in steps of 15, then back down to zero. The ms value sets how long the whole fade takes, split over the steps with delay. At the end it forces the pin LOW.

void fade(int peak, int ms) {
  for (int v = 0; v <= peak; v += 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  for (int v = peak; v >= 0; v -= 15) {
    analogWrite(HEART_PIN, v);
    delay(ms / 34);
  }
  digitalWrite(HEART_PIN, LOW);
}

beat sketch.ino

Makes the lub-dub. First a full-strength fade (255) over 250 ms, then an 80 ms pause, then a softer fade (150) over 200 ms.

void beat() {
  fade(255, 250);   // lub
  delay(80);
  fade(150, 200);   // dub
}

setup sketch.ino

Sets the button as INPUT_PULLUP and the LED pin as an output. It starts the radio at low power, uses the address HEART for both writing and reading, and begins listening.

void setup() {
  Serial.begin(9600);
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(HEART_PIN, OUTPUT);
  radio.begin();
  radio.setPALevel(RF24_PA_LOW);
  radio.openWritingPipe(address);
  radio.openReadingPipe(1, address);
  radio.startListening();
  Serial.println("Heartbeat ready: press the button to send one");
}

loop sketch.ino

Looks for a new button press. On a press it stops listening, writes the message, starts listening again, and glows if the write returned true. Then it checks radio.available and plays beat for any incoming heartbeat.

void loop() {
  bool pressed = digitalRead(BUTTON_PIN) == LOW;
  if (pressed && !wasPressed) {
    sent++;
    char message[8] = "beat";
    radio.stopListening();
    bool heard = radio.write(&message, sizeof(message));
    radio.startListening();
    Serial.print("Heartbeat ");
    Serial.print(sent);
    if (heard) {
      Serial.println(" sent and received");
      fade(60, 300);   // a soft glow: it got there
    } else {
      Serial.println(": nobody answered");
    }
    delay(50);
  }
  wasPressed = pressed;

  if (radio.available()) {
    char message[8] = "";
    radio.read(&message, sizeof(message));
    Serial.println("A heartbeat for you");
    beat();
  }
}

Troubleshooting

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

  • The radio does not work or gets very hot This project
    The nRF24L01 runs on 3.3 V only. Connect its VCC to the 3V3 pin, never to 5 V.
  • The sender always says nobody answered This project
    Check that both boards are wired the same, especially CE on pin 7 and CSN on pin 8. Both must use the same address and both must be running.
  • 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 input reads random HIGH and LOW when the button is not pressed. Pushbutton
    A floating pin picks up noise. Add a 10 k pull-down or pull-up resistor, or use INPUT_PULLUP and wire the button between the pin and GND. Then pressed reads LOW.
  • The button seems always pressed, or never changes. Pushbutton
    A 4-leg tactile switch has legs joined in pairs. Place it across the breadboard center gap so the pairs sit on different rows. Wired along one side, the legs are already connected.
  • The LED is dim, burns out, or the pin reads wrong because the resistor value is off. Resistor
    Read the color bands again. A 220 ohm to 330 ohm resistor suits a typical LED on 5 V. 10k is a common pull-up or pull-down value. Check with a multimeter if unsure.
  • A pull-up or pull-down resistor is missing, so the input floats and reads random values. Resistor
    Tie the input to 5 V or ground through about 10k. Or use INPUT_PULLUP and wire the switch to ground.

Change it and keep it

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