Skip to main content

Controlling the SG-90 Servo Motor with a Raspberry Pi Pico

·246 words
icysamon
Author
icysamon
Electronics & Creator

Specifications
#

  • Operating Voltage: 3.3 ~ 6 V
  • Operating Speed: 0.12 sec / 60 degrees
  • Rotation Range: Approx. -90 ~ 90 degrees
  • Temperature Range: 0 ºC – 55 ºC

Wiring Connections
#

SG-90 Raspberry Pi Pico
Orange Wire GP0 (Depending on code)
Red Wire VBUS (5V)
Brown Wire GND

PWM Control
#

Angle Pulse Width Duty Cycle (Pulse Width / Period)
-90° (Left) 0.5ms 0.5ms / 20ms
0° (Center) 1.45ms 1.45ms / 20ms
90° (Right) 2.4ms 2.4ms / 20ms

Code
#

from machine import Pin, PWM
import time

duty_max = 65025
deg_min = 0.025 # -90
deg_middle = 0.0725 # 0
deg_max = 0.12 # 90

# init
def init(pin_pwm = 0, freq = 50):
    global servo
    servo = PWM(Pin(pin_pwm))
    servo.freq(freq)

# example function
def example(interval = 3):
    servo.duty_u16(round(duty_max * deg_min))
    time.sleep(interval)
        
    servo.duty_u16(round(duty_max * deg_middle))
    time.sleep(interval)
        
    servo.duty_u16(round(duty_max * deg_max))
    time.sleep(interval)

Parameters
#

duty_max
#

The maximum duty value (100%).

deg_min
#

The duty cycle at -90°.

deg_middle
#

The duty cycle at 0°.

deg_max
#

The duty cycle at 90°.

Functions
#

init(pin_pwm = 0, freq = 50)
#

An initialization function to configure the pin and frequency.

  • pin_pwm: The pin connected to the PWM signal wire.
  • freq: The frequency, set here to 50Hz according to the data sheet.

example(interval = 3)
#

A test function. It controls the loop between -90° -> 0° -> 90°.

  • interval: The rotation interval.

Repository
#