ESP32 - Actuator

This tutorial instructs you how to use ESP32 to control a linear actuator. In detail, we will learn:

This tutorial is for linear actuator without feedback. If you want to learn about linear actuator with feedback, see this ESP32 - Actuator with Feedback tutorial.

Hardware Used In This Tutorial

1×ESP-WROOM-32 Dev Module
1×USB Cable Type-C
1×Linear Actuator
1×L298N Motor Driver Module
1×12V Power Adapter
1×(Optional) DC Power Jack
1×Breadboard
1×Jumper Wires
1×(Recommended) ESP32 Screw Terminal Adapter

Or you can buy the following sensor kits:

1×DIYables Sensor Kit (30 sensors/displays)
1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: some of these links are affiliate links. We may earn a commission on your purchase at no extra cost to you. We appreciate it.

Introduction to Linear Actuator

Linear Actuator Extend Retract

Linear Actuator Pinout

Linear Actuator has two wires:

  • Positive wire: usually red
  • Negative wire: usually black
Linear Actuator Pinout

How It Works

When you buy a linear actuator, you need to know what voltage linear actuator works. Let's take a 12V linear actuator as an example.

When you power the 12V linear actuator by a 12V power source:

  • 12V and GND to the positive wire and negative wire, respectively: the linear actuator full-speed extends until it reaches the limit.
  • 12V and GND to the negative wire and positive wire, respectively: the linear actuator full-speed retracts until it reaches the limit.

While extending or retracting, if we stop power to the actuator (GND to both positive and negative wire), the actuator stops extending/retracting

※ NOTE THAT:

For DC motor, servo motor, and stepper motor without gearing, when carrying a load, if we stop powering, they cannot keep the position. Unlike these motors, the actuator can keep the position even when stopping powering while carrying a load.

If we provide power to linear actuators below 12V, the linear actuator still extends/retracts but not at maximum speed. It means if we change the voltage of the power supply, we can change the speed of the linear actuator. However, this method is not used in practice because of the difficulty in controlling the voltage of the power source. Instead, we fix the voltage of the power source and control the speed of the linear actuator via a PWM signal. The more duty cycle the PWM is, the higher speed the linear actuator extends/retracts.

How to control linear actuator

How to control a linear actuator using ESP32

Controlling a linear actuator includes:

  • Extends the linear actuator at maximum speed.
  • Retracts the linear actuator at maximum speed.
  • (optional) controls the extending/retracting speed

ESP32 can generate the signal to control the linear actuator. However, this signal has low voltage and current, We cannot use it to control the linear actuator. We need to use a hardware driver in between ESP32 and linear actuator. The driver does two works:

  • Amplify the control signal from ESP32 (current and voltage)
  • Receive the another control signal from ESP32 to swap pole of power supply → for direction control.

※ NOTE THAT:

  • This tutorial can be applied to all linear actuators. 12V linear actuator is just an example.
  • When you controls 5V linear actuator, although ESP32 pin outputs 5V (the same as linear actuator voltage), you still needs a driver in between ESP32 and linear actuator because the ESP32 pin does not provide enough the current for linear actuator.

There are many kinds of the chip, modules (e.g. L293D, L298N) can be used as linear actuator drivers. In this tutorial, we will use the L298N driver.

※ NOTE THAT:

You can also use relays as a driver. However it requires 4 relays to control a single linear actuator (both extend/retract)

Introduction to L298N Driver

L298N Driver can be used to control linear actuator, DC motor and stepper motor. In this tutorial, we learn how to use it to control the linear actuator.

L298N Driver Pinout

L298N Driver Pinout

L298N Driver Pinout

L298N Driver has two channels, called channel A and channel B. Therefore, L298N Driver can control two linear actuator independently at the same time. Let's assum that the linear actuator A is connected to channel A, the linear actuator B is connected to channel B. L298N Driver has 13 pins:

The common pins for both channels:

  • VCC pin: supplies power for the linear actuator. It can be anywhere between 5 to 35V.
  • GND pin: is a common ground pin, needs to be connected to GND (0V).
  • 5V pin: supplies power for L298N module. It can be supplied by 5V from ESP32.

Channel A pins:

  • ENA pins: are used to control the speed of the linear actuator A. Removing the jumper and connecting this pin to PWM input will let us control the extending/retracting speed of the linear actuator A.
  • IN1 & IN2 pins: are used to control the moving direction of a linear actuator. When one of them is HIGH and the other is LOW, the a linear actuator will extend or retract. If both the inputs are either HIGH or LOW the linear actuator will stop.
  • OUT1 & OUT2 pins: are connected to a linear actuator A.

Channel B pins:

  • ENB pins: are used to control the speed of the linear actuator B. Removing the jumper and connecting this pin to PWM input will let us control the extending/retracting speed of the linear actuator B.
  • IN3 & IN4 pins: are used to control the moving direction of a linear actuator. When one of them is HIGH and the other is LOW, the a linear actuator will extend or retract. If both the inputs are either HIGH or LOW the linear actuator will stop.
  • OUT3 & OUT4 pins: are connected to a linear actuator.

As described above, the L298N driver has two input powers:

  • One for linear actuator (VCC and GND pins): from 5 to 35V.
  • One for the L298N module's internal operation (5V and GND pins): from 5 to 7V.

The L298N driver also has three jumpers for advanced uses or other purposes. For the sake of simplicity, please remove all jumpers from the L298N driver.

We can control two linear actuators independently at the same time by using an ESP32 and an L298N Driver. To control each linear actuator, we need only three pins from ESP32.

※ NOTE THAT:

The rest of this tutorial controls a linear actuator using channel A. Controlling the other linear actuator is similar.

How To Control Linear Actuator

We will learn how to control Linear Actuator using L298N driver

Wiring Diagram

Please remove all three jumpers on the L298N module before wiring.

ESP32 Linear Actuator L298N Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: How to Power ESP32.

How To Make Linear Actuator Expand/Retract

The moving direction of a Linear Actuator can be controlled by applying a logic HIGH/LOW to IN1 and IN2 pins. The below table illustrates how to control the direction in both channels.

IN1 pin IN2 pin Direction
LOW LOW Linear Actuator A stops
HIGH HIGH Linear Actuator A stops
HIGH LOW Linear Actuator A extends
LOW HIGH Linear Actuator A retracts
  • Extends Linear Actuator A
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, LOW);
  • Retracts Linear Actuator A
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, HIGH);

※ NOTE THAT:

The moving direction is inverted if the OUT1 & OUT2 pin connects to two pins of the linear actuator in an inverse way. If so, it just needs to swap between OUT1 & OUT2 pin or change the control signal on IN1 and IN2 pin in the code.

How To Stop Linear Actuator from Extending or Retracting

The linear actuator automatically stops extending/retracting when it reaches the limit. We can also programmably stop it from extending/retracting while it has not reached the limit.

There are two ways to stop linear actuator

  • Controls the speed to 0
analogWrite(ENA_PIN, 0);
  • Controls IN1 IN2 pins to the same value (LOW or HIGH)
digitalWrite(IN1_PIN, LOW); digitalWrite(IN2_PIN, LOW);
  • Or
digitalWrite(IN1_PIN, HIGH); digitalWrite(IN2_PIN, HIGH);

How To Control the Speed of Linear Actuator via L298N Driver

It is simple to control the speed of the linear actuator. Instead of controlling ENA pin to HIGH, We generate a PWM signal to the ENA pin. We can do this by:

  • Connect an ESP32 pin to ENA of L298N
  • Generate PWM signal to ENA pin by using analogWrite() function. L298N Driver amplify PWM signal to linear actuator
analogWrite(ENA_PIN, speed); // speed is a value from 0 to 255

The speed is a value between 0 and 255. If the speed is 0, the linear actuator stops. If the speed is 255, the linear actuator extends/retracts at maximum speed.

ESP32 Example Code

The below code does:

  • Extend actuator at maximum speed
  • Stop the linear actuator
  • Retract actuator at maximum speed
  • Stop the linear actuator
/* * 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 }

Quick Instructions

  • Remove all three jumpers on the L298N module.
  • Copy the above code and paste it to Arduino IDE
  • Compile and upload code to ESP32 board by clicking Upload button on Arduino IDE
  • You will see:
    • Linear actuator extends and then stops when reaching the limit
    • Linear actuator keeps the position a mount of time
    • Linear actuator retracts and then stops when reaching the limit
    • Linear actuator keeps the position a mount of time
    • The above process is run repeatedly.

Video Tutorial

Making video is a time-consuming work. If the video tutorial is necessary for your learning, please let us know by subscribing to our YouTube channel , If the demand for video is high, we will make the video tutorial.

※ OUR MESSAGES