1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ラズパイPico でSG-90サーボモーターを動かす

Last updated at Posted at 2024-09-08

ラズパイPico のPWMを使って、SG-90サーボモーターを動かしてみよう

準備:SG-90サーボモーターを GND、5V、GPIO16に接続

SG90の茶をGND、赤を5V (VBUS)、オレンジをGPIO16に接続します。

servo2.png

SG-90については、以下URL「データシート」を参照してください:
https://akizukidenshi.com/catalog/g/g108761/

プログラム

from machine import Pin, PWM
import time

# サーボモーターの制御信号を送るGPIOピンを設定
SERVO_PIN = machine.Pin(16)
PWM_FREQ = 50	# 20ms(50Hz)
servo = PWM(Pin(SERVO_PIN))
servo.freq(PWM_FREQ)

# サーボモーターを0~180度に動かす
# dutyは0~65535
# PWM Period 20ms(50Hz)
# duty を 0度(0.5ms)~90度(1.45ms)~180度(2.4ms)にする
def move_servo(pwm, angle):
    duty = (angle*(2.4-0.5)/180+0.5) / 20 * 65535
    pwm.duty_u16(int(duty))
    print(duty)

for i in range(4):
    move_servo(servo, 0)
    time.sleep(1)
    move_servo(servo, 90)
    time.sleep(1)
    move_servo(servo, 180)
    time.sleep(1)
    move_servo(servo, 90)
    time.sleep(1)

print("サーボモーターを動かしました!")
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?