Raspberry Pi Pico - Actuator with Feedback

In a previous tutorial, we learned about how to use Raspberry Pi Pico with a linear actuator without feedback. In this tutorial, we will learn how to use Raspberry Pi Pico with the linear actuator with feedback. The feedback signal from the actuator tells provides its position during movement, which helps us control where it goes. We will examine the following details:

Hardware Preparation

1×Raspberry Pi Pico W
1×Raspberry Pi Pico Alternatively,
1×Micro USB Cable
1×12V Linear Actuator with Feedback
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 Feedback Linear Actuator

A feedback linear actuator is a device that moves in a straight line and includes a system to check and adjust its position. It uses a potentiometer that sends out a voltage signal relating to where the actuator is positioned.

Feedback Linear Actuator Pinout

A feedback linear actuator comes with five wires.

  • Positive Wire: This wire uses a high voltage (12V, 24V, 48V) to control the linear actuator.
  • Negative Wire: This wire uses a high voltage (12V, 24V, 48V) to control the linear actuator.
  • 5V Wire: Connect this wire to the feedback potentiometer at 5V or 3.3V.
  • GND Wire: Connect this wire to the feedback potentiometer at the ground (GND).
  • Potentiometer Wire: Also called the feedback or output wire, this wire changes its voltage value according to the stroke position.
Feedback Linear Actuator Pinout

How It Works

When we apply a high voltage to the positive and negative wires, the actuator will either stretch out or pull back. To explain this, if we connect:

  • Connect 12V (or 24V, 48V) to the positive wire and GND to the negative wire: the actuator will extend at its highest speed until it fully extends.
  • Connect 12V (or 24V, 48V) to the negative wire and GND to the positive wire: the actuator will retract at its highest speed until it fully retracts.
  • If the actuator's power is switched off (by connecting GND to both its wires) while it is extending or retracting, it will stop moving.

※ NOTE THAT:

  • The voltage required to operate the actuator depends on its specific features. Look at the datasheet or manual to determine the correct voltage.
  • The actuator can keep its position without electricity, even when supporting a weight.

The voltage in the potentiometer's wire changes when the actuator moves. By measuring this voltage, we can determine the position of the stroke.

BTS7960 Driver

The Pico moves the actuator through a BTS7960 driver module (IBT-2). Inside it, two BTS7960B half-bridge chips make one full H-bridge, which is just right for one actuator. Its strong outputs handle the big current that an actuator pulls at each end of its stroke, and they waste almost no voltage, so the actuator gets nearly the full 12V.

BTS7960 Driver Pinout
  • RPWM: PWM or HIGH here = the actuator extends.
  • LPWM: PWM or HIGH here = the actuator retracts. RPWM and LPWM both at 0 = brake.
  • R_EN and L_EN: the driver does nothing until the Pico sets both of these enable pins HIGH.
  • VCC and GND: VCC takes 5V for the buffer chip on the board. The inputs are happy with the 3.3V signals of the Pico.
  • B+ and B-: the 12V actuator supply (5.5V to 27V is allowed).
  • M+ and M-: the positive and negative wires of the actuator.
  • R_IS and L_IS: not used.

Wiring Diagram

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

This image is created using Fritzing. Click to enlarge image

Part Pin Raspberry Pi Pico Pin
BTS7960 RPWM GP16 (pin 21)
BTS7960 LPWM GP17 (pin 22)
BTS7960 R_EN GP19 (pin 25)
BTS7960 L_EN GP18 (pin 24)
BTS7960 VCC VBUS (pin 40)
BTS7960 GND GND
Actuator potentiometer wire GP26 (pin 31)
Actuator 5V wire 3V3(OUT) (pin 36)
Actuator GND wire GND
  • The actuator's 5V wire goes to 3.3V, not 5V. Then the potentiometer wire stays below 3.3V, which is safe for the analog pin of the Pico.
  • Connect the actuator's motor wires to the BTS7960: positive into M+, negative into M-.
  • B+ and B- go to the + and - terminals of the DC power jack (12V power adapter).
  • VBUS gives 5V only when the Pico is powered by USB.

How to control extend/retract a linear actuator

Learn about the Raspberry Pi Pico Actuator by clicking here.

How to find the position of the linear actuator

Here's how to locate the stroke position on a linear actuator:

Calibration

  • Use a ruler to find out how long the actuator moves or look at the datasheet for details.
  • Plug the 12V power adapter into the DC power jack.
  • To see the values when the linear actuator is fully open or closed, run this code.
""" 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-with-feedback """ from machine import Pin, ADC 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 POTENTIOMETER_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the potentiometer of the actuator # Initialize digital pins as outputs (full speed only, so no PWM is needed) rpwm = Pin(RPWM_PIN, Pin.OUT, value=0) lpwm = Pin(LPWM_PIN, Pin.OUT, value=0) r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) # Initialize the potentiometer as an ADC pin potentiometer = ADC(Pin(POTENTIOMETER_PIN)) # Set R_EN and L_EN to HIGH to enable the motor driver r_en.value(1) l_en.value(1) # Main loop to extend and retract the actuator with feedback while True: # Extend the actuator lpwm.value(0) rpwm.value(1) time.sleep(20) # Wait for 20 seconds (20000 ms) # Read the potentiometer value after fully extending POTENTIOMETER_MAX = potentiometer.read_u16() print("POTENTIOMETER_MAX =", POTENTIOMETER_MAX) # Retract the actuator rpwm.value(0) lpwm.value(1) time.sleep(20) # Wait for 20 seconds (20000 ms) # Read the potentiometer value after fully retracting POTENTIOMETER_MIN = potentiometer.read_u16() print("POTENTIOMETER_MIN =", POTENTIOMETER_MIN)
  • You will see the record in the Shell at the bottom of Thonny like in the example below.
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot POTENTIOMETER_MAX = 987 POTENTIOMETER_MIN = 13
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡
  • Write these values.
  • If the minimum value is more than the maximum value, exchange RPWM_PIN with LPWM_PIN.
  • Change three values in the following code.

Raspberry Pi Pico code that calculate the position of 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-with-feedback """ from machine import Pin, ADC 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 POTENTIOMETER_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the potentiometer of the actuator # Define constants STROKE_LENGTH = 102 # Stroke length in millimeters POTENTIOMETER_MAX = 987 # Max potentiometer value POTENTIOMETER_MIN = 13 # Min potentiometer value # Initialize digital pins as outputs rpwm = Pin(RPWM_PIN, Pin.OUT, value=0) lpwm = Pin(LPWM_PIN, Pin.OUT, value=0) r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) # Initialize the potentiometer as an ADC pin potentiometer = ADC(Pin(POTENTIOMETER_PIN)) # Set R_EN and L_EN to HIGH to enable the motor driver r_en.value(1) l_en.value(1) def map_value(x, in_min, in_max, out_min, out_max): # Map a value from one range to another. return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) # Main loop to extend the actuator and read the stroke position while True: # Extend the actuator lpwm.value(0) rpwm.value(1) # Read the potentiometer value potentiometer_value = potentiometer.read_u16() # Map the potentiometer value to stroke position in millimeters stroke_pos = map_value(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH) # Print the stroke position print("The stroke's position =", stroke_pos, "mm") time.sleep(1) # Wait for 1 second before the next reading
  • Modify the three altered values in the code
  • Upload the code to the Raspberry Pi Pico
  • Check out the results in the Shell at the bottom of Thonny
Shell x
>>> %Run -c $EDITOR_CONTENT
MPY: soft reboot The stroke's position = 2 mm The stroke's position = 35 mm The stroke's position = 43 mm The stroke's position = 60 mm The stroke's position = 68 mm The stroke's position = 79 mm The stroke's position = 83 mm The stroke's position = 96 mm The stroke's position = 100 mm
MicroPython (Raspberry Pi Pico) • Board CDC @ COM29 ≡

How to control a linear actuator to a specific position

""" 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-with-feedback """ from machine import Pin, ADC 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 POTENTIOMETER_PIN = 26 # The Raspberry Pi Pico pin GP26 (ADC0) connected to the potentiometer of the actuator # Define constants STROKE_LENGTH = 102 # Stroke length in millimeters POTENTIOMETER_MAX = 987 # Max potentiometer value POTENTIOMETER_MIN = 13 # Min potentiometer value TOLERANCE = 5 # Tolerance in millimeters targetPosition_mm = 50 # Target position in millimeters # Initialize digital pins as outputs rpwm = Pin(RPWM_PIN, Pin.OUT, value=0) lpwm = Pin(LPWM_PIN, Pin.OUT, value=0) r_en = Pin(R_EN_PIN, Pin.OUT) l_en = Pin(L_EN_PIN, Pin.OUT) # Initialize the potentiometer as an ADC pin potentiometer = ADC(Pin(POTENTIOMETER_PIN)) # Set R_EN and L_EN to HIGH to enable the motor driver r_en.value(1) l_en.value(1) def map_value(x, in_min, in_max, out_min, out_max): # Map a value from one range to another. return int((x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min) def ACTUATOR_extend(): lpwm.value(0) rpwm.value(1) def ACTUATOR_retract(): rpwm.value(0) lpwm.value(1) def ACTUATOR_stop(): # Both 0 while R_EN and L_EN are HIGH = brake rpwm.value(0) lpwm.value(0) # Main loop to control the actuator while True: # Read the potentiometer value potentiometer_value = potentiometer.read_u16() # Map the potentiometer value to stroke position in millimeters stroke_pos = map_value(potentiometer_value, POTENTIOMETER_MIN, POTENTIOMETER_MAX, 0, STROKE_LENGTH) # Print the stroke position print("The stroke's position =", stroke_pos, "mm") # Control the actuator based on the target position and tolerance if stroke_pos < (targetPosition_mm - TOLERANCE): ACTUATOR_extend() elif stroke_pos > (targetPosition_mm + TOLERANCE): ACTUATOR_retract() else: ACTUATOR_stop() time.sleep(0.1) # Small delay to avoid excessive CPU usage

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!