Pico with Servos project - relay contacts control servo behaviour

This topic is being posted on behalf of a helpdesk user, I’ll summarise the project requirements and then propose a solution

Project requirements

A relay connects to a Raspberry Pi Pico, bridging a GPIO pin and a GND pin.

When that relay is open, 4x servos should go to eg. 90°
When that relay is closed, 4x servos should go to eg. 0°

The servos should move smoothly.

Using the following modules (sundry connecting hardware not included in this list)

[ CE08677 ] PiicoDev Expansion Board for Pico (Non-Recharging)
[ CE09181 ] PiicoDev Servo Driver (4 Channel)


A proposed solution

First, i would make sure I’m familiar with how to run a script on the Raspberry Pi Pico. Importantly, how to run the script as main.py so the Pico does not need to be connected to a computer.
All this is covered in Chapter 1 of the Raspberry Pi Pico Workshop

I’d connect the Servo Driver to the Pico in the same was as the PiicoDev Servo Driver Guide

Here is the code I would use to drive the servos as described in the project outline.
The relay contacts are assumed to be connected between GP0 and any GND pin.

# Drive an angular servo with generally safe-to-use default properties
from PiicoDev_Unified import sleep_ms
from PiicoDev_Servo import PiicoDev_Servo, PiicoDev_Servo_Driver
from machine import Pin

button = Pin(0, Pin.PULL_UP) # connect the relay between this GPIO and GND

# Initialise the Servo Driver Module
controller = PiicoDev_Servo_Driver() 

# Defined the 4 servo slots as 1,2,3 and 4 respectively
servoOne   = PiicoDev_Servo(controller, 1)
servoTwo   = PiicoDev_Servo(controller, 2)
servoThree = PiicoDev_Servo(controller, 3)
servoFour  = PiicoDev_Servo(controller, 4)

increment = 5

# This loop will run forever and wait until it detects button a being pressed
while True:
    # decide which direction to move the servos
    if button.value() == True:
        increment = 5
    else:
        increment = -5
        
    angle += increment
    # make sure angle does not go beyond 0-90
    angle = min(angle,90)
    angle = max(angle, 0)
        
        
    servoOne.angle = angle
    servoTwo.angle = angle
    servoThree.angle = angle
    servoFour.angle = angle
    sleep_ms(20)
    

1 Like

Hi Michael
There would be a pretty easy solution with Arduino. Example using 5V version.
Connect Relay NO to 5V via 10kΩ resistor
Connect Relay NO pin to Gnd vis 10kΩ resistor
Connect Relay NO pin to Arduino A0

Arduino:
Load Servo library
Define A0 as analog input
Define output pin for servo drive
Read analog input
Map ADC readings to 0 - 180
Write new ADC readings to servo output

Connect all 4 servo data inputs to servo output pin.

Relay open. A0 at 2.5V remapped ADC should be 90º.
Relay closed. A0 at 0V remapped ADC should be 0º.

Should be able to simply use the example servo sketch suitably modified if required.
Cheers Bob
EDIT. Add:
Connect relay pin NC to Gnd.

4 Likes

Hi Michael (Leader),
My project is to have four level crossing gates on a model railway operate when a train approaches using a Raspberry Pi Pico H board, a Raspberry Pi Pico Expansion Board, a PiicoDev 4 Channel Servo Driver connected to four FS90 Micro Servos operating the gates, a relay which when activated by the track circuits will close the circuit between the 0 pin and GND which will then operate the the servos/gates.
On my computer I have downloaded Thonny and uploaded the Micropython Servo Library.
I’ve also download the code that you kindly wrote for me to operate the the servos but when trying to run the code I keep getting error messages in the Shell and can’t get the code to run or download to the Raspberry Pi Pico.
I’ve included a screenshot of the Thonny window, the code and an error message.
Being completely new to this programming…I’m lost!
With the code I’d also like to be able to control the speed/length of time the gates travel from open to closed (approximately 90 deg) and be able to set different angles for opening and closing each gate (the servos are not at right angles to each other) and pause the time between each gate movement.
Any help would be greatly appreciated.
Regards,
Michael268269

2 Likes

Hi @Michael268269 - if you’re completely new to programming there’s some upskilling that would be prudent. If you haven’t already had a look at the Pico Workshop and the Servo Driver Guides those will be a good place to start.

The behaviour you describe is not too difficult to implement, but it will be easier for us to help if you follow these guides and build some intuition with the hardware and software - since we won’t be able to deliver a complete solution that meets all your needs. You’ll likely want to do some tuning yourself.

The error you’re seeing is expected because you have not installed the PiicoDev_Servo.py file in the way that the guide prescribes. You’ve installed a different servo driver file, which is fine - it just may not be appropriate for the hardware in your project.

Can you provide a photo of how you’ve got everything hooked up? Even if just for prototyping, that will be a good start.

2 Likes