Control Servo SG90 Motor Dengan Raspberry Pi Pico

Persiapan Bahan #

– Raspberry Pi Pico / Pico W
– Servo SG90
– Breadboard
– Kabel Jumper Male to Male
– Kabel Micro USD
– Aplikasi Thonny IDE

Wiring Servo SG90 Raspberry Pi Pico #

  • Connect the VCC to 3.3V
  • GND To GND
  • Signal Pin of the SG-90 Servo Motor to GP0 pin of the Raspberry Pi Pico.

Sample Code MicroPython Code/Program #

The code is divided into two parts. One is & the other is The is the library required by Servo Motor.

from machine import Pin, PWM
class Servo:
    """ A simple class for controlling a 9g servo with the Raspberry Pi Pico.
        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.
            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.minVal = minVal
        self.maxVal = maxVal
    def deinit(self):
        """ Deinitializes the underlying PWM object.
    def goto(self, value: int):
        """ Moves the servo to the specified position.
            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))
    def middle(self):
        """ Moves the servo to the middle.
    def free(self):
        """ Allows the servo to be moved freely.

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):
        print("Turn right ...")
        for i in range(180,0,-10):

Powered by BetterDocs

Leave a Reply

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

Select the fields to be shown. Others will be hidden. Drag and drop to rearrange the order.
  • Image
  • SKU
  • Rating
  • Price
  • Stock
  • Availability
  • Add to cart
  • Description
  • Content
  • Weight
  • Dimensions
  • Additional information
Click outside to hide the comparison bar
What are you looking for in Partdo?
× How can I help you?