Michael Cho
Posts: 1
Joined: Wed May 15, 2019 12:23 pm

why my sg90 servo can't work with the code below?

Wed May 15, 2019 12:42 pm

I searched for many posts and I used their codes that are able to make their sg90 servo motors work.
Then I used their codes, all of them, but none could work.
I confirm that my raspberry pi can work and the connections of lines are correct. And my servo is new so it should work .

Here is my code (It's based on python3):

Code: Select all

#! python3

import RPi.GPIO as GPIO
import time
tilt = 12
GPIO.setmode(GPIO.BOARD)
GPIO.setwarnings(False)
GPIO.setup(tiltPin, GPIO.OUT)
tilt = GPIO.PWM(tiltPin, 50)
tilt.start(0)
tilt.ChangeDutyCycle(2)
time.sleep(1)
tilt.ChangeDutyCycle(3)
time.sleep(1)
tilt.stop()
GPIO.cleanup()
So what's wrong with my code?

User avatar
joan
Posts: 15949
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: why my sg90 servo can't work with the code below?

Wed May 15, 2019 8:05 pm

At 50Hz a dutycycle of 2% will give pulses of width 400µs and 3% will give 600µs. I suppose a SG90 might respond to 600µs. I would start with pulse widths in the range 1000-2000µs which is normally a safe range for servos.

pcmanbob
Posts: 12841
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: why my sg90 servo can't work with the code below?

Wed May 15, 2019 10:30 pm

If you want to drive an sg90 servo using the code you posted you need to set duty cycle values between 2.5 and 12.5 , these are the safe limits which equate to the the 2 extremes of travel on the sg90 servo.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Troubleshooting”