What’s In This Page
This is a directory page with information related to operating retro trains, operating narrow gauge train lines and train museums. There are also selected video insertions.
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
update in progress
# Complete Guide for Controlling a Motor with ESP32 and ESP-NOW
This guide will walk you through the process of setting up two ESP32 devices (ESP32-1 and ESP32-2) to control a 12V DC motor using a potentiometer and a 3-way switch. The ESP32 devices will communicate using ESP-NOW.
## Equipment Needed
### ESP32-1:
– ESP32 development board
– Potentiometer
– 3-way switch (On-Off-On)
– Two LEDs (Green and Red)
– Resistors for LEDs (220Ω)
– Breadboard and jumper wires
### ESP32-2:
– ESP32 development board
– L293D motor driver IC
– 12V DC motor
– Two LEDs (Green and Red)
– Resistors for LEDs (220Ω)
– 12V power supply
– Breadboard and jumper wires
## Wiring Connections
### ESP32-1:
1. **Potentiometer:**
– Connect the middle pin of the potentiometer to GPIO 34.
– Connect the other two pins to 3.3V and GND.
2. **3-Way Switch:**
– Connect the middle pin to GND.
– Connect the other two pins to GPIO 25 and GPIO 26.
3. **LEDs:**
– Connect the Green LED’s anode to GPIO 18 via a 220Ω resistor, and the cathode to GND.
– Connect the Red LED’s anode to GPIO 19 via a 220Ω resistor, and the cathode to GND.
### ESP32-2:
1. **L293D Motor Driver:**
– Connect the 12V motor to the L293D’s motor output pins (pins 3 and 6).
– Connect L293D pin 1 (Enable 1) to 5V.
– Connect L293D pin 2 (Input 1) to GPIO 14.
– Connect L293D pin 7 (Input 2) to GPIO 27.
– Connect L293D pin 4, 5, 12, and 13 to GND.
– Connect L293D pin 8 to 12V power supply.
2. **LEDs:**
– Connect the Green LED’s anode to GPIO 32 via a 220Ω resistor, and the cathode to GND.
– Connect the Red LED’s anode to GPIO 33 via a 220Ω resistor, and the cathode to GND.
## Programming the ESP32s
### ESP32-1 Code:
This code reads the potentiometer value and the switch position, then sends the corresponding motor speed and direction to ESP32-2 using ESP-NOW.
“`cpp
#include <WiFi.h>
#include <esp_now.h>
#define POT_PIN 34
#define SWITCH_PIN_FWD 25
#define SWITCH_PIN_REV 26
#define GREEN_LED_PIN 18
#define RED_LED_PIN 19
typedef struct struct_message {
int motorSpeed;
bool direction; // true for forward, false for reverse
} struct_message;
struct_message myData;
// Callback when data is sent
void onDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print(“\r\nLast Packet Send Status:\t”);
Serial.println(status == ESP_NOW_SEND_SUCCESS ? “Delivery Success” : “Delivery Fail”);
}
void setup() {
Serial.begin(115200);
pinMode(POT_PIN, INPUT);
pinMode(SWITCH_PIN_FWD, INPUT_PULLUP);
pinMode(SWITCH_PIN_REV, INPUT_PULLUP);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
esp_now_register_send_cb(onDataSent);
// Register peer
esp_now_peer_info_t peerInfo;
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
if (esp_now_add_peer(&peerInfo) != ESP_OK) {
Serial.println(“Failed to add peer”);
return;
}
}
void loop() {
int potValue = analogRead(POT_PIN);
myData.motorSpeed = map(potValue, 0, 4095, 0, 255);
if (digitalRead(SWITCH_PIN_FWD) == LOW) {
myData.direction = true;
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
} else if (digitalRead(SWITCH_PIN_REV) == LOW) {
myData.direction = false;
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
} else {
myData.motorSpeed = 0;
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
}
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData));
if (result == ESP_OK) {
Serial.println(“Sent with success”);
} else {
Serial.println(“Error sending the data”);
}
delay(100);
}
“`
### ESP32-2 Code:
This code receives the motor speed and direction data from ESP32-1 and controls the motor accordingly using the L293D motor driver.
“`cpp
#include <WiFi.h>
#include <esp_now.h>
#define MOTOR_PIN1 14
#define MOTOR_PIN2 27
#define GREEN_LED_PIN 32
#define RED_LED_PIN 33
typedef struct struct_message {
int motorSpeed;
bool direction; // true for forward, false for reverse
} struct_message;
struct_message myData;
// Callback when data is received
void onDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&myData, incomingData, sizeof(myData));
Serial.print(“Bytes received: “);
Serial.println(len);
Serial.print(“Motor speed: “);
Serial.println(myData.motorSpeed);
Serial.print(“Motor direction: “);
Serial.println(myData.direction ? “Forward” : “Reverse”);
if (myData.direction) {
digitalWrite(MOTOR_PIN1, HIGH);
digitalWrite(MOTOR_PIN2, LOW);
digitalWrite(GREEN_LED_PIN, HIGH);
digitalWrite(RED_LED_PIN, LOW);
} else {
digitalWrite(MOTOR_PIN1, LOW);
digitalWrite(MOTOR_PIN2, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, HIGH);
}
analogWrite(MOTOR_PIN1, myData.motorSpeed);
}
void setup() {
Serial.begin(115200);
pinMode(MOTOR_PIN1, OUTPUT);
pinMode(MOTOR_PIN2, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
if (esp_now_init() != ESP_OK) {
Serial.println(“Error initializing ESP-NOW”);
return;
}
esp_now_register_recv_cb(onDataRecv);
}
void loop() {
// Put your main code here, to run repeatedly:
}
“`
## Conclusion
With these setups and code, you should be able to control the speed and direction of a 12V DC motor using a potentiometer and a 3-way switch. The ESP32-1 reads the input from the potentiometer and switch, sends the data to ESP32-2 via ESP-NOW, and ESP32-2 controls the motor based on the received data. The LEDs on both ESP32s indicate the motor’s direction.

