Built-in project · ESP32 DevKit V1

ESP-NOW button and LED on an ESP32 DevKit

Two ESP32s and no wire between them: the button on one sends on or off over ESP-NOW, the other lights its LED and prints each message, and the sender hears the ACK. The whole build, an ESP32 DevKit and 3 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.

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

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

The circuit runs here once the page has loaded.

What you'll learn

  • Sending data between boards with ESP-NOW
  • What a MAC address is and why the sender needs one
  • Packing data into a struct shared by both boards
  • Callbacks that run when a send finishes or a message arrives
  • Detecting a button press, not just a held button
  • Difficulty: Intermediate
  • Time: About 20 minutes
  • Board: ESP32 DevKit V1
  • Parts: 5

How it works

Two ESP32 boards talk straight to each other with ESP-NOW, with no router and no wire between them. The sender has a button on pin 4. The receiver has an LED on pin 4 behind a 220 ohm resistor.

Each press on the sender flips an on/off flag and sends a small message to the receiver's MAC address. The message holds the flag and a press count. The receiver copies it back out and sets its LED to match.

The sender's radio gets an acknowledgment back from the receiver. The sender prints Delivery success or Delivery fail so you can see if the message arrived.

Parts list

5 parts, plus the jumper wires. They retail for about $18.25; 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
Pushbutton pin 1aESP32 DevKit V1 (1) pin D4
Resistor, 220 Ω pin 1ESP32 DevKit V1 (2) pin D4
Pushbutton pin 2aGround
LED, green pin AResistor, 220 Ω pin 2
LED, green pin CGround

The code

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

sketch.ino · ESP32 DevKit V1
// ESP-NOW sender: the button on GPIO 4 switches the LED on the other board.
//
// No wire and no router between the two ESP32s: ESP-NOW sends a frame straight
// to the receiver's MAC, and its radio answers with an ACK, which is what the
// send callback reports. In Mokxi every ESP board on the canvas shares the air.
//
// The receiver's MAC is written below. In this project the receiver's wifiMac
// setting is that address; on real boards, copy the line the receiver prints
// when it starts. Change one byte here and every send fails: nobody has that
// MAC, so no ACK comes back.

#include <WiFi.h>
#include <esp_now.h>

uint8_t receiverMac[] = {0x02, 0x4D, 0x4F, 0x00, 0x00, 0x02};

// The same struct on both boards: the receiver copies the bytes back into it.
typedef struct {
    bool on;
    uint32_t presses;
} Message;

const int kButton = 4; // to GND, with the pad's own pull-up
Message msg = {false, 0};
bool wasPressed = false;

// Arduino-ESP32 core 3.3's send callback. On cores 2.x to 3.2 the first
// argument was `const uint8_t *mac_addr`; Mokxi builds that one too.
void onSent(const wifi_tx_info_t *info, esp_now_send_status_t status)
{
    (void)info;
    Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery success" : "Delivery fail");
}

void setup()
{
    Serial.begin(115200);
    pinMode(kButton, INPUT_PULLUP);
    WiFi.mode(WIFI_STA);
    Serial.print("Sender MAC: ");
    Serial.println(WiFi.macAddress());

    if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
    }
    esp_now_register_send_cb(onSent);

    esp_now_peer_info_t peer = {};
    memcpy(peer.peer_addr, receiverMac, 6);
    peer.channel = 0; // the radio's own channel
    peer.encrypt = false;
    if (esp_now_add_peer(&peer) != ESP_OK) {
        Serial.println("Failed to add peer");
        return;
    }
    Serial.println("Press the button");
}

void loop()
{
    // Read every 20 ms: longer than the button bounces, so one press is one.
    bool pressed = digitalRead(kButton) == LOW;
    if (pressed && !wasPressed) {
        msg.on = !msg.on;
        msg.presses++;
        esp_err_t result = esp_now_send(receiverMac, (uint8_t *)&msg, sizeof(msg));
        Serial.print("Press ");
        Serial.print(msg.presses);
        Serial.print(": LED ");
        Serial.print(msg.on ? "on" : "off");
        Serial.println(result == ESP_OK ? ", sent" : ", not sent");
    }
    wasPressed = pressed;
    delay(20);
}
sketch.ino · ESP32 DevKit V1
// ESP-NOW receiver: lights the LED on GPIO 4 when the other board says so.
//
// It prints its own MAC first: that is the address the sender writes into its
// sketch. In this project the board's wifiMac setting fixes it at
// 02:4D:4F:00:00:02; empty, Mokxi gives each board a MAC of its own.
//
// The receive callback runs as soon as a frame arrives, as it does on a real
// ESP32, from the WiFi task rather than from loop().

#include <WiFi.h>
#include <esp_now.h>

typedef struct {
    bool on;
    uint32_t presses;
} Message;

const int kLed = 4; // through a resistor to GND

// Arduino-ESP32 core 3.x's receive callback; core 2.x's took the sender's MAC
// as `const uint8_t *mac` instead of `info`, and Mokxi builds that one too.
void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len)
{
    if (len != sizeof(Message)) return;
    Message msg;
    memcpy(&msg, data, sizeof(msg));
    digitalWrite(kLed, msg.on ? HIGH : LOW);
    char from[18];
    snprintf(from, sizeof(from), "%02X:%02X:%02X:%02X:%02X:%02X", info->src_addr[0], info->src_addr[1],
             info->src_addr[2], info->src_addr[3], info->src_addr[4], info->src_addr[5]);
    Serial.print("From ");
    Serial.print(from);
    Serial.print(", ");
    Serial.print(len);
    Serial.print(" bytes: press ");
    Serial.print(msg.presses);
    Serial.print(", LED ");
    Serial.println(msg.on ? "on" : "off");
}

void setup()
{
    Serial.begin(115200);
    pinMode(kLed, OUTPUT);
    WiFi.mode(WIFI_STA);
    Serial.print("Receiver MAC: ");
    Serial.println(WiFi.macAddress());

    if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
    }
    esp_now_register_recv_cb(onReceive);
    Serial.println("Waiting for the sender");
}

void loop()
{
    delay(1000);
}

Code walkthrough

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

setup sketch.ino

Turns on the WiFi radio in station mode, starts ESP-NOW and registers the send callback. Then it adds the receiver as a peer using receiverMac. Without that peer entry, the sender cannot address a message to the other board.

void setup()
{
    Serial.begin(115200);
    pinMode(kButton, INPUT_PULLUP);
    WiFi.mode(WIFI_STA);
    Serial.print("Sender MAC: ");
    Serial.println(WiFi.macAddress());

    if (esp_now_init() != ESP_OK) {
        Serial.println("Error initializing ESP-NOW");
        return;
    }
    esp_now_register_send_cb(onSent);

    esp_now_peer_info_t peer = {};
    memcpy(peer.peer_addr, receiverMac, 6);
    peer.channel = 0; // the radio's own channel
    peer.encrypt = false;
    if (esp_now_add_peer(&peer) != ESP_OK) {
        Serial.println("Failed to add peer");
        return;
    }
    Serial.println("Press the button");
}

loop sketch.ino

Checks the button every 20 ms. It only acts when the button goes from not pressed to pressed, so one press sends exactly one message. Each press flips msg.on, adds one to msg.presses and sends the struct with esp_now_send.

void loop()
{
    // Read every 20 ms: longer than the button bounces, so one press is one.
    bool pressed = digitalRead(kButton) == LOW;
    if (pressed && !wasPressed) {
        msg.on = !msg.on;
        msg.presses++;
        esp_err_t result = esp_now_send(receiverMac, (uint8_t *)&msg, sizeof(msg));
        Serial.print("Press ");
        Serial.print(msg.presses);
        Serial.print(": LED ");
        Serial.print(msg.on ? "on" : "off");
        Serial.println(result == ESP_OK ? ", sent" : ", not sent");
    }
    wasPressed = pressed;
    delay(20);
}

onReceive sketch.ino

Runs whenever a message arrives. It checks the length matches the Message struct, copies the bytes into one, and writes the on flag to the LED pin. It also prints who sent it and what it said.

// Arduino-ESP32 core 3.x's receive callback; core 2.x's took the sender's MAC
// as `const uint8_t *mac` instead of `info`, and Mokxi builds that one too.
void onReceive(const esp_now_recv_info_t *info, const uint8_t *data, int len)
{
    if (len != sizeof(Message)) return;
    Message msg;
    memcpy(&msg, data, sizeof(msg));
    digitalWrite(kLed, msg.on ? HIGH : LOW);
    char from[18];
    snprintf(from, sizeof(from), "%02X:%02X:%02X:%02X:%02X:%02X", info->src_addr[0], info->src_addr[1],
             info->src_addr[2], info->src_addr[3], info->src_addr[4], info->src_addr[5]);
    Serial.print("From ");
    Serial.print(from);
    Serial.print(", ");
    Serial.print(len);
    Serial.print(" bytes: press ");
    Serial.print(msg.presses);
    Serial.print(", LED ");
    Serial.println(msg.on ? "on" : "off");
}

Troubleshooting

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

  • The sender prints Delivery fail This project
    The address in receiverMac must match the receiver exactly. Even one wrong byte means nobody answers.
  • The receiver prints nothing when you press the button This project
    Make sure the receiver sketch is running and has printed Waiting for the sender before you press.
  • The LED is dark even when messages arrive This project
    Check the LED direction and that the resistor connects pin 4 on the second board to the LED.
  • 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.
  • The LED does not light, or it lights once and then dies. LED
    LEDs have polarity. The longer leg (anode) goes toward the positive side. Always add a series resistor, about 220 ohms to 1 k ohm on a 5 V pin.

Change it and keep it

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