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.

This tutorial shows how to program the ESP32 using the Arduino language (C/C++) via the Arduino IDE. If you’d like to learn how to program the ESP32 with MicroPython, visit this ESP32 MicroPython - Actuator tutorial.

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 control a 5V linear actuator, you still need a driver in between ESP32 and linear actuator because the ESP32 pin outputs only 3.3V and does not provide enough current for the linear actuator.

Many chips and modules can work as a linear actuator driver. The BTS7960 is the driver we picked. It replaced the L298N on this page: an actuator pulls a big current when it starts and when it hits the end of its stroke, and the BTS7960 handles that easily, with almost no voltage loss.

※ 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 BTS7960 Driver

The BTS7960 module (also sold as IBT-2) drives ONE motor or ONE linear actuator in both directions. It has two BTS7960B power chips. Each chip connects one actuator wire to the + or to the - of the power supply. Together, the two chips make a full H-bridge, so the current can flow through the actuator in both directions.

BTS7960 Driver Pinout

BTS7960 Motor Driver Pinout

The module has an 8-pin header for the control signals and four screw terminals for the power.

The header pins:

  • RPWM pin: a PWM signal on this pin makes the actuator extend. The value sets the speed.
  • LPWM pin: a PWM signal on this pin makes the actuator retract. The value sets the speed.
  • R_EN and L_EN pins: the enable inputs. A HIGH level switches each half of the driver on, and a LOW level switches it off. The actuator moves only when both are HIGH.
  • R_IS and L_IS pins: current sense outputs. We do not use them in this tutorial.
  • VCC pin: the power for the small logic chip (a 74HC244 buffer) on the module. It needs 5V. On the ESP32, connect it to the VIN pin, which gives 5V from the USB cable.
  • GND pin: connect it to GND of the ESP32.

The screw terminals:

  • B+ and B- terminals: the power for the actuator, from 5.5V to 27V. We use a 12V adapter here.
  • M+ and M- terminals: the two wires of the linear actuator go here.

The control inputs work with 3.3V and 5V signals, so the ESP32 can drive them directly. They have pull-down resistors inside, so no 5V comes back to the ESP32 pins. The driver can give up to 43A for a short time, which is much more than a 12V actuator needs.

How To Control Linear Actuator

Here, the ESP32 sends its commands to a BTS7960 module, and the module powers the actuator.

Wiring Diagram

The ESP32 gets its power from the USB cable. The actuator gets its power from the 12V adapter, through the B+ and B- terminals.

ESP32 Linear Actuator BTS7960 Driver Wiring Diagram

This image is created using Fritzing. Click to enlarge image

BTS7960 Module ESP32 / Other
RPWM → GPIO16
LPWM → GPIO17
R_EN → GPIO19
L_EN → GPIO18
VCC → VIN (5V)
GND → GND
R_IS, L_IS not connected
B+, B- → + and - of the 12V adapter (through the DC power jack)
M+, M- → the two wires of the linear actuator

How To Make Linear Actuator Expand/Retract

First, the ESP32 sets R_EN and L_EN to HIGH, so the driver is on. Then the two PWM pins choose the direction. The below table shows how it works.

RPWM pin LPWM pin Direction
0 0 Linear Actuator stops
255 (HIGH) 0 Linear Actuator extends
0 255 (HIGH) Linear Actuator retracts

One of RPWM and LPWM always stays at 0, so the two pins never get PWM together.

  • Extends Linear Actuator
analogWrite(LPWM_PIN, 0); analogWrite(RPWM_PIN, 255);
  • Retracts Linear Actuator
analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 255);

※ NOTE THAT:

The moving direction is inverted if the M+ & M- terminals connect to two pins of the linear actuator in an inverse way. To correct it, reverse the two wires on M+ & M-. Trading the RPWM and LPWM values in your code works as well.

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

  • Brake: leave R_EN and L_EN at HIGH and drop RPWM and LPWM to 0. The actuator halts at once.
analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 0);
  • Disable both half-bridges with LOW on R_EN and L_EN. The outputs are off, and the actuator stops.
digitalWrite(R_EN_PIN, LOW); digitalWrite(L_EN_PIN, LOW);

How To Control the Speed of Linear Actuator via BTS7960 Driver

To change the speed, use a value smaller than 255 on the active PWM pin. We make the PWM signal with the analogWrite() function. The BTS7960 repeats this weak signal at 12V, with plenty of current for the actuator.

analogWrite(RPWM_PIN, speed); // extend, speed is a value from 0 to 255 analogWrite(LPWM_PIN, speed); // or retract, 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.

If you're unfamiliar with how to supply power to the ESP32 and other components, you can find guidance in the following tutorial: The best way to Power ESP32 and sensors/displays.

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 */ // ESP32 controls a 12V linear actuator with the BTS7960 motor driver // RPWM = extend, LPWM = retract. R_EN and L_EN must be HIGH. #define RPWM_PIN 16 // The ESP32 pin GPIO16 connected to the RPWM pin of the BTS7960 #define LPWM_PIN 17 // The ESP32 pin GPIO17 connected to the LPWM pin of the BTS7960 #define R_EN_PIN 19 // The ESP32 pin GPIO19 connected to the R_EN pin of the BTS7960 #define L_EN_PIN 18 // The ESP32 pin GPIO18 connected to the L_EN pin of the BTS7960 // the setup function runs once when you press reset or power the board void setup() { // initialize digital pins as outputs. pinMode(RPWM_PIN, OUTPUT); pinMode(LPWM_PIN, OUTPUT); pinMode(R_EN_PIN, OUTPUT); pinMode(L_EN_PIN, OUTPUT); // no PWM yet, then turn the driver on analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 0); digitalWrite(R_EN_PIN, HIGH); digitalWrite(L_EN_PIN, HIGH); } // the loop function runs over and over again forever void loop() { // extend the actuator at maximum speed analogWrite(LPWM_PIN, 0); analogWrite(RPWM_PIN, 255); delay(20000); // actuator will stop extending automatically when reaching the limit // retracts the actuator at maximum speed analogWrite(RPWM_PIN, 0); analogWrite(LPWM_PIN, 255); delay(20000); // actuator will stop retracting automatically when reaching the limit }

Quick Instructions

  • Do the wiring as the above image. Plug in the 12V adapter last.
  • 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 an amount of time
    • Linear actuator retracts and then stops when reaching the limit
    • Linear actuator keeps the position an amount 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