Hondo's Cabin
http://www.hondosackett.com/yabb/YaBB.pl
The Cabin >> Raspberry and Other Pies >> Connecting a Servo to the R-Pi
http://www.hondosackett.com/yabb/YaBB.pl?num=1420313817

Message started by Fernando on Jan 3rd, 2015, 2:36pm

Title: Connecting a Servo to the R-Pi
Post by Fernando on Jan 3rd, 2015, 2:36pm

After many tries and a lot of BS from various websites, I finally got a Model B Raspberry Pi to run a single servo. Part of the problem was wiring. The other part was the program. This is very similar to the Arduino running a servo.

First off, I bought a few cheap servos from Prototyping.com, a Chinese firm. They had these micro servos - the "SG90" for $2.25. I kid you not. These things go for $30 at the Hobby Stores and $20 at Radio Shack and they are the same kind! Only thing I see wrong with them them is the wiring. Instead of Red (V+), Black (GND) and White or Yellow (Signal), they have Red/Red-Orange for (V+), Brown (Ground) and Yellow (Signal). And for a simple cheap servo, like many in the industry, they do use plastic gears - so it you stress them, you will chew them up. Prototyping.com does have a larger servo with heavy duty metal gears for under $12, which is down right cheap as those go for $80 - $120!!! They have 4 other servos for $5 to $20, including a micro servo about the size of a thumb nail and a huge one that fits in the palm of your hand with an option of plastic or metal gears. The only problem I had with them, which is not their fault was customs holding back the delivery some 5 weeks!

The SG90 servo could run off the Model B's +5V output but it is not recommended. You should run it off a battery. I'll put up a diagram later after I draw it up. The +V from the battery goes to the Red/Red-Orange wire of the Servo. The Ground goes to the Brown wire of the servo AND the Ground Pi of the Raspberry Pi, which is Pin 9 on the GPIO.

Web Bullshit #1 - many sites say that Ground on the Raspberry Pi is elsewhere on the GPIO like Pin #23. But in connecting the Ground from the servo to those pins, none of them worked. Only Pin 9 on the GPIO worked.

Though you can program any GPIO Pin to be Input or Output, only a few will work as a Pulsed Modulation Output. Of all the programs I found, this one which uses GPIO 13/MSIO or Pin #21. This is the program, I had to fix a couple of bugs in it, mostly the " ' " on the comment for "don't" and "doesn't" turned it into a statement somehow so I changed it to "do not" and does not". These are the original author's comments in the program and not mine.


Code:
import RPi.GPIO as GPIO

import time

GPIO.setmode(GPIO.BOARD)

GPIO.setup(21,GPIO.OUT)

p = GPIO.PWM(21,50)        
#sets pin 21 to PWM and sends 50 signals per second
#pin 21 is the MSIO or GPIO 13

p.start(7.5)        
#starts by sending a pulse at 7.5% to center the servo

try:                      
# I still do not know what this does

   while True:      
   #starts an infinite loop

       p.ChangeDutyCycle(4.5)    
       #sends a 4.5% pulse to turn the servo CCW

       time.sleep(0.5)                  
       #continues for a half a second

       p.ChangeDutyCycle(10.5)    
       #sends a 10.5% pulse to turn the servo CW

       time.sleep(0.5)                  
       #continues for a half a second

       p.ChangeDutyCycle(7.5)    
       #sends a 7.5% pulse to center the servo again

       time.sleep(0.5)                  
       #continues for a half a second

except KeyboardInterrupt:

   p.stop()

   GPIO.cleanup()                
   #supposed to stop when a key is pressed, does not work


You can cut and paste this to a text editor on your PC and save it to a USB Drive and then transfer it to the R-Pi through the USB Drive. Raspbian does this extremely well. Save it to the main directory with a ".py" ending to denote it as a python script.

Open up the Terminal program to get into the "DOS" part of the system.
Then type (without the brackets):

sudo python [location of file if any/name of file.py] [enter/return]


If your wiring is right, the servo will start moving.

In my case, the SG90 servo turns about 120 degrees. I will need to play with some values to get it to go 180 degrees. But at least I got it this far.

Like I said before, I'll post up a diagram later and further experiment in getting more than one servo going.

Other Web Bullshit:
1) AdaFruit, who sells Raspberry Pi add ons said that you needed their version of Linux called "Occidental." I'm doing this on Raspbian, which I wanted on.

2) Many sites stated that you needed to update the firmware and Raspbian to get it to work. As far as I know, this one R-Pi I'm using is from 2011 and it's firmware is not updated. Also I'm using the 2014 version of Raspbian released in September.

3) Many sites stated that you need to do this with this pin or that pin, and labeled all their wiring wrong. Even the kid I got this from had a few mistakes in his work and I had to consult with a GPIO Diagram I found on RaspberryPi.org. Once I found which pins go where, I put in the wires and it ran perfectly.

4) Some sites claim that you need to have a heavy duty power supply on the R-Pi. Though this is true, especially for the Model B, I'm running it off a 2600ma rechargeable batter they cell for extending your cellphone battery life. These things sell for about $30 without a charger, I got a couple of them new for $8 (plus $4 for the charger) at the odd lot stores. My next steps is to run this off a solar battery.

When I have this going as I would like it to go, next project is to set up a keypad to enter a pass number code, which then will activate a servo to unlock a door and a green LED turns on. Get the code wrong, and it will flash a red LED.


Prototyping.com's website is at: http://smart-prototyping.com
Their Servo pages are:
SG90 Servo - http://smart-prototyping.com/index.php?route=product/product&product_id=23531
($2.60 or so)
Micro Servo - http://smart-prototyping.com/Goteck-1.5-g-micro-analog-servo-loading-two-linear-servo.html
($12 or so)
Standard Size Servo with Plastic or Metal Gears - http://smart-prototyping.com/E-max-analog-servo-ES08A-metal-plastic-gear.html
(you select the plastic or metal gear on the page. The plastic one goes for about $3 the metal one goes for about $4.50)
Big Servo, comes 2 in a box - http://smart-prototyping.com/E-max-analog-servo-ES08A-metal-plastic-gear.html
(I believe they are plastic gears for $12 or $6 each).

There is a shipping charge to be added when you check out. They have Overnight and 3Day Shipping through DHL and other companies, but at $50 for that, it's not worth it.

Title: Re: Connecting a Servo to the R-Pi
Post by Fernando on Jan 4th, 2015, 1:33am

Raspberry Pi Wiring Diagram:

http://www.hondosackett.com/Fernando/RaspberryPi/R-Pi_Servo.png

Title: Re: Connecting a Servo to the R-Pi
Post by Fernando on Jan 7th, 2015, 9:27pm

I see his problem at the end of the code. He put a "Stop" command before the GPIO Clean up! LOL!

Title: Re: Connecting a Servo to the R-Pi
Post by Hondo I. Sackett on Jan 18th, 2015, 3:49pm

Nice! like it. Very helpful info!

Title: Re: Connecting a Servo to the R-Pi
Post by Fernando on Jan 22nd, 2015, 1:31am

Ebay is cheaper for the same servos and the shipping is less longer of a wait.

They have the SG90 Mini Servo for $1.90 on average, and the MG90 (metal gear version of the SG90) for $4 on average.

Title: Re: Connecting a Servo to the R-Pi
Post by Hondo I. Sackett on Jan 27th, 2015, 9:16pm

not bad pricing on either. how much torque can they provide?

Title: Re: Connecting a Servo to the R-Pi
Post by Fernando on Jan 27th, 2015, 10:48pm

(This is the post I lost so I'll retype it. #$^#$! Free internet!)

The 90 in SG90 has 90 grams of torque or a couple of ounces. Mind you, this it a very tiny servo, about the size of the thumb from tip to the first joint. The metal version (the MG90) has more than 5X more torque.

Hondo's Cabin » Powered by YaBB 2.1!
YaBB © 2000-2005. All Rights Reserved.