Raspberry Pi - Actuator

This tutorial instructs you how to use Raspberry Pi to control Linear Actuator. In detail, we will learn:

There are two types of linear actuators:

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

1×Raspberry Pi 5
1×Linear Actuator
1×BTS7960 Motor Driver Module
1×12V Power Adapter
1×DC Power Jack
1×Jumper Wires
1×Recommended: Screw Terminal Block Shield for Raspberry Pi
1×Recommended: Raspberry Pi Prototyping Base Plate & Breadboard Kit

Or you can buy the following kits:

1×DIYables Sensor Kit (18 sensors/displays)
Disclosure: Some of the links provided in this section are Amazon affiliate links. We may receive a commission for any purchases made through these links at no additional cost to you.
Additionally, some of these links are for products from our own brand, DIYables .

Overview of Linear Actuator

Linear Actuator Extend Retract

Linear Actuator Pinout

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

Linear Actuator pinout

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 linear actuator

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

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

The wiring diagram between Raspberry Pi and Linear Actuator BTS7960 Driver

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:

Raspberry Pi Screw Terminal Block Shield
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).
lpwm.ChangeDutyCycle(0) rpwm.ChangeDutyCycle(100)
  • Reverse the Linear Actuator (full speed).
rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(100)

※ 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.
rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(0)
  • Drive both enable pins (R_EN and L_EN) LOW. With the outputs off, the actuator coasts to a stop.
GPIO.output(R_EN_PIN, GPIO.LOW) GPIO.output(L_EN_PIN, GPIO.LOW)

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.
lpwm.ChangeDutyCycle(0) rpwm.ChangeDutyCycle(speed) # speed is a value from 0 to 100

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:
sudo apt-get update sudo apt-get install python3-rpi.gpio
  • Create a Python script file bts7960_actuator.py and add the following code:
""" This Raspberry Pi code was developed by newbiely.com This Raspberry Pi code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pi/raspberry-pi-actuator """ import RPi.GPIO as GPIO import time # BTS7960 pins (BCM numbering), actuator on M+ / M- RPWM_PIN = 19 # GPIO19 (pin 35) -> BTS7960 RPWM, extend LPWM_PIN = 13 # GPIO13 (pin 33) -> BTS7960 LPWM, retract R_EN_PIN = 5 # GPIO5 (pin 29) -> BTS7960 R_EN L_EN_PIN = 6 # GPIO6 (pin 31) -> BTS7960 L_EN PWM_FREQ = 1000 # PWM frequency in Hz # Setup GPIO.setmode(GPIO.BCM) for pin in (RPWM_PIN, LPWM_PIN, R_EN_PIN, L_EN_PIN): GPIO.setup(pin, GPIO.OUT, initial=GPIO.LOW) rpwm = GPIO.PWM(RPWM_PIN, PWM_FREQ) lpwm = GPIO.PWM(LPWM_PIN, PWM_FREQ) rpwm.start(0) lpwm.start(0) # Turn the driver on: R_EN and L_EN HIGH GPIO.output(R_EN_PIN, GPIO.HIGH) GPIO.output(L_EN_PIN, GPIO.HIGH) # Main loop try: while True: # Extend the actuator at full speed lpwm.ChangeDutyCycle(0) rpwm.ChangeDutyCycle(100) time.sleep(20) # Actuator will stop extending automatically when reaching the limit # Retract the actuator at full speed rpwm.ChangeDutyCycle(0) lpwm.ChangeDutyCycle(100) time.sleep(20) # Actuator will stop retracting automatically when reaching the limit except KeyboardInterrupt: pass finally: # Stop PWM, turn the driver off and cleanup GPIO on program exit rpwm.stop() lpwm.stop() GPIO.output(R_EN_PIN, GPIO.LOW) GPIO.output(L_EN_PIN, GPIO.LOW) GPIO.cleanup()
  • 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:
python3 bts7960_actuator.py

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.

Video Tutorial

Learn More

※ OUR MESSAGES

  • As freelancers, We are AVAILABLE for HIRE. See how to outsource your project to us
  • Please feel free to share the link of this tutorial. However, Please do not use our content on any other websites. We invested a lot of effort and time to create the content, please respect our work!