Raspberry Pi Pico Projects

Interface Servo Motor With Raspberry Pi Pico

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 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.

Required Components 

  1. Raspberry Pi Pico
  2. Servo motor
  3. 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.

Ad

Schematic Diagram for Servo Motor With Raspberry Pi Pico

Servo 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

Ad
  • 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.

Source Code 

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 with your laptop.
  • To create a new project; go to Files > New.
  • Connect the Raspberry Pi Pico board with 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 3 codes in Raspberry Pi Pico Board.

  1. Click on the ‘save’ icon to save the program.
  2. After clicking on the ‘Save’, an option ‘Where to save to? can select any of the ‘Raspberry Pi Pico’ options.
  3. Run the program once it is successfully saved and compiled without any bugs.
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 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:

Leave a Reply

Your email address will not be published.

Related Articles

Ad
Back to top button