Lesson 8 Servo

Share for us
Share on facebook
Share on twitter
Share on pinterest
Share on whatsapp

Introduction

Servo is a type of geared motor that can only rotate 180 degrees. It is controlled by sending electrical pulses from your microcontroller. These pulses tell the servo what position it should move to.

A servo has three wires, the brown wire is GND, the red one is VCC, and the orange one is signal line, as shown below.

Components

– 1*Raspberry Pi

– 1*Breadboard

– 1*Network cable (or USB wireless network adapter)

– 1*Servo

– Several jumper wires

Experimental Principle

Servo consists of shell, circuit board, non-core motor, gear and location detection. Its working principle is as follow: Raspberry Pi controller sends PWM signal to servo motor, and then this signal is processed by IC on circuit board to calculate rotation direction to drive motor, and then this driving power is transferred to swing arm by reduction gear. At the same time, position detector returns location signal to judge whether set location is reached or not.

Experimental Procedures

Step 1: Connect the circuit

           Servo                          Raspberry Pi

            Orange line————————————— GPIO1

              Red line —————————————-  +5V

            Brown line—————————————-  GND

Step 2: Edit and save the code (see path/Rpi_BasicKit/08_servo/servo.py)

Step 3: Modify the file permissions     

chmod  +x  servo.py

Step 4: Run the program        

python  servo.py 

Press Enter, you can see the servo motor rotate 180 degrees. And then rotate in opposite direction.

C Code

#include <wiringPi.h> #include <stdio.h> #include <unistd.h> #define SIG 0 void servoInit(void) { pinMode(SIG, OUTPUT); digitalWrite(SIG, 0); } void servoCtrl(int dutyCycle) { digitalWrite(SIG, 1); delayMicroseconds(dutyCycle); digitalWrite(SIG, 0); delayMicroseconds(20000 – dutyCycle); } int main(void) { int i; if(wiringPiSetup() == -1){ //when initialize wiring failed,print messageto screen printf(“setup wiringPi failed !”); return 1; } servoInit(); while(1){ /* for(i=500;i<=2500;i+=5){ servoCtrl(i); delay(150); } delay(1000); */ /* for(i=2500;i>=500;i–){ servoCtrl(i); delay(100); } delay(1000);*/ /* servoCtrl(500); sleep(1); servoCtrl(1000); sleep(1); */ servoCtrl(1500); sleep(1); servoCtrl(2000); sleep(1); servoCtrl(2500); sleep(1); } return 0; }


Python Code

#!/usr/bin/env python import RPi.GPIO as GPIO import time SigPin = 11 T = 0.02 #0.02s, 50HZ pluse_width = [0.0005, 0.0010, 0.00125, 0.0015, 0.00175, 0.0020, 0.0025] #0,45,68,90,112,135,180 def set_angle(angle): GPIO.output(SigPin, GPIO.HIGH) time.sleep(angle) GPIO.output(SigPin, GPIO.LOW) time.sleep(T-angle) def setup(): GPIO.setmode(GPIO.BOARD) #Number GPIOs by its physical location GPIO.setup(SigPin, GPIO.OUT) #set pin mode is output GPIO.output(SigPin, GPIO.LOW) #set pin high level(3.3V) def loop(): while True: set_angle(pluse_width[0]) time.sleep(0.5) set_angle(pluse_width[1]) time.sleep(0.5) set_angle(pluse_width[2]) time.sleep(0.5) set_angle(pluse_width[3]) time.sleep(0.5) set_angle(pluse_width[4]) time.sleep(0.5) def destroy(): #When program ending, the function is executed. GPIO.output(SigPin, GPIO.LOW) GPIO.setup(SigPin, GPIO.IN) #set pin mode is input GPIO.cleanup() def ctrl(angle): n = 50 while n: n = n – 1 setup() if angle == 0: set_angle(0.0005) elif angle == 45: set_angle(0.0010) elif angle == 68: set_angle(0.00125) elif angle == 90: set_angle(0.0015) elif angle == 112: set_angle(0.00175) elif angle == 135: set_angle(0.0020) elif angle == 180: set_angle(0.0025) destroy() if __name__ == ‘__main__’: #Program start from here setup() try: loop() except KeyboardInterrupt: destroy()