Raspberry Pi 3B+ pwm servo control for python3

Spread the love
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  
  •  

Code

import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(8, GPIO.OUT)

p = GPIO.PWM(8, 50)

p.start(7.5)

try:
    while True:
        print('7.5 - middle')
        p.ChangeDutyCycle(7.5)  # turn towards 90 degree
        time.sleep(1) # sleep 1 second

        print('2.5 - full right')
        p.ChangeDutyCycle(2.5)  # turn towards 0 degree
        time.sleep(1) # sleep 1 second

        print('7.5 - middle')
        p.ChangeDutyCycle(7.5)  # turn towards 0 degree
        time.sleep(1) # sleep 1 second

        print('12.5 - full left')
        p.ChangeDutyCycle(12.5) # turn towards 180 degree
        time.sleep(1) # sleep 1 second 

except KeyboardInterrupt:
    print('7.5 - middle')
    p.ChangeDutyCycle(7.5)  # turn towards 90 degree
    time.sleep(1) # sleep 1 second

    print('Stopping.')
    p.stop()
    GPIO.cleanup()

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.