Let’s See Your AI Creations! Submit your project now Here! and get $5 off your next purchase! 🔥
[Holiday Announcement] We will be closed for the Raya holiday from March 29 to April 6. Orders placed after March 28 at 12 PM (GMT +8) will be processed starting April 7. Thank you for your understanding.

Micro Servo Motor

This guide will show you how to control the micro servo motor connected to your Robo Pico.

The figure below shows the GPIO for each of the Servo Ports.

 

1. While your Robo Pico is connected to your computer, open your editor software (for example Thonny). Select the code.py file in your drive.


2. Connect a micro servo motor to one of the servo ports. For example, a SG90 micro servo is connected to the S1 port on Robo Pico.

3. Copy and paste the following code to your editor.

Copy
"""
DESCRIPTION:
This example code will uses: Robo Pico and Raspberry Pi Pico / Pico W
to control the servo on the Robo Pico servo port.
AUTHOR : Cytron Technologies Sdn Bhd
WEBSITE : www.cytron.io
EMAIL : support@cytron.io
REFERENCE:
Tutorial link:
https://www.cytron.io/tutorial/get-started-robo-pico-circuitpython-micro-servo-motor
"""
import time
import board
import digitalio
import pwmio
from adafruit_motor import servo
# create a PWMOut object on the control pin.
pwm1 = pwmio.PWMOut(board.GP12, duty_cycle=0, frequency=50)
pwm2 = pwmio.PWMOut(board.GP13, duty_cycle=0, frequency=50)
pwm3 = pwmio.PWMOut(board.GP14, duty_cycle=0, frequency=50)
pwm4 = pwmio.PWMOut(board.GP15, duty_cycle=0, frequency=50)
# You might need to calibrate the min_pulse (pulse at 0 degrees) and max_pulse (pulse at 180 degrees) to get an accurate servo angle.
# The pulse range is 750 - 2250 by default (if not defined).
# Initialize Servo objects.
servo1 = servo.Servo(pwm1, min_pulse=580, max_pulse=2700)
servo2 = servo.Servo(pwm2, min_pulse=580, max_pulse=2700)
servo3 = servo.Servo(pwm3, min_pulse=580, max_pulse=2700)
servo4 = servo.Servo(pwm4, min_pulse=580, max_pulse=2700)
angle = 0
while True:
#Set servo angle
angle = 90
# Update servo angles.
servo1.angle = servo2.angle = servo3.angle = servo4.angle = angle
# Change the angle every 1 second.
time.sleep(1)
#Set servo angle
angle = 45
# Update servo angles.
servo1.angle = servo2.angle = servo3.angle = servo4.angle = angle
# Change the angle every 1 second.
time.sleep(1)

 

4. Flash the codes to your Raspberry Pi Pico / Pico W. And your CIRCUITPY drive should look like this :

Library file(s) required:
    adafruit_motor (file) : motor.mpy, servo.mpy, stepper.mpy

If you don't have this file in your computer, download the lib bundle here and search for this file inside the "lib" folder.

 

5. You may also try to flash this code to your Raspberry Pi Pico / Pico W and observe what will happen to your Raspberry Pi Pico / Pico W.

Note: The following code will program all 4 servo ports, but still applicable if you have only 1 micro servo motor connected to one of the ports.

Copy
"""
DESCRIPTION:
This example code will uses: Robo Pico and Raspberry Pi Pico / Pico W
to control the servo on the Robo Pico servo port.
AUTHOR : Cytron Technologies Sdn Bhd
WEBSITE : www.cytron.io
EMAIL : support@cytron.io
REFERENCE:
Tutorial link:
https://www.cytron.io/tutorial/get-started-robo-pico-circuitpython-micro-servo-motor
"""
import time
import board
import digitalio
import pwmio
from adafruit_motor import servo
# create a PWMOut object on the control pin.
pwm1 = pwmio.PWMOut(board.GP12, duty_cycle=0, frequency=50)
pwm2 = pwmio.PWMOut(board.GP13, duty_cycle=0, frequency=50)
pwm3 = pwmio.PWMOut(board.GP14, duty_cycle=0, frequency=50)
pwm4 = pwmio.PWMOut(board.GP15, duty_cycle=0, frequency=50)
# You might need to calibrate the min_pulse (pulse at 0 degrees) and max_pulse (pulse at 180 degrees) to get an accurate servo angle.
# The pulse range is 750 - 2250 by default (if not defined).
# Initialize Servo objects.
servo1 = servo.Servo(pwm1, min_pulse=580, max_pulse=2700)
servo2 = servo.Servo(pwm2, min_pulse=580, max_pulse=2700)
servo3 = servo.Servo(pwm3, min_pulse=580, max_pulse=2700)
servo4 = servo.Servo(pwm4, min_pulse=580, max_pulse=2700)
angle = 0
add_angle = True
while True:
# Condition to add angle
if angle <= 0:
add_angle = True
if angle >= 180:
add_angle = False
# Add angle increment or decrement
if add_angle:
angle += 5
else:
angle -= 5
# Update servo angles.
servo1.angle = servo2.angle = servo3.angle = servo4.angle = angle
print("turn servo", angle)
# Change the angle every 0.5 second.
time.sleep(0.5)

 

DC Motor
Read Sensor Value

Hardware Components

Robo Pico

S$21.29 S$21.29

x 1 unit(s)