Continuous Servo using Raspberry Pi Pico

Hi! I am new to coding and am using a Raspberry Pi Pico to control a continuous servo motor. Using the guide from Core Electronics on YouTube I have been able to wire the servo and make it move with the code. Does anyone know how I can have the servo turn on for 5 seconds (clockwise) and then stop. Eventually I want to add a button to the circuit to carry this out when it is pressed.

Hi Erin, welcome to the forums!

Assuming you are following this guide:

The easiest way to do this would be to change the while loop to a for loop.

the “while True:” line will repeat the code inside it forever. If you add a second variable below your “delay_ms” variable that looks like

delay_seconds = 5

you can use this to determine how long this loop will run for. Then you would change the while loop to a for loop so that it runs for a specific time like this.

for x in range((1000 * delay_seconds)/delay_ms):

That for loop will take into account the approx. time each iteration of the loop is taking to complete and is calculating how many times the loop would need to repeat for 5 seconds to pass. then it runs the loop for this number of iterations.

As for activating this with a button, this is a super common task on a Pico and Raspberry Pi has some great documentation for how to achieve this here.

https://projects.raspberrypi.org/en/projects/introduction-to-the-pico/10

If you need me to explain this in more detail let me know! Feel free to share your code if you need help with specific modifications

Hope this helps. :smiley:
Sam