Motor
Controlling DC Motor L298N with ESP32
What is the DC Motor L298N?
DC Motor L298N practical reference
A geared DC motor paired with the ubiquitous L298N dual H-bridge driver — the standard starting point for wheeled robots.
Key specifications
- Motor: Brushed DC gear motor — the common yellow "TT motor" spec is ~200 RPM at 6V, ~1:48 gear ratio
- Driver: L298N dual H-bridge, 2A per channel
- Control: One direction pair (IN1/IN2) plus one PWM speed pin (ENA) per motor
- Supply: Motor voltage 5V-35V DC on the driver's VMS terminal, separate from the MCU's own power
Pinout and connection notes
This entry covers the motor + L298N pairing as a unit — see the L298N Driver page for the full breakdown of every driver pin if you're wiring more than one motor.
- OUT1 / OUT2: Motor leads — connect directly to the L298N's motor A output terminal, polarity sets default spin direction
- ENA: PWM input on the driver — controls motor speed
- IN1 / IN2: Direction inputs on the driver — set which way current flows through the motor
- 12V / GND: Motor supply voltage and ground, into the driver's VMS/GND terminal
Integration checklist
- Confirm the exact part number and breakout-board revision.
- Verify supply voltage, current demand, logic levels, and signal direction against the datasheet.
- Check controller pin assignments and bus address or interface mode against the example code.
- Power up with external loads disconnected, then test signals before full integration.
- If readings or communication fail, check power and ground first, then pin mapping, interface settings, and library initialization.
Troubleshooting and reproducibility
Record the board used, wiring, library/version, supply arrangement, and measured behavior. Use the selected board-specific wiring example as the source of truth. Never assume visually similar modules share identical voltage tolerance or pin order.
Setting Up DC Motor L298N with ESP32
Wiring
| Component pin | ESP32 pin | Note |
|---|---|---|
| ENA | GPIO25 (PWM via ledc) | ESP32 has no analogWrite by default in older cores — use the ledc PWM API (or analogWrite if your core version supports the compatibility wrapper) |
| IN1 | GPIO26 | |
| IN2 | GPIO27 | |
| 12V / GND | Separate motor power supply — GND shared with the ESP32 | Never power the motor rail from the ESP32's 3.3V/5V pins |
Swipe sideways to see every column →
ESP32's ledc PWM API needs a channel set up once in setup() (ledcSetup/ledcAttachPin) rather than a plain analogWrite call — the logic is otherwise identical to the Uno version.
Related Components
BLDC ESC
High-speed brushless motor and electronic speed controller combo.
L298N Driver
Dual H-bridge motor driver — the standard for controlling two DC motors or one stepper.
NEMA17
The standard 42 mm stepper behind most 3D printers and small CNC machines.
Stepper 28BYJ48
Low-cost geared stepper for precise, low-speed positioning.
Related Guides & Projects
MicroPython vs Arduino on ESP32: Which Should You Learn First?
An honest comparison for beginners: where MicroPython is easier, where Arduino C++ is faster, how libraries and memory differ, and how to choose based on your project instead of hype.
MicroPython ESP32 Web Server: Control an LED or Relay from Your Browser
Create a working web server with the socket module, parse requests, serve a responsive control page for an LED or a relay, keep the displayed state truthful, choose a safe relay startup state and make the server survive bad or repeated requests.
MicroPython on ESP32 and Raspberry Pi Pico W: The Complete Guide
A structured MicroPython learning path for ESP32 and Pico W. Set up your tools, control GPIO, read sensors, connect to WiFi, build web servers, publish MQTT data and make projects reliable. Start here, then follow the links by level.
boot.py vs main.py in MicroPython: What Runs, When and Why
Understand the difference between boot.py and main.py in MicroPython, startup order, Wi-Fi setup, project structure, and recovery from boot errors.