Servo Motor Control Using Raspberry Pi

Introduction and Component :-
  1. 5V, 1A Power Supply
  2. Raspberry Pi 2 B+
  3. Analog Servo Motor
  4. Connecting Wires
Servo motor has got three connection pins, Vcc, ground and signal. It requires a 5V 1A power, which is provided from a suitable supply source. On the Raspberry Pi board, GPIO pin number 22 is selected as the signal pin. The GND pin of the power supply is connected to the ground pin of the servo and also to the 6th pin on the Raspberry Pi. After providing suitable connections open the python editor and program the Raspberry Pi. A sample code is given below, which you can use as it is or you can modify it to suit yourself
 Code :-
import RPi                                  ## Import GPIO Library.
import time                                 ## Import ‘time’ library for a delay.

GPIO.setmode(GPIO.BOARD)                    ## Use BOARD pin numbering.
GPIO.setup(22, GPIO.OUT)                    ## set output.

pwm=GPIO.PWM(22,100)                        ## PWM Frequency
pwm.start(5)

angle1=10
duty1= float(angle1)/10 + 2.5               ## Angle To Duty cycle  Conversion

angle2=160
duty2= float(angle2)/10 + 2.5

ck=0
while ck<=5:
     pwm.ChangeDutyCycle(duty1)
     time.sleep(0.8)
     pwm.ChangeDutyCycle(duty2)
     time.sleep(0.8)
     ck=ck+1
time.sleep(1)
GPIO.cleanup()

 As per the above program the servo rotates 180 degrees. The length of the pulse will determine the position of the servo. Equivalent  duty cycle is calculated for an angle and the RPi will generate a pulse for the calculated duty cylce, which will be send to the servos signal pin every 10 milliseconds. When executed the servo will rotate in a to and fro motion as shown in the illustration above.
Raspberry Pi kit along with servo is a great package for developing real time robotic projects as the RPi supports both image and audio processing units. The servo can be controlled on the basis of the output obtained from image processing operations, which would aid realizing innovative robotic ideas


Hardware Interfacing :-








Comments

Popular posts from this blog

Simple SMPS

Turning on an LED with your Raspberry Pi's GPIO Pins

Ultrasonic Distance Measurement Using raspberry pi