esp control leds
What’s In This Page
page under construction
If you are using a phone, at the bottom of the page there are links to many supplies for modelers. Otherwise those links are in the sidebar to the right. You can also find other information by using the search function that is in the navigation bar above.
Also In Addition To What’s In This Page, What This Site Can Do For You
If you are a model train enthusiast, modeler, diorama maker, or interested in making Arduino projects the easy way, there is categorized information for you here. Click Here.
The Latest Information
Anything new and page updates are posted on X (formerly Twitter), Rumble and Facebook.
It is easy to keep up with the latest by following us on either of them.
Free Help
Write a complete guide which includes equipment, wiring connections and programs for the following.
there are two esp32 i will call esp32-1 and esp32-2
on esp32-1 there are eight on off switches
on esp32-2 there are eight leds
the switches on esp32-1 remotely turn the leds on esp32-2 on and off
the esp communicate using es pnow
### Complete Guide for Remote Control of LEDs Using ESP32 and ESP-NOW
This guide provides step-by-step instructions to set up a system where ESP32-1 (with eight switches) remotely controls the LEDs on ESP32-2 using ESP-NOW for communication.
#### **Equipment Required**
1. **Two ESP32 development boards**
2. **Eight push-button switches**
3. **Eight LEDs**
4. **Eight 220Ω resistors (for LEDs)**
5. **Breadboards and jumper wires**
6. **USB cables for programming ESP32 boards**
7. **A computer with Arduino IDE installed**
#### **Wiring Connections**
**ESP32-1 (Switches)**
| Switch | GPIO Pin |
| —— | ——– |
| S1 | GPIO 12 |
| S2 | GPIO 13 |
| S3 | GPIO 14 |
| S4 | GPIO 27 |
| S5 | GPIO 26 |
| S6 | GPIO 25 |
| S7 | GPIO 33 |
| S8 | GPIO 32 |
* Connect one side of each switch to the corresponding GPIO pin as shown in the table.
* Connect the other side of each switch to the ground (GND).
**ESP32-2 (LEDs)**
| LED | GPIO Pin |
| — | ——– |
| L1 | GPIO 16 |
| L2 | GPIO 17 |
| L3 | GPIO 18 |
| L4 | GPIO 19 |
| L5 | GPIO 21 |
| L6 | GPIO 22 |
| L7 | GPIO 23 |
| L8 | GPIO 5 |
* Connect the anode (long leg) of each LED to the corresponding GPIO pin as shown in the table.
* Connect the cathode (short leg) of each LED to one end of a 220Ω resistor.
* Connect the other end of each resistor to the ground (GND).
#### **Programming the ESP32 Boards**
**ESP32-1 Code (Switches)**
“`cpp
#include <esp_now.h>
#include <WiFi.h>
// Structure to hold the button states
typedef struct struct_message {
int buttonStates[8];
} struct_message;
struct_message myData;
// Array of switch pins
const int switchPins[8] = {12, 13, 14, 27, 26, 25, 33, 32};
void setup() {
Serial.begin(115200);
// Initialize the switch pins as input
for (int i = 0; i < 8; i++) {
pinMode(switchPins[i], INPUT_PULLUP);
}
// Initialize WiFi in station mode
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Set the peer address
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println(“Failed to add peer”);
return;
}
}
void loop() {
// Read the switch states
for (int i = 0; i < 8; i++) {
myData.buttonStates[i] = digitalRead(switchPins[i]);
}
// Send the switch states to ESP32-2
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println(“Sent successfully”);
} else {
Serial.println(“Error sending the data”);
}
delay(100); // Send data every 100ms
}
“`
**ESP32-2 Code (LEDs)**
“`cpp
#include <esp_now.h>
#include <WiFi.h>
// Structure to hold the received button states
typedef struct struct_message {
int buttonStates[8];
} struct_message;
struct_message myData;
// Array of LED pins
const int ledPins[8] = {16, 17, 18, 19, 21, 22, 23, 5};
void setup() {
Serial.begin(115200);
// Initialize the LED pins as output
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
}
// Initialize WiFi in station mode
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Register receive callback
esp_now_register_recv_cb(onDataRecv);
}
void loop() {
// Nothing to do here
}
// Callback function that will be executed when data is received
void onDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
// Set the LED states based on the received button states
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], myData.buttonStates[i]);
}
}
“`
#### **Explanation**
1. **Initialization:**
* Both ESP32 boards are initialized in station mode (`WiFi.mode(WIFI_STA)`).
* ESP-NOW is initialized (`esp_now_init()`).
2. **ESP32-1 (Switches):**
* The GPIO pins are set up as inputs with pull-up resistors.
* The button states are read and stored in a structure.
* The structure is sent to ESP32-2 using `esp_now_send()`.
3. **ESP32-2 (LEDs):**
* The GPIO pins are set up as outputs.
* A callback function (`onDataRecv`) is registered to handle received data.
* When data is received, the LED states are updated based on the received button states.
#### **Running the System**
1. **Upload the ESP32-1 code** to the first ESP32 board.
2. **Upload the ESP32-2 code** to the second ESP32 board.
3. **Open the Serial Monitor** in the Arduino IDE for both boards to monitor the status.
4. **Press the switches** on ESP32-1 and observe the LEDs on ESP32-2 turning on and off accordingly.
This setup uses ESP-NOW for efficient and low-latency communication between the two ESP32 boards, making it ideal for real-time control applications like this one.

