In this tutorial we are going to control a SG-90 servo motor with Raspberry Pi Pico board, There are many beginners suffering from the basics of Raspberry Pi Pico Board. So I decided to start a series of articles discussing about basics of Raspberry Pi Pico Projects. Checkout our all 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 motors. if you want to control multiple Servos motors then you have to change code. If you understand code you will able to control multiple servos.
- Raspberry Pi Pico
- Servo motor
- Jumper cable
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
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
Copy all this below code and then save all of the 3 codes in Raspberry Pi Pico Board.
from time import sleep from machine import Pin from machine import PWM pwm = PWM(Pin(15)) pwm.freq(50) #Function to set an angle #The position is expected as a parameter def setServoCycle (position): pwm.duty_u16(position) sleep(0.01) while True: for pos in range(1000,9000,50): setServoCycle(pos) for pos in range(9000,1000,-50): setServoCycle(pos)
When you run the code on Raspberry Pi pico board, you will see that the servo is rotating from 0 to 180 degree and 180 to 0 degree continuously.