Controlling DC Motor Speed and Direction with ESP32

Engineering illustration: Controlling DC Motor Speed and Direction with ESP32

ESP32 can control a DC motor through an H-bridge driver; a motor must never be connected directly to GPIO. The L298N is common in teaching, but its older bipolar output stage can lose several volts and dissipate substantial heat.

Power and wiring

Connect the motor to the driver outputs and use a separate motor supply matched to the load. Share ground with ESP32. IN1 and IN2 set direction, while ENA receives PWM for speed. Remove a fixed enable jumper when the module requires that for PWM control.

Current Arduino-ESP32 PWM API

const int ena = 25;
ledcAttach(ena, 20000, 8);   // pin, frequency, resolution
ledcWrite(ena, 180);         // duty: 0..255

The current Arduino-ESP32 API selects a channel automatically. Older core releases used a channel-oriented API, so check the installed package version when maintaining legacy examples.

Select the right driver

Check running and stall current, not voltage alone. Battery projects often benefit from a newer MOSFET driver with lower loss than the L298N. Add noise suppression, thermal protection, and a fuse where the risk assessment requires them.

Safety: Remove power before rewiring, restrain moving parts, and start with a low duty cycle.

Leave your comment

*
*
Only registered users can leave comments.