Raspberry Pi Pico - Actuator

by DIYables in Circuits > Raspberry Pi

9 Views, 0 Favorites, 0 Comments

Raspberry Pi Pico - Actuator

raspberry-pi-pico-actuator-cover.jpg

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-actuator

This guide shows how to control a linear actuator with a Raspberry Pi Pico using an L298N motor driver. You wire the actuator to the driver's output, drive IN1 and IN2 from the Pico to set direction, and use PWM on ENA to control speed. The guide covers linear actuators without position feedback.

Supplies

Everything below is what the build uses. The links go to the parts we make and test.

  1. Raspberry Pi Pico W
  2. Raspberry Pi Pico
  3. Micro USB Cable
  4. Linear Actuator
  5. L298N Motor Driver Module
  6. DC Power Jack
  7. Jumper Wires
  8. Breadboard
  9. DIYables Sensor Kit (18 sensors/displays)

Wiring

raspberry-pi-pico-linear-actuator-wiring-diagram.jpg

Remove all three jumpers from the L298N module before wiring anything. Connect the L298N VCC pin to the 12V power adapter (via the DC power jack) and GND to common ground. Connect the L298N 5V pin to the Pico's 5V output to power the module logic. Connect IN1 and IN2 to two GPIO pins on the Pico to control direction, and ENA to a PWM-capable GPIO to control speed. Wire the linear actuator's red (positive) and black (negative) wires to OUT1 and OUT2 on channel A.

How It Works

The Pico's GPIO signals are too weak to drive the actuator directly, so the L298N sits between them, boosting the signal and switching the polarity of the 12V supply to set direction. Setting IN1 HIGH and IN2 LOW extends the actuator; reversing them retracts it. Setting both HIGH or both LOW stops it, as does leaving ENA low. Since the actuator runs at a fixed 12V, speed is controlled not by changing voltage but by applying a PWM signal to ENA: a higher duty cycle moves the actuator faster.

Code

The example code sets IN1 and IN2 to extend the actuator quickly, waits, stops it by setting both pins LOW, retracts it by reversing the pins, then stops it again. A second version replaces the fixed HIGH on ENA with a PWM signal, letting you vary the duty cycle to slow down or speed up extension and retraction instead of only running at full speed. Upload and run the code from Thonny IDE with MicroPython installed on the Pico.

raspberry-pi-pico-actuator.py - the sketch from the tutorial, unchanged.

"""
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
import time

# Define pins
ENA_PIN = 5 # The Raspberry Pi Pico pin GP5 connected to the EN1 pin of L298N
IN1_PIN = 4 # The Raspberry Pi Pico pin GP4 connected to the IN1 pin of L298N
IN2_PIN = 3 # The Raspberry Pi Pico pin GP3 connected to the IN2 pin of L298N

# Initialize digital pins as outputs
ena = Pin(ENA_PIN, Pin.OUT)
in1 = Pin(IN1_PIN, Pin.OUT)
in2 = Pin(IN2_PIN, Pin.OUT)

# Set ENA_PIN to HIGH to enable the motor driver
ena.value(1)

# Main loop to extend and retract the actuator
while True:
# Extend the actuator
in1.value(1)
in2.value(0)

time.sleep(20) # Wait for 20 seconds (20000 ms)

# Retract the actuator
in1.value(0)
in2.value(1)

time.sleep(20) # Wait for 20 seconds (20000 ms)

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/raspberry-pico/raspberry-pi-pico-actuator