ESP32 - Actuator
This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://esp32io.com/tutorials/esp32-actuator
This build uses an ESP32 and an L298N motor driver to control a 12V linear actuator that has just two wires, positive and negative. You will wire the driver between the ESP32 and the actuator, then control full-speed extend, full-speed retract, stop, and variable speed with PWM.
Supplies
Everything below is what the build uses. The links go to the parts we make and test.
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Narrow
- 38-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- 30-pin ESP32 ESP-WROOM-32 Dev Module - Wide
- ESP32 Uno-form board
- ESP32 S3 Uno-form board
- USB Cable Type-A to Type-C (for USB-A PC)
- USB Cable Type-C to Type-C (for USB-C PC)
- Linear Actuator
- L298N Motor Driver Module
- DC Power Jack
- Breadboard
- Jumper Wires
- Screw Terminal Expansion Board for ESP32
- Breakout Expansion Board for ESP32
- Power Splitter for ESP32
- DIYables ESP32 Starter Kit (ESP32 included)
- DIYables ESP32 S3 Starter Kit (ESP32 S3 included)
- DIYables Sensor Kit (18 sensors/displays)
Wiring
Remove all three jumpers on the L298N module before wiring anything. Connect the L298N VCC pin to the 12V power adapter and GND to a common ground shared with the ESP32. Power the L298N logic by connecting its 5V pin to the ESP32 5V. Wire the actuator's positive and negative wires to OUT1 and OUT2 on channel A. Connect IN1, IN2, and ENA on the L298N to digital/PWM-capable pins on the ESP32.
How It Works
IN1 and IN2 set the direction: one HIGH and the other LOW makes the actuator extend or retract at full speed, and setting both the same (HIGH or LOW) stops it. ENA sets the speed. Tying ENA HIGH runs the actuator at full speed, but feeding it a PWM signal lets you vary speed: higher duty cycle means higher speed, since the L298N amplifies that PWM to the actuator instead of you having to vary the 12V supply itself.
Code
The example sketch drives IN1 and IN2 with digitalWrite() to pick a direction, and drives ENA with analogWrite() to set speed, a value from 0 (stopped) to 255 (maximum speed). The demo sequence extends the actuator at maximum speed, stops it, retracts it at maximum speed, then stops it again. Upload it with the Arduino IDE after removing the L298N jumpers.
esp32-actuator.ino - the sketch from the tutorial, unchanged.
Full Tutorial
The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-actuator