0

I use 9v/0.5A DC cable to breadboard and connect servo motor but it doesn't work. Code might be wrong but I'm not sure.

I connect raspberry pi - breadboard - servo motor like this

red:(+), balck:gound(-), orange:GPIO12

and this is the code

import RPi.GPIO as GPIO     
from time import sleep      

print('1')
GPIO.setmode(GPIO.BCM)      
print('2')
GPIO.setwarnings(False)

servo_pin = 12                   
print('3')
GPIO.setup(servo_pin, GPIO.OUT)  
servo = GPIO.PWM(servo_pin, 50)  
servo.start(0)  

servo_min_duty = 3               
servo_max_duty = 12              
print('4')
def set_servo_degree(degree):    

    if degree > 180:
        degree = 180
    elif degree < 0:
        degree = 0

    
    duty = servo_min_duty+(degree*(servo_max_duty-servo_min_duty)/180.0)
    
    servo.ChangeDutyCycle(duty)
print('5')

try:                                    
    while True:                         
        set_servo_degree(0)             
        sleep(1)                        
        set_servo_degree(90)
        sleep(1)
        set_servo_degree(120)
        sleep(1)
        set_servo_degree(180)
        sleep(1)
        print('6')



finally:                                
    GPIO.cleanup()                      

Please help me. Thank you!

New contributor
bluesky is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
4
  • 1
    It doesn't appear to be connected to anything.
    – Milliways
    2 days ago
  • You are using wire colors indiscriminately. For example, you are using two black wires, one as ground, one as power.
    – NomadMaker
    2 days ago
  • why do you hide connections in the picture?
    – jsotola
    2 days ago
  • You need to provide clear photos so that we can follow all the connections. To me it looks like you have connected both servo ground and power to the breadboard 5V rail.
    – joan
    2 days ago

0

Your Answer

bluesky is a new contributor. Be nice, and check out our Code of Conduct.

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.