Wireless Button Control Using ESP-NOW
ESPHow ToIoT HardwaresIoT ProtocolsProgrammingTutorials/DIY

Wireless Button Control Using ESP-NOW

ESP32 ESP8266 NodeMCU – Remote Control Without Wi-Fi Router

In this tutorial, you’ll learn how to build a simple wireless remote control system using the ESP-NOW protocol. We will create a project where one ESP board acts as a transmitter connected to a physical push button, and another ESP board acts as a receiver that controls an LED or relay module.

The best part?

✔ No Wi-Fi router required
✔ No internet connection needed
✔ Extremely fast peer-to-peer communication

This makes ESP-NOW a perfect choice for wireless button-based IoT control.

Read This: ESP-NOW Protocol: A Complete Guide for ESP32 and ESP8266


Table of Contents

  • Project Overview
  • Why Use ESP-NOW for Button Control
  • Hardware Requirements
  • Wiring Diagrams
  • Understanding MAC Address Pairing
  • Arduino Code for ESP-NOW Sender
  • Arduino Code for ESP-NOW Receiver
  • Testing the System
  • Expanding the Project
  • Troubleshooting Tips
  • Wrapping Up

Project Overview

We will build a two-device setup:

DeviceRole
ESP32 / ESP8266Sender – button node
ESP32 / ESP8266Receiver – LED controller
LED ModuleOutput Device

When the button on the sender device is pressed:

👉 A wireless ESP-NOW packet is sent
👉 The receiver gets the command
👉 LED turns ON or OFF

You can also replace the LED with a relay to control real appliances.


Why Use ESP-NOW for Wireless Button Control?

Normally, remote button control using Wi-Fi requires:

  • connecting to router,
  • creating a web server,
  • or using MQTT/Bluetooth.

ESP-NOW simplifies everything.

Traditional method:

Button → ESP → Router → Server → ESP

ESP-NOW method:

Button Node → Controller Node

Advantages

  • Ultra-low latency
  • Lower power consumption
  • Simple architecture
  • Works in noisy network environments

For applications like smart switches, doorbells, or emergency triggers, ESP-NOW is much more efficient than HTTP or Bluetooth.


Hardware Requirements

To follow along, you need:

  • Two ESP boards (any combination):
    • ESP32 Dev Board
    • ESP8266 NodeMCU
  • One push button
  • One LED + 220Ω resistor
  • USB cables
  • Arduino IDE installed on PC

Wiring the Push Button to ESP Sender

Let’s connect a simple button circuit to the sender ESP board.

ComponentESP Pin
Button one legGPIO 0
Button other legGND

You can use any GPIO pin, but in this example we use GPIO0.


Wiring the LED to ESP Receiver

LED PinESP32ESP8266
PositiveGPIO2GPIO2
NegativeGNDGND

MAC Address Pairing

Before ESP-NOW devices can communicate, you must know the receiver MAC address.


Getting Receiver MAC Address

Upload this simple sketch once to the receiver board:

For ESP32:

#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  Serial.println("MAC Address:");
  Serial.println(WiFi.macAddress());
}

void loop() {}

For ESP8266:

#include <ESP8266WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  Serial.println(WiFi.macAddress());
}

void loop() {}

Open the Serial Monitor and copy the MAC address. You’ll need it in the sender program.


ESP-NOW Sender Code

Now let’s program the sender ESP board.

This script:

  • reads button state,
  • and sends ON/OFF command to receiver.

Sender Code (ESP8266 Example)

#include <espnow.h>
#include <ESP8266WiFi.h>

#define BUTTON_PIN 0

uint8_t receiverMAC[] = {0x24, 0x6F, 0x28, 0xA1, 0xB2, 0xC3};

typedef struct struct_message {
  bool buttonPressed;
} struct_message;

struct_message outgoingData;

void onSent(uint8_t *mac_addr, uint8_t status) {
  Serial.println(status == 0 ? "Send Success" : "Send Fail");
}

void setup() {
  Serial.begin(115200);
  pinMode(BUTTON_PIN, INPUT_PULLUP);

  WiFi.mode(WIFI_STA);

  if (esp_now_init() != 0) {
    Serial.println("ESP-NOW Init Failed");
    return;
  }

  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
  esp_now_register_send_cb(onSent);

  esp_now_add_peer(receiverMAC, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}

void loop() {

  if(digitalRead(BUTTON_PIN) == LOW) {
    outgoingData.buttonPressed = true;
  } else {
    outgoingData.buttonPressed = false;
  }

  esp_now_send(receiverMAC, (uint8_t *) &outgoingData, sizeof(outgoingData));

  delay(100);
}

How the Sender Code Works

  • The button is configured as INPUT with internal pull-up.
  • Whenever GPIO0 reads LOW, it means the button is pressed.
  • A small structure packet is transmitted every 100 ms.

ESP-NOW Receiver Code

Now let’s write the program for the receiving ESP board.


Receiver Code (ESP32 Example)

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

#define LED_PIN 2

typedef struct struct_message {
  bool buttonPressed;
} struct_message;

struct_message incomingData;

void onReceive(const esp_now_recv_info *info, const uint8_t *data, int len) {

  memcpy(&incomingData, data, sizeof(incomingData));

  if(incomingData.buttonPressed) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

void setup() {
  Serial.begin(115200);

  pinMode(LED_PIN, OUTPUT);

  WiFi.mode(WIFI_STA);

  if (esp_now_init() != ESP_OK) {
    Serial.println("ESP-NOW Init Failed");
    return;
  }

  esp_now_register_recv_cb(onReceive);
}

void loop() {}

How the Receiver Code Works

  • Uses callback onReceive() to accept ESP-NOW packets.
  • Turns LED ON when packet says true.
  • Turns LED OFF when packet says false.

Testing the System

Step-by-Step Testing

  1. Upload the receiver code to ESP32 or ESP8266.
  2. Open Serial Monitor (optional).
  3. Upload sender code to second ESP board.
  4. Press the physical push button.

Expected Behavior

  • When you press the button on the sender node:
    ✔ Receiver LED turns ON immediately
  • When released:
    ✔ LED turns OFF

The reaction feels real-time, like wired communication.


Expanding the Project Further

This simple example can be extended in many ways.

You can build:

  • Wireless doorbell system
  • Appliance control with relays
  • Multiple buttons to control multiple outputs
  • Motor or buzzer control
  • Long-range button triggers

Troubleshooting Tips

If things don’t work as expected:

1) ESP-NOW Init Failed

  • Make sure Wi-Fi mode is set to STA.
  • Use correct libraries for your board.

2) Packets Not Received

  • Double-check MAC address bytes.
  • Re-flash firmware.
  • Ensure boards are within range.

3) Constant Reboots

  • Power supply issues (brownout).
  • Try another USB cable.

Wrapping Up

In this tutorial, you learned how to create a very simple wireless control system using a push button and ESP-NOW protocol. This method is efficient, fast, and perfect for local IoT communication.

ESP-NOW opens the door to building reliable peer-to-peer embedded systems without complex networking.

Harshvardhan Mishra

Hi, I'm Harshvardhan Mishra. Tech enthusiast and IT professional with a B.Tech in IT, PG Diploma in IoT from CDAC, and 6 years of industry experience. Founder of HVM Smart Solutions, blending technology for real-world solutions. As a passionate technical author, I simplify complex concepts for diverse audiences. Let's connect and explore the tech world together! If you want to help support me on my journey, consider sharing my articles, or Buy me a Coffee! Thank you for reading my blog! Happy learning! Linkedin

Leave a Reply

Your email address will not be published. Required fields are marked *