Raspberry Pi Hands-on Seminars Interfacing to Servo Motors Gregory Griffes Gregory Griffes 8/16/15
Overview Types of servo motors and operation Electronic connections to a servo Python code to control a servo Wiring and operating your own servo motor
Hardware High power charging hub Custom servo cable Servo motor RPi GPIO Breakout breadboard 3.3v to 5v Level shifter (I2C safe version)
Parts Needed Canakit Ultimate Starter kit with breadboard, breakout board, ribbon cable, jumper wires, and electronic parts one Tek Republic TUH-3700 USB 3.0 7 Port Hub charging hub ($30) one HiTec HS-5055MG digital servo motor ($18) one Parallax continuous rotation servo motor ($10) one Adafruit 4-channel I2C-safe Bi- directional Logic Level Converter ($8)
Step 1: Avoid Damage to your Rpi hardware ESD – ElectroStatic Discharge Use a wrist strap, mat, & ESD bags
Step 2: Breadboard Remove the Rpi from the static bag Connect breadboard circuit (see video) Make sure the connector is correctly aligned
Step 2: Hardware Warnings DANGER! Step 2: Hardware Warnings +5V Damage! Voltage and current can damage Rpi GPIOs are 3.3 volt and NOT protected from other voltages Applying 5 volts to a GPIO can cause permanent damage to the Rpi When wired correctly – no damage occurs Rpi GPIO max voltage = 3.3v Rpi GPIO max current = 16ma (0.016) Rpi all GPIOs max current = 50ma / /- Rpi Correct +3.3V Rpi GPIO
Pulse Width Modulator PWM used for controlling the servo motor
Servo Motors PID (Proportional Integral Differentiator)
Servo Motor Specs Parameter Tower Pro SG51r Tower Pro SG-5010 Stall Torque @4.8v, 8.3 oz-in1 @4.8v, 76.4 oz-in Speed @4.8v, 0.08sec/60°2 @4.8v, 0.19sec/60° Price $5 Availability good Surge current3 660ma @ 5.2v 2.2 amps @ 5.2v Voltage Droop4 3.9v 3.3v Voltage Droop5 4.8v 4.6v 1” away from center line 8.3 ounces of force. The closer to the shaft the more force is applied but the travel distance is less. Takes 80 milliseconds to move 60 degrees at 4.8v power source Amps required to move to a new position Long wires (>12”); small gauge (breadboard wires); no filter cap Short wires (6”); large gauge (USB charging cable); 1000uf filter cap
180 degree servo (aka. Standard Servo) Type A: Wire colors: Brown (GND), Orange (+5v power), Yellow (PWM signal) 500us -> 2500us = counter-clockwise rotation Type B: Wire colors: Black (GND), Red (+5v power), Yellow (PWM signal) 500us -> 2500us = clockwise rotation
Continuous Rotation servo Wire colors: Black (GND), Red (+5v power), White (PWM signal) 1000us = maximum speed clockwise rotation 1500us = stopped 2000us = maximum speed counter- clockwise rotation
Custom Servo Motor cable PWM signal connection 1000uf capacitor USB Charging cable Servo motor
Wiring up the servo Power off the Raspberry Pi Wire breadboard Connect servo Power on servo and Rpi
Three approaches to controlling the servo Direct from 3.3v GPIO Using the Adafruit I2C safe level shifter Using the Adafruit 8 channel level shifter 3.3V GPIO I2C Safe 8 Channel Advantage Simple Cheap No soldering Can be used for both servo and sensors Fits on GPIO breakout breadboard 8 channels No pullups needed Smaller footprint than two I2C safe devices Disadvantage Unreliable long term Does not protect the RPi output from higher voltages Only 4 channels Requires pull ups Does not support I2C interface Additional breadboard needed Cost $0.05 $4 $8
Servo Motor Schematic (I2C safe) Use a separate power source for the servo Reason: when the motor moves, it causes a spike on the +5v power line
Using 3.3 volt GPIO output Custom Servo cable Servo motor GPIO 18 Series resistor (3.9k approx) RPi GPIO Breakout breadboard
Using Adafruit 8 channel level shifter Servo motor Servo cable 3.3v to 5v Level shifter (8ch version) RPi GPIO Breakout breadboard
Verify Have someone else verify your wiring to make sure you don’t damage your Rpi when you turn it on
Turn On Rpi Connect monitor, mouse, and keyboard Turn on power and boot the Rpi Login and startx, open file mgr Create a projects directory Connect to internet or use thumb drive Hover over the “projects” directory and “open terminal window” Enter “git clone http://github.com/griffegg/servo_motors”
Run the program Enter “cd servo_motors” Enter “chmod +x servo-slider.py” to make it executable. Enter “sudo ./servo-slider.py” Use mouse to move slider, observe servo motor moving
DMA Controller Direct Memory Access (DMA) Hardware used to perform memory transfers to/from memory or peripherals independent from CPU operation Very fast data transfers which are not interrupted by the CPU 16 DMA channels
Using the DMA engine to control the servo joan2937 commented a day ago @Vijayenthiran servoblaster and pi-blaster (and my pigpio) have been updated to work on the Pi2. The servoblaster github repository has not been updated. You can find a link to the updated software by searching for servoblaster on the rasperrypi.org forums.
Using the DMA engine This program does not use the DMA engine Observe how the servo motor jitters Use Servoblaster, pi-blaster, or pigpio to implement the DMA servo controller
Switch to Continuous Rotation servo Unplug the 180 degree servo and plug in the continuous rotation servo Observe the different behavior
Questions ? Done!