How to control a servo motor with the Raspberry Pi Pico board

How to control a servo motor with the Raspberry Pi Pico board

Hello and welcome back. In this tutorial, we will learn how to control a servo motor with a Raspberry Pi Pico board. For that, I used an SG90 servo motor and a potentiometer. This potentiometer allows you to rotate the servo motor to the desired angle. That is, we can control the PWM output values. Also, I used the Thony IDE and MicroPython language for this process. If you are not familiar with this Thonny IDE and MicroPython language, please visit my previous articles. I think this tutorial will help you create robots, drones, aircraft, or other servo motor-based projects.

Servo motor

In this tutorial, I used the SG90 servo motor. Also, we can rotate this servo motor from 0 to 180 degrees. This requires a 5v potential to operate and can be rotated to the desired degrees by a PWM signal. You can buy this servo motor low cost in the market. Also, these servo motors are available in different sizes and it depends on your task. You can use this link for how to control a servo motor with the Arduino.

Potentiometer

Basically, we can control the resistance using this component. Therefore, we can get the Analog input to the Raspberry Pi Pico board and this value is between 0 to 65535. For that, we can use GPIO 26,27,28, and 29 pins on the Raspberry Pi Pico board. You can also use a preset if you want.

PWM

PWM is simply called Pulse Width Modulation. In the Raspberry Pi Pico board that value is between 0 and 0-65535. That is, 0v to 3.3v.

Do you know? On the Arduino board, this value is between 0 and 255.

The Arduino board includes the specific pins for the PWM outputs. But, PWM signals can be received from any GPIO PIN on the Raspberry Pi Pico board. For this process, we can use the PWM class in the MicroPython language.

More info about the PWM – Click me

OK, let’s do it step by step. The required components are given below.

Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.

Step 1

Firstly, identify these components.

Step 2

Secondly, attach these components to the breadboard.

Step 3

Thirdly, connect these components to the Raspberry Pi Pico board. For that, use the circuit diagram below.

How to control a servo motor with the Raspberry Pi Pico board

Step 4

Next, connect the Raspberry Pi Pico board to the computer.

Step 5

Now, let’s create the program for this. It’s as follows. You must use the Thonny IDE for that. (You can use this link to set up Thonny IDE with Raspberry Pi Pico board)

  • Full details of this tutorial – Download
#Include the library files
from machine import Pin,PWM,ADC
from time import sleep

servo = PWM(Pin(0))#Include the servo motor pin
potentiometer = ADC(28)#Include the potentiometer pin
servo.freq(50)#Set the frequency

#PWM min and max value
in_min = 0
in_max = 65535
#Servo motor min and max degrees
out_min = 1000
out_max = 9000


while True:
    #Get the potentiometer values
    value = potentiometer.read_u16()
    #Convert PWM values from 0 to 180
    Servo = (value - in_min) * (out_max - out_min) / (in_max - in_min) + out_min
    #Rotate the servo motor
    servo.duty_u16(int(Servo))

Step 6

Then, save this code on the Raspberry Pi Pico board. Also, you must remember to save it as “main.py”.

Step 7

Finally, click the Run button. Now you can test this tutorial without a computer. Provide a 5VDC power supply for this. The full video guide is below. So, see you in the next project or tutorial.

How to control a servo motor with the Raspberry Pi Pico board

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *