Overview
This is a detailed tutorial on Servo Motor control using Raspberry Pi Pico RP2040 Microcontroller. We will use PWM (Pulse Width Modulation) in Raspberry Pi Pico to rotate and control Servo Motor.
Pulse-width modulation (PWM) is used for controlling the amplitude of digital signals in order to control devices and applications requiring power or electricity. This is an advanced tutorial on PWM generation. We will learn how to generate a variable PWM signal with the Raspberry Pi Pico RP2040 microcontroller. Variable PWM signal is used for controlling the speed of DC motors/Fans.
In this guide, we will learn about the PWM with the help of a Servo Motor. We will also learn how to interface Servo with the Raspberry Pi Pico RP2040 Microcontroller. The MicroPython Code along with the Servo library can rotate the Servo Motor in the desired direction.
Components Required
In this guide, I used Elecrow Raspberry Pi Pico Starter Kit to test different Modules. You can buy the kit and perform some other operations as well. From this kit, you can use the following components.
1. Raspberry Pi Pico Board – 1
2. Servo Motor – 1
3. Breadboard – 1
4. Jumper Wires – 4
5. Micro-USB Cable – 1
Servo Motor
Servo motors are high-torque motors that are commonly used in robotics and several other applications due to the fact that it’s easy to control their rotation. Servo motors have a geared output shaft that can be electrically controlled to turn one (1) degree at a time. For controlling, servo motors usually have additional pins (signal Pins) apart from power pins (Vcc and GND). The signal pin is used to control the servo motor, turning its shaft to any desired angle.
Micro Servo Motor SG90 is a tiny and lightweight server motor with high output power. The servo can rotate approximately 180 degrees (90 in each direction) and works just like the standard kinds but smaller. You can use any servo code, hardware, or library to control these servos. It comes with 3 horns (arms) and hardware.
Control Servo Motor with Raspberry Pi Pico
Let us learn how we can interface the SG-90 Servo Motor with Raspberry Pi Pico to control its rotation. Connect the Servo Motor with Raspberry Pi Pico as shown in the circuit diagram below.
Connect the VCC, GND & Signal Pin of the SG-90 Servo Motor to the 3.3V, GND & GP0 pin of the Raspberry Pi Pico.
MicroPython Code/Program
The code is divided into two parts. One is servo.py & the other is main.py. The servo.py is the library required by Servo Motor.
servo.py
Copy the following code and save it as servo.py in Raspberry Pi Pico.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
from machine import Pin, PWM class Servo: """ A simple class for controlling a 9g servo with the Raspberry Pi Pico. Attributes: minVal: An integer denoting the minimum duty value for the servo motor. maxVal: An integer denoting the maximum duty value for the servo motor. """ def __init__(self, pin: int or Pin or PWM, minVal=2500, maxVal=7500): """ Creates a new Servo Object. args: pin (int or machine.Pin or machine.PWM): Either an integer denoting the number of the GPIO pin or an already constructed Pin or PWM object that is connected to the servo. minVal (int): Optional, denotes the minimum duty value to be used for this servo. maxVal (int): Optional, denotes the maximum duty value to be used for this servo. """ if isinstance(pin, int): pin = Pin(pin, Pin.OUT) if isinstance(pin, Pin): self.__pwm = PWM(pin) if isinstance(pin, PWM): self.__pwm = pin self.__pwm.freq(50) self.minVal = minVal self.maxVal = maxVal def deinit(self): """ Deinitializes the underlying PWM object. """ self.__pwm.deinit() def goto(self, value: int): """ Moves the servo to the specified position. args: value (int): The position to move to, represented by a value from 0 to 1024 (inclusive). """ if value < 0: value = 0 if value > 1024: value = 1024 delta = self.maxVal-self.minVal target = int(self.minVal + ((value / 1024) * delta)) self.__pwm.duty_u16(target) def middle(self): """ Moves the servo to the middle. """ self.goto(512) def free(self): """ Allows the servo to be moved freely. """ self.__pwm.duty_u16(0) |
main.py
Copy the following code and save it as main.py in the Raspberry Pi Pico.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
import utime from servo import Servo s1 = Servo(0) # Servo pin is connected to GP0 def servo_Map(x, in_min, in_max, out_min, out_max): return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min def servo_Angle(angle): if angle < 0: angle = 0 if angle > 180: angle = 180 s1.goto(round(servo_Map(angle,0,180,0,1024))) # Convert range value to angle value if __name__ == '__main__': while True: print("Turn left ...") for i in range(0,180,10): servo_Angle(i) utime.sleep(0.05) print("Turn right ...") for i in range(180,0,-10): servo_Angle(i) utime.sleep(0.05) |
Now click on the Run button to run the library and main file.
The Servo will rotate clockwise and in an anticlockwise direction. The rotation speed and angle can be controlled from the above code.
1 Comment
How would I attach a button as well