본문 바로가기

Computer/Linux

라즈베리파이 Servo motor SG90


import RPi.GPIO as GPIO

import time


GPIO.setmode(GPIO.BOARD)


GPIO.setup(12, GPIO.OUT)


p = GPIO.PWM(12, 50)


p.start(7.5)


try:

        while True:

                p.ChangeDutyCycle(7.5)  # turn towards 90 degree

                time.sleep(1) # sleep 1 second

                p.ChangeDutyCycle(2.5)  # turn towards 0 degree

                time.sleep(1) # sleep 1 second

                p.ChangeDutyCycle(12.5) # turn towards 180 degree

                time.sleep(1) # sleep 1 second

except KeyboardInterrupt:

        p.stop()

        GPIO.cleanup()






3V - Red

Ground - Green

Output GPIO18 = 12 - Yellow





아두이노는 5V 에 입력을 하는데, 라즈베리파이는 3V에 연결을 해야함, 5V연결시에 라즈베리파에 보드에 데미지를 줄 수 있음