Complete Guide to Controlling LEDs on One ESP32 with Switches on Another using ESP-NOW
What’s In This Page
This guide covers the setup, wiring, and programming of two ESP32 boards to control LEDs on one board using switches on another via ESP-NOW communication.
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.
OverView
This project uses two ESP32 boards. They are named ESP32-1 and ESP32-2.
On ESP32-1 1 there are 8 on/off switches that control individual LEDs that are on ESP32-2
This project is first set up on a breadboard and later moved to a prototype format. In that format the LED pins will act as the gates for Mosfet switches. They will control on/off power for a display of dioramas.
Equipment Needed
1. ESP32-1:
– 1 ESP32 development board
– 8 push-button switches
– 8 pull-down resistors (10k ohms)
– Breadboard and jumper wires
2. ESP32-2:
– 1 ESP32 development board
– 8 LEDs
– 8 current-limiting resistors (220 ohms)
– Breadboard and jumper wires
Wiring Connections
ESP32-1 (Switches)
1. **Connect Switches:**
– Connect one terminal of each switch to ground (GND).
– Connect the other terminal of each switch to the input pins on the ESP32-1 through pull-down resistors. Here are the GPIO connections:
– Switch 1 to GPIO 13
– Switch 2 to GPIO 12
– Switch 3 to GPIO 14
– Switch 4 to GPIO 27
– Switch 5 to GPIO 26
– Switch 6 to GPIO 25
– Switch 7 to GPIO 33
– Switch 8 to GPIO 32
2. Pull-down Resistors:
– Connect one end of each pull-down resistor to GND.
– Connect the other end to the corresponding switch pin and ESP32 GPIO pin.
ESP32-2 (LEDs)
1. **Connect LEDs:**
– Connect the anode (longer leg) of each LED to the output pins on the ESP32-2 through current-limiting resistors. Here are the GPIO connections:
– LED 1 to GPIO 13
– LED 2 to GPIO 12
– LED 3 to GPIO 14
– LED 4 to GPIO 27
– LED 5 to GPIO 26
– LED 6 to GPIO 25
– LED 7 to GPIO 33
– LED 8 to GPIO 32
2. Current-limiting Resistors:
– Connect one end of each current-limiting resistor to the anode of the corresponding LED.
– Connect the other end to the corresponding ESP32 GPIO pin.
3. Ground Connections:
– Connect the cathode (shorter leg) of each LED to GND.
Programming the ESP32 Boards
Libraries Needed
Before starting, install the necessary libraries in the Arduino IDE:
– ESP32 by Espressif Systems
– ESP-NOW
ESP32-1 Code (Transmitter)
#include <esp_now.h>
#include <WiFi.h>
// Define the GPIO pins for switches
const int switchPins[8] = {13, 12, 14, 27, 26, 25, 33, 32};
// MAC address of the receiver (ESP32-2)
uint8_t receiverMAC[] = {0x24, 0x6F, 0x28, 0x8C, 0x57, 0xE0}; // Replace with your receiver’s MAC address – to find that MAC address go here. MMMMMMMMM
typedef struct struct_message {
bool switches[8];
} struct_message;
struct_message myData;
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, receiverMAC, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println(“Failed to add peer”);
return;
}
// Initialize switch pins
for (int i = 0; i < 8; i++) {
pinMode(switchPins[i], INPUT);
}
}
void loop() {
// Read switch states and store them in the struct
for (int i = 0; i < 8; i++) {
myData.switches[i] = digitalRead(switchPins[i]);
}
// Send message via ESP-NOW
esp_err_t result = esp_now_send(receiverMAC, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println(“Sent with success”);
} else {
Serial.println(“Error sending the data”);
}
delay(500);
}
“`
ESP32-2 Code (Receiver)
#include <esp_now.h>
#include <WiFi.h>
// Define the GPIO pins for LEDs
const int ledPins[8] = {13, 12, 14, 27, 26, 25, 33, 32};
typedef struct struct_message {
bool switches[8];
} struct_message;
struct_message myData;
// 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 received data
for (int i = 0; i < 8; i++) {
digitalWrite(ledPins[i], myData.switches[i] ? HIGH : LOW);
}
}
void setup() {
// Initialize Serial Monitor
Serial.begin(115200);
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
// Initialize ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
// Register the receive callback function
esp_now_register_recv_cb(onDataRecv);
// Initialize LED pins
for (int i = 0; i < 8; i++) {
pinMode(ledPins[i], OUTPUT);
digitalWrite(ledPins[i], LOW); // Ensure LEDs are off initially
}
}
void loop() {
// Nothing to do here, everything is handled in the callback function
}
“`
Step-by-Step Instructions
1. Setup the Hardware:
– Assemble the circuits on breadboards as per the wiring diagrams for ESP32-1 and ESP32-2.
– Connect the ESP32 boards to your computer using USB cables.
2. Configure the Arduino IDE:
– Open the Arduino IDE.
– Go to `File` > `Preferences` and add the following URL to the Additional Boards Manager URLs: `https://dl.espressif.com/dl/package_esp32_index.json`.
– Go to `Tools` > `Board` > `Boards Manager`, search for `esp32` and install the `esp32` package by Espressif Systems.
3. Program ESP32-1:
– Copy and paste the ESP32-1 code into a new Arduino sketch.
– Replace the `receiverMAC` array with the MAC address of ESP32-2 (you can find it by running a basic WiFi sketch on ESP32-2 to print its MAC address).
– Select the appropriate board (`ESP32 Dev Module`) and the correct port in the Arduino IDE.
– Upload the code to ESP32-1.
4. Program ESP32-2:
– Copy and paste the ESP32-2 code into a new Arduino sketch.
– Select the appropriate board (`ESP32 Dev Module`) and the correct port in the Arduino IDE.
– Upload the code to ESP32-2.
5. Test the Setup:
– Press the switches on ESP32-1 and observe the corresponding LEDs on ESP32-2 lighting up or turning off.
– Ensure the communication is reliable and there is minimal delay in response.
Feedback
Your questions and comments help us clarify and upgrade the information presented. Even if you find this helpful, please tell us.
Please let us know if this page has been helpful And if you have questions or suggestions, use this spam free system.
Or use the green WhatsApp button that is to the right.