By Abdul Rehman
If you want to control servo motor with Raspberry Pi, or you wonder how to drive servo motor with raspberry pi? Then this article is for you. In this article we are going to present Raspberry Pi servo motor control python code so that any one who wish to drive servo motor with raspberry pi will be able to benefit from this.
Servo motors are one of famous types of motors among students to experiment different robotic projects. These motors are widely used in Robotics because of their accurate angle control which helps us to obtain very precise position control. Like other dc motors, servo motors also operates on DC motors and one of famous type of servo SG-90 operates on 5 volt DC.
We are going to interface servo motor with our Raspberry PI 3 model. So first lets decide the desired pin configuration which we want to connect our servo motor with. As we know that servo motor have 3 wires, in which one is for positive one for ground and remaining one wire mostly Yellow wire is for input signal. This input signal is normally in form of PWM signal. In our previous article we talked about raspberry PI Pin configurations. So with the help of visual Pin outs we will decide one of GPIO to generate PWM over it. Here is alternate tutorial about driving servo motor with raspberry pi if you want to check another option.
Here is Python code with RPi.GPIO library to interface servo motor with GPIO pin 17 which board pin name is 11
Code language: PHP (php)
import RPi.GPIO as GPIO servoPin = 11 GPIO.setmode(GPIO.BOARD) GPIO.setup(servoPin,GPIO.OUT) #configure as Output p = GPIO.PWM(servoPin,50) #initialize pwm with 50% duty cycle p.start(2.5) #start with angle
If some how later on we need to change the angle of servo motor we just simply need to call following function with reference of ‘p’ variable which we had saved for this purpose
Code language: CSS (css)
After we had done everything we just simply need to cleanup our GPIOs, as we are used to do it in any GPIO related application.
Code language: CSS (css)
If you which your python code to run on startup? You just simply do this with five different ways. My simple and favorite method is to edit rc.local file.
sudo nano /etc/rc.local
Don’t forget to call sudo before it. You need root permission to edit this file. Once you are in it, just simply add a new line with full path of your python file which you want to run on startup.
sudo python /home/pi/sample.py &
we added (&) ampersand sign at end of the line because our program will run continuously forever with infinite loop.
28 December 2021
13 December 2021
30 November 2021
19 November 2021
13 November 2021
FYP Solutions Powered By Impressive Business WordPress Theme