This project provides a complete solution for controlling a DC motor using an ESP32 microcontroller and an L298N motor driver. The system features on/off control, speed control via PWM, and direction control with a web-based interface for remote operation.
- On/Off control of DC motor
- Speed control using PWM (0-255 range)
- Direction control (forward/reverse)
- Web-based interface for remote control
- Real-time status monitoring
- ESP32 development board
- L298N motor driver module
- DC motor
- Power supply for the motor (compatible with your motor's voltage requirements)
- Jumper wires for connections
ESP32 Pin -> L298N Pin
GPIO 18 -> ENA (Enable A - PWM)
GPIO 19 -> IN1 (Input 1 - Direction)
GPIO 23 -> IN2 (Input 2 - Direction)
VCC (3.3V) -> VCC (Logic power)
GND -> GND (Common ground)
Motor Power Supply:
L298N VCC <- External power supply positive (for motor)
L298N GND <- External power supply negative (common ground)
DC Motor:
L298N OUT1 -> Motor terminal 1
L298N OUT2 -> Motor terminal 2
- Install the Arduino IDE with ESP32 board support
- Install the following libraries:
- WiFi.h (included with ESP32 core)
- WebServer.h (included with ESP32 core)
- Open the
esp32_dc_motor_controller.inofile in Arduino IDE - Modify the WiFi credentials in the code:
const char* ssid = "your_wifi_ssid"; const char* password = "your_wifi_password";
- Upload the code to your ESP32 board
- ENA_PIN (GPIO 18): PWM signal for speed control
- IN1_PIN (GPIO 19): Direction control input 1
- IN2_PIN (GPIO 23): Direction control input 2
- ENB_PIN (GPIO 17): Enable B (for dual motor mode)
- IN3_PIN (GPIO 16): Input 3 (for dual motor mode)
- IN4_PIN (GPIO 4): Input 4 (for dual motor mode)
Once the ESP32 is connected to WiFi, the controller creates a web server accessible through the IP address shown in the serial monitor. The interface includes:
- Start/Stop buttons
- Speed control slider (0-255)
- Direction control buttons (Forward/Reverse)
- Real-time status display
The controller also provides REST API endpoints for programmatic control:
GET /- Main web interfaceGET /control?cmd=start- Start the motorGET /control?cmd=stop- Stop the motorGET /control?cmd=speed&value=[0-255]- Set motor speedGET /control?cmd=dir&value=[0|1]- Set motor direction (0=reverse, 1=forward)GET /status- Get current motor status
- Power on the ESP32 and L298N board
- Connect to the same WiFi network as the ESP32
- Open a web browser and navigate to the ESP32's IP address
- Use the interface to control the motor
Send HTTP GET requests to control the motor from other devices:
http://[ESP32_IP]/control?cmd=start
http://[ESP32_IP]/control?cmd=speed&value=200
http://[ESP32_IP]/control?cmd=dir&value=0
http://[ESP32_IP]/control?cmd=stop
- Ensure proper power supply ratings for both the ESP32 and motor
- Make sure all grounds are properly connected
- Monitor motor temperature during operation
- Implement emergency stop procedures in your application
- Use appropriate fuses/circuit protection for the motor power supply
- If motor doesn't respond, check all connections
- If WiFi connection fails, verify SSID and password
- If speed control doesn't work, ensure ENA pin is connected to a PWM-capable GPIO
- Check serial monitor for error messages
The code can be easily modified to:
- Add multiple motors
- Implement speed feedback
- Add acceleration/deceleration profiles
- Include additional sensors
- Create scheduled operations