In this tutorial we are going to control an SG-90 Servo motor with raspberry pi pico board, Servo motors are used in a variety of applications such as RC cars, drones, and robot arms. They are also used in industrial applications such as CNC machines and 3D printers. Servo motors have three wires:
There are many beginners suffering from the basics of the Raspberry Pi Pico Board. So I decided to start a series of articles discussing the basics of Raspberry Pi Pico Projects. Check out our Raspberry Pi Pico Projects Tutorials.
In this tutorial, I’m going to run only one Servo Motor. because the Code is only for one Servo motor. if you want to control multiple Servos motors then you have to change the code. If you understand code you will be able to control multiple servos.
- Raspberry Pi Pico
- Servo motor
- Jumper cable
What Is Servo Motor
A servo motor is a rotary actuator that uses feedback to control its angular position. It consists of a motor, a gear train, a control circuit, and a feedback mechanism. The gear train reduces the motor’s speed while increasing its torque. The control circuit monitors the position of the motor shaft and compares it to the desired position. The feedback mechanism adjusts the current to the motor, based on the difference between the two positions.
Schematic Diagram for Servo Motor With Raspberry Pi Pico
The schematic diagram is very easy To control Servo Motor With Raspberry Pi Pico please follow the circuit diagram.
Servo Motor —> Raspberry Pi Pico
- PWM Pin —> GP0 Pin
- VCC Pin —> 3.3V
- GND Pin —> GND Pin
The Servo motor consists of three wires; red, brown, and yellow. Two wires are for VCC and ground and the third one is the data or control pin.
To program the Raspberry Pi Pico there is various development Software available (like uPyCraft IDE, Visual Studio Code, Thonny IDE, etc.) I used the Thonny IDE that supports Micropython on the Raspberry Pi Pico.
First setup Thonny IDE for Raspberry Pi Pico:- How to Set Up and Program Raspberry Pi Pico
- Open the Thonny IDE once it is successfully installed.
- Choose the “for all users” option.
- Connect the Raspberry Pi Pico board to your laptop.
- To create a new project; go to Files > New.
- Connect the Raspberry Pi Pico board to the laptop using a USB cable.
- Click on the ‘MicoPython(Raspberry Pi Pico)’ option f
- Select the interpreter for raspberry Pi Pico
Copy all the below codes and then save all of the codes on Raspberry Pi Pico Board.
- Click on the ‘save’ icon to save the program.
- After clicking on the ‘Save’, an option ‘Where to save to? can select any of the ‘Raspberry Pi Pico’ options.
- Run the program once it is successfully saved and compiled without any bugs.
from machine import Pin,PWM
def __init__(self, pin: int=15, hz: int=50):
self._servo = PWM(Pin(pin))
#duty = 1638 = 0.5ms = 65535/2/(T)(1/50)/2*1000
def ServoDuty(self, duty):
if duty <= 1638:
duty = 1638
if duty >= 8190:
duty = 8190
def ServoAngle(self, pos):
if pos <= 0:
pos = 0
if pos >= 180:
pos = 180
pos_buffer = (pos/180) * 6552
self._servo.duty_u16(int(pos_buffer) + 1638)
def ServoTime(self, us):
if us <= 500:
us = 500
if us >= 2500:
us = 2500
pos_buffer= (us / 1000) * 3276
after uploading myservo.py myservo.py it’s time to upload the main code, upload below code and
from myservo import Servo
for i in range(0, 180, 1):
for i in range(180, 0, -1):
When you run the code on the Raspberry Pi pico board, you will see that the servo is rotating from 0 to 180 degrees and 180 to 0 degrees continuously.
Read Similar Articles:
- Getting Started Raspberry Pi Pico – Pinout, Specs – Beginner Guide
- Interfacing PIR Motion Sensor with Raspberry Pi Pico
- Raspberry Pi Pico Home Automation System
- Interface Servo Motor With Raspberry Pi Pico
- Interface 0.96″ OLED Display with Raspberry Pi Pico
- Raspberry Pi Pico Weather Station Using Dht11 Sensor
- Interface 16*2 LCD Display With Raspberry Pi Pico