Raspberry Pi - Actuator
This tutorial instructs you how to use Raspberry Pi to control Linear Actuator. In detail, we will learn:
- How a linear actuator works
- How to program Raspberry Pi to make a linear actuator extend or retract
- How to program Raspberry Pi to control the speed of a linear actuator
There are two types of linear actuators:
- Linear actuator without feedback: This one usually extends or retracts all the way and cannot be precisely controlled to stop at a specific position.
- Linear actuator with feedback: This one has a feedback signal that allows it to be precisely controlled to extend or retract to a specific position.
This tutorial is for linear actuator without feedback. If you are looking to learn about linear actuator with feedback, please refer to this Raspberry Pi - Actuator with Feedback tutorial.
Hardware Preparation
Or you can buy the following kits:
| 1 | × | DIYables Sensor Kit (18 sensors/displays) |
Additionally, some of these links are for products from our own brand, DIYables .
Overview of Linear Actuator

Linear Actuator Pinout
A Linear Actuator has two wires, with the positive wire typically being red and the negative wire usually black.

How It Works
When purchasing a linear actuator, it is important to understand what voltage it requires to operate. As an example, let's consider a 12V linear actuator.
When you power the 12V linear actuator by a 12V power source:
- Connect 12V to the positive wire and GND to the negative wire: the linear actuator full-speed extends until it reaches the limit.
- Connect 12V to the negative wire and GND to the positive wire: the linear actuator full-speed retracts until it reaches the limit.
When power is cut off to the actuator by connecting the ground wire to both the positive and negative wires, the process of extending or retracting will cease.
※ NOTE THAT:
For DC motor, servo motor, and stepper motor without gearing, when a load is present, if the power is cut off, they are unable to maintain their position. In contrast, an actuator can remain in its position even when the power is no longer supplied while it is carrying a load.
If the voltage of the power supply for linear actuators is less than 12V, the linear actuator will still extend/retract, but not at its maximum speed. This implies that we can alter the speed of the linear actuator by changing the voltage. Nevertheless, this method is not commonly used due to the difficulty in controlling the voltage of the power source. Therefore, the voltage of the power source is kept constant and the speed of the linear actuator is regulated with a PWM signal. The greater the duty cycle of the PWM, the faster the linear actuator extends/retracts.

How to control a linear actuator using Raspberry Pi
Controlling a linear actuator involves:
- Extending the linear actuator at its maximum rate.
- Retracting the linear actuator at its maximum rate.
- (Optional) Regulating the extending/retracting speed.
Raspberry Pi is able to generate a signal to control the linear actuator, however, this signal has low voltage and current. Therefore, a hardware driver is required in between Raspberry Pi and linear actuator to amplify the control signal from Raspberry Pi, as well as to receive another control signal from Raspberry Pi to switch the pole of power supply for direction control.
※ NOTE THAT:
- This tutorial can be used with any linear actuator. 12V linear actuator is just one example.
- A GPIO pin of the Raspberry Pi gives only 3.3V and a few milliamps. Even a small 5V linear actuator needs far more current, so a driver is always needed between the Raspberry Pi and the actuator.
There are numerous types of chips and modules that can be used as linear actuator drivers. From all of these, this tutorial uses the BTS7960 driver.
Overview of BTS7960 Driver
Shops often sell the BTS7960 module as IBT-2. This powerful board drives one DC load, for example a linear actuator or a big gear motor. A linear actuator pulls a big current when it starts and again when it hits its end stop. The BTS7960 handles this easily, and it loses almost no voltage. That is why it replaces the old L298N on this page.
BTS7960 Driver Pinout

On the board, two BTS7960B chips work as the two halves of one H-bridge. So one module drives one linear actuator, in both directions. The module has an 8-pin header for the control signals and a 4-way screw terminal for the power.
The header pins:
- RPWM pin: feed PWM into it to extend the actuator. Its duty cycle controls how fast the rod moves.
- LPWM pin: a PWM signal here makes the actuator retract.
- R_EN & L_EN pins: the enable pins of the two halves. Both must be HIGH for the driver to work. Both LOW turn the outputs off.
- R_IS & L_IS pins: current-sense outputs. We do not use them, so leave them free.
- VCC pin: the 5V supply for the small 74HC244 buffer chip on the board. Connect it to a 5V pin of the Raspberry Pi.
- GND pin: the ground (0V).
The screw terminal:
- B+ & B- terminals: the actuator power goes in here, from 5.5V to 27V. We use a 12V power adapter.
- M+ & M- terminals: the two wires of the linear actuator go here.
Some good facts about this driver:
- It can take a current of up to 43A for a short peak, far more than any small actuator needs.
- The control inputs accept 3.3V to 5V signals, so the 3.3V GPIO pins of the Raspberry Pi drive them directly.
- The inputs have pull-down resistors, and R_IS / L_IS stay free, so no 5V can reach a GPIO pin.
To control one linear actuator, we need four pins of the Raspberry Pi: two PWM pins and two enable pins.
How To Control Linear Actuator
We will be studying the technique of utilizing the BTS7960 driver to manage a Linear Actuator.
Wiring Diagram

This image is created using Fritzing. Click to enlarge image
To simplify and organize your wiring setup, we recommend using a Screw Terminal Block Shield for Raspberry Pi. This shield ensures more secure and manageable connections, as shown below:

| BTS7960 Pin | Raspberry Pi Pin | Physical Pin |
|---|---|---|
| RPWM | GPIO19 | pin 35 |
| LPWM | GPIO13 | pin 33 |
| R_EN | GPIO5 | pin 29 |
| L_EN | GPIO6 | pin 31 |
| VCC | 5V | pin 4 |
| GND | GND | any GND pin |
| R_IS and L_IS | not connected | - |
The power side:
- BTS7960 B+ to the + terminal of the DC power jack (12V power adapter)
- BTS7960 B- to the - terminal of the DC power jack
- BTS7960 M+ and M- to the two wires of the linear actuator
WARNING
Never connect B+, B-, M+ or M- to the Raspberry Pi header. They carry 12V, and the GPIO pins accept only 3.3V.
How To Make Linear Actuator Expand/Retract
Both enable pins, R_EN and L_EN, must be HIGH before anything moves. Then the direction of the linear actuator depends on which PWM pin gets the signal. The table below shows the states.
| RPWM pin | LPWM pin | Direction |
|---|---|---|
| 0 | 0 | Linear Actuator stops |
| PWM | 0 | Linear Actuator extends |
| 0 | PWM | Linear Actuator retracts |
- Increase the length of the Linear Actuator (full speed).
- Reverse the Linear Actuator (full speed).
※ NOTE THAT:
- Before you start PWM on one pin, set the other one (RPWM or LPWM) to 0. The two pins must never run PWM together.
- If the actuator retracts when you expect it to extend, swap its two wires on M+ and M-, or swap RPWM and LPWM in the code.
How To Stop Linear Actuator from Extending or Retracting
The linear actuator will cease extending/retracting when it reaches the limit. It is also possible to programmatically halt its extension/retraction before it reaches the limit.
There are two methods to halt linear actuator:
- Put a 0% duty cycle on RPWM and LPWM, but keep R_EN and L_EN HIGH. This brakes the actuator, and it stops at once.
- Drive both enable pins (R_EN and L_EN) LOW. With the outputs off, the actuator coasts to a stop.
How To Control the Speed of Linear Actuator via BTS7960 Driver
The speed control is already built into the direction pins. Instead of a duty cycle of 100, we give RPWM (to extend) or LPWM (to retract) a smaller duty cycle. This can be done by:
- Making a PWM object on the RPWM pin and on the LPWM pin
- Changing the duty cycle with the pwm.ChangeDutyCycle() function. The BTS7960 copies this ON/OFF pattern to the actuator with the full 12V.
The speed is a value ranging from 0 to 100. When the speed is 0, the linear actuator will cease movement. At a speed of 100, the linear actuator will extend/retract at its maximum rate.
Raspberry Pi Example Code
The code below:
- Controls the linear actuator to its fullest extent at maximum speed
- Controls the linear actuator to its minimum extent at maximum speed
Detailed Instructions
- Make sure you have Raspbian or any other Raspberry Pi compatible operating system installed on your Pi.
- Make sure your Raspberry Pi is connected to the same local network as your PC.
- Make sure your Raspberry Pi is connected to the internet if you need to install some libraries.
- If this is the first time you use Raspberry Pi, See how to set up the Raspberry Pi
- Connect your PC to the Raspberry Pi via SSH using the built-in SSH client on Linux and macOS or PuTTY on Windows. See to how connect your PC to Raspberry Pi via SSH.
- Make sure you have the RPi.GPIO library installed. If not, install it using the following command:
- Create a Python script file bts7960_actuator.py and add the following code:
- Plug the 12V power adapter into the DC power jack.
- Save the file and run the Python script by executing the following command in the terminal:
The script runs in an infinite loop continuously until you press Ctrl + C in the terminal.
You should observe:
- The linear actuator extending until it reaches the limit, then halting.
- The linear actuator staying in that position for a certain amount of time.
- The linear actuator retracting until it reaches the limit, then halting.
- The linear actuator staying in that position for a certain amount of time.
- This cycle repeating itself.