ESP32 - Actuator

by DIYables in Circuits > Microcontrollers

66 Views, 1 Favorites, 0 Comments

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

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.

/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/esp32-actuator
*/

#define ENA_PIN 27 // The ESP32 pin GPIO27 connected to the EN1 pin L298N
#define IN1_PIN 26 // The ESP32 pin GPIO26 connected to the IN1 pin L298N
#define IN2_PIN 25 // The ESP32 pin GPIO25 connected to the IN2 pin L298N


// the setup function runs once when you press reset or power the board
void setup() {
// initialize digital pins as outputs.
pinMode(ENA_PIN, OUTPUT);
pinMode(IN1_PIN, OUTPUT);
pinMode(IN2_PIN, OUTPUT);

digitalWrite(ENA_PIN, HIGH);
}

// the loop function runs over and over again forever
void loop() {
// extend the actuator
digitalWrite(IN1_PIN, HIGH);
digitalWrite(IN2_PIN, LOW);

delay(20000); // actuator will stop extending automatically when reaching the limit

// retracts the actuator
digitalWrite(IN1_PIN, LOW);
digitalWrite(IN2_PIN, HIGH);

delay(20000); // actuator will stop retracting automatically when reaching the limit
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://esp32io.com/tutorials/esp32-actuator