Close

Controlling a servo motor with RPi

A project log for Raspberry Pi Weather App

Using the Raspberry Pi to get weather from the internet

vaneh-boghosianVaneh Boghosian 05/07/2018 at 22:300 Comments

5/7/18

I learned how to control a servo motor using Raspberry Pi. This servo motor will most likely be used as a dial that will turn depending on the humidity (or temperature) of the weather outside. The servo did initially shake a lot when going to different angles. The solution to reduce this shaking was that after going to a certain angle, stop the servo for half a second, then start it again; below is the code of how I did this.  

p.ChangeDutyCycle(7.5)
        time.sleep(0.3)
        p.stop()
        time.sleep(0.5)
        
        p = GPIO.PWM(19,50) 
        p.start(7.5)
        p.ChangeDutyCycle(12.5)

First I start the servo motor at a duty cycle of 7.5 which corresponds to 90 degrees. I give it a third of a second to get there before stopping the servo motor for half a second. I then must put which pin I am using again (p = GPIO.PWM(19,50)) because it isn't saved after the servo is stopped. I then start the power again making sure that I am starting it at the same angle that I stopped at. Then I go to duty cycle of 12.5 which is 180 degrees (duty cycle of 2.5 is 0 degrees).

The entire code that can be found on my GitHub page is in the Scratch Pad folder, the file is called LED.py. This code moves the servo motor from 0 to 90 to 180 as can be seen in the attached video.

Discussions