Raspberry Pi Pico - Actuator

This tutorial instructs you how to use Raspberry Pi Pico to control the linear actuator. In detail, we will discover:

This guide focuses on linear actuators that do not have feedback. If you need information about linear actuators with feedback, please go to this guide: Raspberry Pi Pico - Actuator with Feedback.

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×Linear Actuator
1×BTS7960 Motor Driver Module
1×12V Power Adapter
1×DC Power Jack
1×Breadboard
1×Jumper Wires
1×Recommended: Screw Terminal Expansion Board for Raspberry Pi Pico

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.

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

How It Works

When buying a linear actuator, it is important to know its operating voltage. For example, think about a linear actuator that uses 12 volts.

When you link the 12V linear actuator with a 12V power source:

  • Attach 12V to the positive wire and GND to the negative wire: this makes the linear actuator extend quickly until it reaches its final point.
  • Attach 12V to the negative wire and GND to the positive wire: this makes the linear actuator retract quickly until it reaches its final point.

When you disconnect the power from the actuator by linking the positive and negative wires to the ground, the actuator will stop moving.

※ NOTE THAT:

DC motors, servo motors, and stepper motors without gears cannot hold their position if there is no power and they have a load. But an actuator can hold its position without power, even with a load.

If we use less than 12V for linear actuators, they still function but at a slower speed. This indicates that altering the power supply voltage can vary the speed of the linear actuator. However, accurately adjusting the voltage is challenging, so this approach is not often used. Typically, the voltage remains steady and the speed of the linear actuator is managed with a PWM signal. When the PWM duty cycle increases, the linear actuator moves quicker.

How to control linear actuator

How to control a linear actuator using Raspberry Pi Pico

To operate a linear actuator involves:

  • Makes the linear actuator extend as quickly as possible.
  • Makes the linear actuator retract as quickly as possible.
  • (optional) Changes how fast it extends and retracts.

The Raspberry Pi Pico can create a control signal for a linear actuator. However, this signal has low voltage and current, so it cannot control the actuator directly. We must use a hardware driver between the Raspberry Pi Pico and the linear actuator. The driver has two roles:

  • Boost the strength and voltage of the control signal from Raspberry Pi Pico.
  • Use an additional signal from Raspberry Pi Pico to switch the polarity of the power supply, which determines the direction.

※ NOTE THAT:

  • This guide is suitable for all linear actuators. Here, we show how it works using a 12V linear actuator as an example.
  • A Pico pin gives only 3.3V and a very small current. Even a 5V linear actuator needs much more, so always put a driver between the Pico and the actuator.

This guide is based on the BTS7960 driver. Other driver boards can also manage linear actuators.

※ NOTE THAT:

You can use relays as a driver too, but controlling one linear actuator (to extend and retract it) requires four relays.

Overview of BTS7960 Driver

This guide will show you how to use the BTS7960 Driver (also sold as IBT-2) to control a linear actuator. It is a big, strong board with a heat sink, made for loads that pull many amps. A linear actuator pulls a high current at the start and again when it reaches an end, so a strong driver is a good choice. The BTS7960 also loses almost no voltage, unlike the L298N that this guide used before.

BTS7960 Driver Pinout

BTS7960 Driver Pinout

The board carries two BTS7960B chips. Each chip is one half of an H-bridge, so together they drive one linear actuator in both directions. The control pins sit on an 8-pin header, and the power goes through a 4-way screw terminal.

The header pins:

  • RPWM pin: PWM in this pin makes the linear actuator extend. A bigger duty cycle means a faster move.
  • LPWM pin: PWM in this pin makes the linear actuator retract.
  • R_EN & L_EN pins: the on/off switches of the driver. Drive both HIGH so the actuator can move, or LOW to shut the outputs down.
  • R_IS & L_IS pins: current-sense outputs. They are not used here, so leave them free.
  • VCC pin: 5V for the small 74HC244 buffer chip on the board. On the Pico, we take it from VBUS, which gives 5V when the Pico runs on USB.
  • GND pin: connect it to GND (0 volts).

The screw terminal:

  • B+ & B- terminals: the actuator supply, from 5.5V to 27V. We use a 12V power adapter.
  • M+ & M- terminals: connect these to the two wires of the linear actuator.

Useful facts:

  • The board can handle up to 43A for a short peak.
  • The control inputs accept 3.3V to 5V signals, so the 3.3V pins of the Pico work directly.
  • The inputs have pull-down resistors, so no 5V flows back into the Pico.

To run one linear actuator, we need four pins from the Raspberry Pi Pico: RPWM, LPWM, R_EN and L_EN.

How To Control Linear Actuator

Let's see how a Raspberry Pi Pico and a BTS7960 driver work together to move a linear actuator.

Wiring Diagram

The wiring diagram between Raspberry Pi and Pico Linear Actuator BTS7960 Driver

This image is created using Fritzing. Click to enlarge image

BTS7960 Pin Raspberry Pi Pico Pin
RPWM GP16 (pin 21)
LPWM GP17 (pin 22)
R_EN GP19 (pin 25)
L_EN GP18 (pin 24)
VCC VBUS (pin 40)
GND GND
R_IS, L_IS not connected
  • B+ and B- go to the + and - terminals of the DC power jack (12V power adapter).
  • M+ and M- go to the two wires of the linear actuator.
  • Never connect B+, B-, M+ or M- to any pin of the Pico. 12V will destroy it.

How To Make Linear Actuator Expand/Retract

Begin with R_EN and L_EN at HIGH, because the driver ignores RPWM and LPWM while they are LOW. Then you choose the direction by sending PWM to RPWM or to LPWM. Below is a table that explains how to do this.

RPWM pin LPWM pin Direction
0 0 Linear Actuator stops (brake)
PWM 0 Linear Actuator extends
0 PWM Linear Actuator retracts
  • Extends the Linear Actuator at full speed
lpwm.duty_u16(0) rpwm.duty_u16(65535)
  • The linear actuator pulls back at full speed.
rpwm.duty_u16(0) lpwm.duty_u16(65535)

※ NOTE THAT:

  • Do not drive both PWM inputs at once. When RPWM gets PWM, LPWM must be 0, and the other way around.
  • If the actuator moves the wrong way, swap its two wires on M+ and M-, or swap RPWM and LPWM in the software.

How To Stop Linear Actuator from Extending or Retracting

The linear actuator automatically stops moving forward or backward when it reaches its farthest or closest point. We can also set it to stop before it gets to these points.

There are 2 ways to stop a linear actuator.

  • Sets RPWM and LPWM to 0, with R_EN and L_EN still HIGH. This is a brake: the actuator stops at once.
rpwm.duty_u16(0) lpwm.duty_u16(0)
  • Sets R_EN and L_EN to LOW. The driver turns off its outputs and the actuator coasts.
r_en.value(0) l_en.value(0)

Raspberry Pi Pico Example Code

This code does the following things:

  • Quickly extend the actuator
  • Stop the actuator
  • Quickly retract the actuator
  • Stop the actuator
""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-actuator """ from machine import Pin, PWM import time # Define pins RPWM_PIN = 16 # The Raspberry Pi Pico pin GP16 connected to the RPWM pin of BTS7960 (extend) LPWM_PIN = 17 # The Raspberry Pi Pico pin GP17 connected to the LPWM pin of BTS7960 (retract) R_EN_PIN = 19 # The Raspberry Pi Pico pin GP19 connected to the R_EN pin of BTS7960 L_EN_PIN = 18 # The Raspberry Pi Pico pin GP18 connected to the L_EN pin of BTS7960 # PWM outputs, both at 0 at the start rpwm = PWM(Pin(RPWM_PIN)) lpwm = PWM(Pin(LPWM_PIN)) rpwm.freq(1000) lpwm.freq(1000) rpwm.duty_u16(0) lpwm.duty_u16(0) # Set R_EN and L_EN to HIGH to enable the motor driver r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) r_en.value(1) l_en.value(1) # Main loop to extend and retract the actuator while True: # Extend the actuator at full speed lpwm.duty_u16(0) rpwm.duty_u16(65535) time.sleep(20) # Wait for 20 seconds, it stops by itself at the end # Retract the actuator at full speed rpwm.duty_u16(0) lpwm.duty_u16(65535) time.sleep(20) # Wait for 20 seconds, it stops by itself at the end

Detailed Instructions

Please follow these instructions step by step:

  • Ensure that Thonny IDE is installed on your computer.
  • Ensure that MicroPython firmware is installed on your Raspberry Pi Pico.
  • If this is your first time using a Raspberry Pico, refer to the Raspberry Pi Pico - Getting Started tutorial for detailed instructions.
  • Connect the Raspberry Pi Pico to the linear actuator via BTS7960 module according to the provided diagram.
  • Plug the 12V power adapter into the DC power jack.
  • Connect the Raspberry Pi Pico to your computer using a USB cable.
  • Launch the Thonny IDE on your computer.
  • On Thonny IDE, select MicroPython (Raspberry Pi Pico) Interpreter by navigating to Tools Options.
  • In the Interpreter tab, select MicroPython (Raspberry Pi Pico) from the drop-down menu.
  • Ensure the correct port is selected. Thonny IDE should automatically detect the port, but you may need to select it manually (e.g., COM3 on Windows or /dev/ttyACM0 on Linux).
  • Copy the above code and paste it to the Thonny IDE's editor.
  • Save the script to your Raspberry Pi Pico by:
    • Click the Save button, or use Ctrl+S keys.
    • In the save dialog, you will see two sections: This computer and Raspberry Pi Pico. Select Raspberry Pi Pico
    • Save the file as main.py
  • Click the green Run button (or press F5) to run the script. The script will execute.
  • Check out the actuator state. It will:
    • The linear actuator extends and stops when it reaches its maximum point.
    • The linear actuator maintains its position for some time.
    • The linear actuator retracts and stops when it reaches its end point.
    • The linear actuator maintains its position for a certain duration.
    • This pattern keeps happening again and again.

How To Control the Speed of Linear Actuator via BTS7960 Driver

To change the speed of the linear actuator easily, give RPWM (to extend) or LPWM (to retract) a duty cycle below the maximum, instead of full power. The below MicroPython script moves the actuator at about half speed:

""" This Raspberry Pi Pico MicroPython code was developed by newbiely.com This Raspberry Pi Pico code is made available for public use without any restriction For comprehensive instructions and wiring diagrams, please visit: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-actuator """ from machine import Pin, PWM import time # Define pins RPWM_PIN = 16 # The Raspberry Pi Pico pin GP16 connected to the RPWM pin of BTS7960 (extend) LPWM_PIN = 17 # The Raspberry Pi Pico pin GP17 connected to the LPWM pin of BTS7960 (retract) R_EN_PIN = 19 # The Raspberry Pi Pico pin GP19 connected to the R_EN pin of BTS7960 L_EN_PIN = 18 # The Raspberry Pi Pico pin GP18 connected to the L_EN pin of BTS7960 # PWM outputs, both at 0 at the start rpwm = PWM(Pin(RPWM_PIN)) lpwm = PWM(Pin(LPWM_PIN)) rpwm.freq(1000) # Set the PWM frequency to 1kHz lpwm.freq(1000) rpwm.duty_u16(0) lpwm.duty_u16(0) # Set R_EN and L_EN to HIGH to enable the motor driver r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) r_en.value(1) l_en.value(1) def to_duty(speed): # Convert the speed (0-255) to duty cycle (0-65535) return int((speed / 255) * 65535) # Example usage: speed = 128 # Set desired speed (0-255) # Main loop to extend and retract the actuator while True: # Extend the actuator at the chosen speed lpwm.duty_u16(0) rpwm.duty_u16(to_duty(speed)) time.sleep(20) # Wait for 20 seconds (20000 ms) # Retract the actuator at the chosen speed rpwm.duty_u16(0) lpwm.duty_u16(to_duty(speed)) time.sleep(20) # Wait for 20 seconds (20000 ms)

Video Tutorial

※ 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!