JontyMC
Posts: 9
Joined: Tue Jun 11, 2013 8:43 am

Controlling servos

Tue Jun 11, 2013 9:00 am

Hi, I am new to the Raspberry Pi and have limited electronics knowledge. I've done a fair bit of googling around how to control servos with the pi and there is a lot of different information. I eventually used the circuit layout from this:

http://raspi.tv/tag/direct-servo-contro ... spberry-pi

I used the GPIO library https://pypi.python.org/pypi/RPi.GPIO and a variation on this program to get the servo moving:

http://raspi.tv/2013/how-to-use-soft-pw ... ed-control

I have a few questions:

[*] What GPIO commands should I use to control the servo? The above worked, but I'm not sure what it is actually doing. Can I just turn the GPIO to ON?
[*] Is controlling servos directly via GPIO pins "safe" for the pi?
[*] Should I buy a board for this? If so, which one and how should I connect?

Thanks!
Jon

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Controlling servos

Tue Jun 11, 2013 10:35 am

Servos generally have a rather arcane interface (servos pre-date digital electronics).

To set the position of a servo you need to send it regular pulses (50 times a second is fairly normal). So every 20 ms you need to send a pulse.

The length of the pulse determines the servo position. A 1.5 ms on pulse during the 20 ms means go to the centre position. Shorter pulses move clockwise, longer pulses move anti-clockwise (there is a 1 in 2 chance I have the directions wrong).

Just setting the gpio high or low and not changing it means nothing to the servo. It needs pulses.

It is safe to drive the control signal directly from a gpio.

You don't need to buy external hardware. Some people think you may have less jitter if you use external hardware. Personally I'd only buy an interface board if the software solution fails.

JontyMC
Posts: 9
Joined: Tue Jun 11, 2013 8:43 am

Re: Controlling servos

Tue Jun 11, 2013 10:46 am

Thanks, great reply!

What's the best way of programmatically sending the pulses? Should I write something like this:

Code: Select all

while True:
	GPIO.output(pin, GPIO.HIGH)  
	time.sleep(0.0015)  
	GPIO.output(pin, GPIO.LOW)  
	time.sleep(0.0185)

User avatar
joan
Posts: 14359
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Controlling servos

Tue Jun 11, 2013 10:57 am

That is absolutely the correct way.

It will work, but.

Python (like everything else) is being interrupted by the operating system to let other programs run. They will cause jitter in the pulse when your program is interrupted between the start and stop pulse lines. It may be OK for your use, it depends on what you want to do.

There is a solution. Use one of the software solutions which use DMA to time the pulses. DMA is not affected by operating system interrupts. Look for servoblaster, piblaster, pipwm etc., or my own pigpio.

PiGraham
Posts: 3665
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Controlling servos

Tue Jun 11, 2013 3:19 pm

Take a look at pigpio http://abyz.co.uk/rpi/pigpio/index.html
or Servoblaster https://github.com/richardghirst/PiBits

These will handle the timing for you, producing a steady stream of pulses.

Trying to drive servo PWM from script is not likely to work very well because timing will be variable and your servos will probably jitter. Use a library that runs timing from hardware or high-priority kernel code.

An alternative is a dedicated servo controller to take the load off the Pi.
E.g. http://www.hobbytronics.co.uk/pololu-se ... word=servo
Last edited by PiGraham on Tue Jun 11, 2013 4:28 pm, edited 1 time in total.

JontyMC
Posts: 9
Joined: Tue Jun 11, 2013 8:43 am

Re: Controlling servos

Tue Jun 11, 2013 4:19 pm

Awesome, thanks :D

updowndown
Posts: 18
Joined: Fri Jul 19, 2013 8:49 am

Re: Controlling servos

Sat Aug 03, 2013 3:34 pm

I don't know what you want to use your Servo for, but if it's for something that doesn't need much precision (such as moving in increments of 10 degrees or more) using a software implementation of pulse-width modulation (PWM) is the easiest approach.

Simply connect the signal wire to a GPIO pin, the positive and negative to 5V and GND respectively (assuming it's a 5V Servo), and set the GPIO pin to a PWM pin at 50Hz for the required duty cycle.

I've made the following video that walks you through the steps:

http://youtu.be/ddlDgUymbxc

Hope that helps!
Raspberry Pi Tutorials at: www.youtube.com/user/updowndown

User avatar
Richard-TX
Posts: 1549
Joined: Tue May 28, 2013 3:24 pm
Location: North Texas

Re: Controlling servos

Wed Aug 07, 2013 11:38 am

As far as I am concerned, when it comes to servos, the Adafruit 16 channel controller can't be beat. Easy to program and robust. I have two in operation now and will be adding more.
Richard
Doing Unix since 1985.
The 9-25-2013 image of Wheezy can be found at:
http://downloads.raspberrypi.org/raspbian/images/raspbian-2013-09-27/2013-09-25-wheezy-raspbian.zip

mikerr
Posts: 2781
Joined: Thu Jan 12, 2012 12:46 pm
Location: UK
Contact: Website

Re: Controlling servos

Wed Aug 07, 2013 12:58 pm

My biped robot uses no external chips to control the 6 servos, its all just software PWM (servoblaster):
http://www.raspberrypi.org/phpBB3/viewt ... 37&t=47315

The view that software PWM is very "jittery" assumes the pi is slower than it is IMO. I find it fine in practice.

LeoWhite
Posts: 131
Joined: Mon Apr 08, 2013 9:20 am
Contact: Website

Re: Controlling servos

Wed Aug 07, 2013 1:03 pm

Richard-TX wrote:As far as I am concerned, when it comes to servos, the Adafruit 16 channel controller can't be beat. Easy to program and robust. I have two in operation now and will be adding more.
I picked up one of those earlier this week and am starting to get to grips with it. I've not played with servos much before and it seems rather trail and error based. Are you controlling it using Python? (as in the examples provided?)

calistra
Posts: 5
Joined: Fri Sep 06, 2013 6:27 pm

Re: Controlling servos

Fri Sep 06, 2013 6:31 pm

You may like to look at the Raspy Juice board.

It has an ATMEGA controller to drive four servos leaving the PI free for other jobs. In addition, it has a good power regulator on board to allow the Pi to be powered by anything from 6 to 23 volts.

You can get it from http://www.2wattelements.com/ or Adafruit.

SunayK
Posts: 12
Joined: Fri Apr 28, 2017 7:01 pm
Contact: Website

Re: Controlling servos

Wed May 03, 2017 7:21 pm

JontyMC wrote:Hi, I am new to the Raspberry Pi and have limited electronics knowledge. I've done a fair bit of googling around how to control servos with the pi and there is a lot of different information. I eventually used the circuit layout from this:

http://raspi.tv/tag/direct-servo-contro ... spberry-pi

I used the GPIO library https://pypi.python.org/pypi/RPi.GPIO and a variation on this program to get the servo moving:

http://raspi.tv/2013/how-to-use-soft-pw ... ed-control

I have a few questions:

[*] What GPIO commands should I use to control the servo? The above worked, but I'm not sure what it is actually doing. Can I just turn the GPIO to ON?
[*] Is controlling servos directly via GPIO pins "safe" for the pi?
[*] Should I buy a board for this? If so, which one and how should I connect?

Thanks!
Jon
No Need to buy additional servo Motor drivers for Raspberry PI, may it sound strange but I was able to test it and make it work and its been working for long time now. In Raspberry PI you can controll upto 8 servo motors using software drivers, may be more Servo.
http://www.leenabot.com/en/Driving-Serv ... pberry-Pi/ give you detail how to install and use software drivers for driving servo with Raspberry PI.
Last edited by SunayK on Wed May 31, 2017 1:27 am, edited 1 time in total.
Build your own Robot! visit www.LeenaBOT.com for more detail

PiGraham
Posts: 3665
Joined: Fri Jun 07, 2013 12:37 pm
Location: Waterlooville

Re: Controlling servos

Thu May 04, 2017 8:43 am

mikerr wrote:My biped robot uses no external chips to control the 6 servos, its all just software PWM (servoblaster):
http://www.raspberrypi.org/phpBB3/viewt ... 37&t=47315

The view that software PWM is very "jittery" assumes the pi is slower than it is IMO. I find it fine in practice.
I think ServoBlaster, pigpio and other libraries that allow PWM on any pin are not strictly "software pwm" because they use DMA hardware to get consistent timing. You can do "software PWM" using timing loops in code (do-nothing instructions or polling the system uptime counter, for example, that will suffer more from preemptive scheduling and interrupts than is the case for the DMA approach.

I agree that it is unlikely that jitter will be a problem using ServoBlaster or pigpio to control 6 servos.

SunayK
Posts: 12
Joined: Fri Apr 28, 2017 7:01 pm
Contact: Website

Re: Controlling servos

Thu May 04, 2017 3:52 pm

PiGraham wrote:
mikerr wrote:My biped robot uses no external chips to control the 6 servos, its all just software PWM (servoblaster):
http://www.raspberrypi.org/phpBB3/viewt ... 37&t=47315

The view that software PWM is very "jittery" assumes the pi is slower than it is IMO. I find it fine in practice.
I think ServoBlaster, pigpio and other libraries that allow PWM on any pin are not strictly "software pwm" because they use DMA hardware to get consistent timing. You can do "software PWM" using timing loops in code (do-nothing instructions or polling the system uptime counter, for example, that will suffer more from preemptive scheduling and interrupts than is the case for the DMA approach.

I agree that it is unlikely that jitter will be a problem using ServoBlaster or pigpio to control 6 servos.
You have option of using PCM instead of PWM signal to drive servo using software servodrivers.. I have been using it and no Servo Jitter sound..
Build your own Robot! visit www.LeenaBOT.com for more detail

Return to “Automation, sensing and robotics”