Using the Dagu Pan/Tilt Kit with the Raspberry Pi

The Pan/Tilt kit that we sell, developed by Dagu with the guys over at Let’s Make Robots, is a great way to mount mobile sensors on your robotic projects. In this tutorial we look at how you can control the Pan/Tilt kit with a Raspberry Pi and the Python programming language.

Required Materials and Equipment

Assembly

In order to control the servos of the pan/tilt kit with the Pi, we need to provide the servos with power, and connect their signal lines to GPIO pins on the Pi. Nearly all RC servos have the same wiring layout, although the colours may vary slightly. On the pan/tilt servos, the red (middle) wire is Vcc, the brown wire is Ground and the orange wire is the signal wire.

Connect the servos to the Pi as shown in the circuit diagram below. To begin with, we’d recommend that you get the servos working before you fully assemble the Pan/Tilt kit, as this way you can work out where the centre points of the servos are.

Circuit Diagram

Circuit Diagram – Made using Fritzing
 

External Servo Power

External Servo Power – Diagram made using Fritzing

As the the servos in the Dagu pan/tilt kit don’t require much current, we can power them from the 5V line of the Pi. If you’d prefer to power them from another power supply, or if you’re using different servos that require more current, or a higher voltage, then you can modify the circuit, as shown in this image.

Understanding Servo Motors

Unlike standard DC electric motors, which turn continuously at a given speed, servo motors contain control circuitry which is constantly trying to drive the servo motor to a set angle. Most servo motors are given their target angle by sending a Pulse Width Modulated (PWM) signal along their signal wire. A PWM signal consists of a pulse that repeats at a fixed frequency (usually close to 50Hz for a servo motor), the width of the pulse determines the angle that that we want the servo to move to.

Most servos will be centred with a pulse width of approximately 1500μs, and then turn left or right with smaller or bigger pulse widths. The minimum and maximum allowable pulse widths vary from servo to servo but are usually close to 800μs and 2200μs respectively. You should avoid trying to move a servo beyond its minimum and maximum limits, as this can damage the servo.

Controlling the Pan/Tilt Kit with Python

Once you’ve assembled the circuit, you can control the servos using this Python script. This script requires the RPIO Python library, which can be installed with the following steps

sudo apt-get install python-dev python-pip
sudo pip install RPIO

To use the script run

sudo python py_pan_tilt_control.py

The script will then sit in a loop, waiting for one of 3 commands. A ‘p’ will move the pan servo, ‘t’ will move the tilt servo, and ‘e’ will run an exploration animation, scanning the pan/tilt head back and forth. You can specify either an angle in degrees, or a pulse width in μs for the ‘p’ and ‘t’ commands by putting a number straight after them (no space). The program treats any number less than 500 as an angle in degrees, and all other numbers and a pulse width. As an example

: p90 - Will move the pan servo to 90 degrees
: p2000 - Will set the pan servo's pulse width to 2000μs
: t50 - Will set the tilt servo to 50 degrees
: e - Will the start the exploration animation

The script is a bit too long to explain in detail, but the important bit occurs on lines 144 to 151

#-----------------------------------------------------------
# Create ServoPWM instances to control the servos
panServoPWM = ServoPWM( PAN_PWM_PIN,
    minAnglePulseWidthPair=( 45.0, 1850 ),
    midAnglePulseWidthPair=( 90.0, 1400 ),
    maxAnglePulseWidthPair=( 135.0, 1000.0 ) )
tiltServoPWM = ServoPWM( TILT_PWM_PIN,
    minAnglePulseWidthPair=( 45.0, 1850 ),
    midAnglePulseWidthPair=( 90.0, 1500 ),
    maxAnglePulseWidthPair=( 180.0, 500.0 ) )

This allows you to specify 3 ( angle, pulse width ) pairs for each servo, that tell the script which pulse width corresponds to each angle. The script can then work out the pulse widths to use for other angles by using linear interpolation. The precise pulse widths will vary from servo to servo, so you should experiment with different pulse widths to get your servos turning accurately, and then adjust the ( angle, pulse width ) pairs accordingly.

Once you have your servo motors turning accurately, you can then finish assembling your pan/tilt kit. This approach takes much less time than hurriedly assembling the pan/tilt kit when you first get it, and then having to take it apart again to get it moving accurately, which was our initial approach. :)

In order to hold our pan/tilt kit steady as it moved, we mounted the complete circuit onto a bit of foamboard.

Assembled Circuit

Assembled Circuit

Taking Things Further

Pan/Tilt Kit with Camera

With the pan/tilt kit up and running, you can now mount sensors on it, and give them a much wider field of view than they would have if they were just mounted statically. The obvious sensor to mount for the Raspberry Pi is the camera, and we’ll hopefully have a look at using the camera with the pan/tilt kit in a future post.

 

33 thoughts on “Using the Dagu Pan/Tilt Kit with the Raspberry Pi

  1. Pingback: Using the Dagu Pan/Tilt kit with the #RaspberryPi | Raspberry PiPod

  2. roberto

    hello I have successfully installed the 2 servos.
    but the problem in the communication command line.
    I downloaded and installed
    sudo apt-get install python-dev python-pip
    sudo pip install RPIO
    pyton and scripts.

    after launching shell writes:
    using hardware PWM
    PW increments: 10us
    Initializing channel 0 …..

    but there is no movement.
    you need to change python script?
    thanks support
    roberto

    Reply
    1. Alan Post author

      Hi Roberto,

      You shouldn’t need to change the script, some things to look at are

      * What commands did you try? The ‘e’ explore command is alwats good
      * Are you using the mini servos and pan tilt kit we use here? Bigger servos will need an external battery pack
      * Can you check to make sure that you’ve attached the servo signal lines to the correct GPIO pins
      * If you’ve got access to an ossiloscope then you should be able to see the PWM signals to control the servos on the GPIO pins

      Let me know how you get on.

      Regards

      Alan

      Reply
  3. roberto

    I could move the servo, I need to program servo pan automatically with little or modify existing scripts.
    I would like to make panoramic photos automatically with raspicam with insertion script in crontab.
    with the actual script pan movement is manual but need to set angle automatically.
    are not practical in python.
    thanks for the support.
    roberto

    Reply
    1. Alan Post author

      Hi Roberto,

      You can’t do that with the script provided here. But you can modify it a bit to do what you want.

      Delete everything from line 158 (above the try statement) down

      Replace it with the following

      panServoPWM.setCommand( 50 ) # Move pan to 50 degrees
      tiltServoPWM.setCommand( 120 ) # Move tilt to 120 degrees

      time.sleep( 0.05 ) # Give time for servos to move (this may not be needed)

      RPIO.PWM.clear_channel_gpio( PWM_DMA_CHANNEL, panServoPWM.pwmPin )
      RPIO.PWM.clear_channel_gpio( PWM_DMA_CHANNEL, tiltServoPWM.pwmPin )

      RPIO.PWM.cleanup()
      RPIO.cleanup()

      That should set the servos to 50 degrees and 120 degrees. If you use google for a bit you should be able to work out how to use command line arguments to set the pan and tilt angles.

      Regards

      Alan

      Reply
  4. Ryan

    Hi, great value kit, although a little fiddly to assemble for the novice (i.e. me!).

    I have everything ready, centered and working – to a point. Unfortunately whilst the script works very well, the Pi freezes during or just after a random number of servo moves. I tried putting 1k resistors on the control lines but this doesn’t help.

    Can you advise at all?

    p.s. was very impressed that you guys added the mountings I needed but didn’t order as the site showed them to be out of stock (I guess they just arrived in the nick of time!) – fantastic customer service <3

    Reply
    1. Alan Post author

      Hi Ryan,

      This sounds a bit like a power issue. What type of batteries are you using to run your robot?

      Cheers

      Alan

      Reply
      1. Ryan

        Hi Alan,

        I’m just following the above tutorial, so I only have the two servos hooked into the Pi’s 5v/Ground lines.

        Hope you can help!

        Reply
        1. Alan Post author

          Hi Ryan,

          Apologies, I’m an idiot. I only looked at your commment in the WordPress console and just assumed that you were following our tutorial for building a Pi robot (which often has issues if good batteries aren’t used). :P I should have read the post title properly. :)

          However, in your case, power could still be an issue. The servo motors on the pan/tilt kit are quite small so the Pi can usually provide enough current. But often with servos, you need to power them with a separate battery pack. I’m assuming that you’re running your Pi from a wall adapter, how many amps is it rated to provide? I find that I can power a Model B Pi, wifi dongle and the pan/tilt kit from a 1.2A wall adapter. Also, if you have a spare battery pack lying around then you could try powering the servos from that (as shown in the diagram entitled ‘External Servo Power’) to see if that improves things.

          Regards

          Alan

          Reply
  5. Ryan

    No worries Alan :D

    I tried the Pi with a wall adapter, and same problem. Additionally, the problem happened both when issuing rapid commands (such as having mouse position drive servo position), and issuing sparse commands (such as command-line mode.

    The problem can be solved by switching to an external power supply (or presumably, adding something between the Pi’s vcc pin and the servos? Can you advise?). As I say I’m a total electronics newb so I’m not sure exactly what the problem was other than it doesn’t happen when you draw power from an external source. Perhaps it tries drawing too much power and overwhelms the Pi?

    Thanks for your continued speedy replies, will definitely be placing further orders :)

    p.s. the servos “buzz” when maintaining position, I assume that’s normal operation…

    Reply
    1. Alan Post author

      Hi Ryan,

      If the problem can be solved by switching to external power for the servo, then it defintely sounds like your wall adapter can’t deliver enough current. Basically if the current drawn by the servos gets too high then the voltage supplied to the Pi will drop, and it will brownout, shutting down and restarting. The current draw from the servos will increase if they have to change direction quickly or if they’re stalled (i.e. if you stop them from moving with your hand). The wall adapter I tend to use is this one from RS components so it may be worth getting one for your Pi projects.

      As for the servo buzz, that is fairly normal for servo motors I’m afraid. Basically the motor control electronics inside the servo are continually seeking back and forth past the target angle, although hopefully you shouldn’t see too much judder. It may be possible to turn off the PWM signal to the servo once you think its reached its target angle which should stop the buzz. But then the servo wouldn’t be held in place and could be turned by hand.

      Regards

      Alan

      Reply
    1. Alan Post author

      Sorry for the delayed reply.

      This sounds a bit odd. Perhaps a bug that’s crept in due to a change in a recent version of Raspbian. Could you maybe try running the script on an older version of Raspbian or one of our older SD card images?

      I’ll hopefully have some time to look at this later in the week so that I can check it’s still working on our Pis.

      Regards

      Alan

      Reply
  6. Mark Routledge

    Dear All,

    Loving the kit, learning about it all the time. A bit fiddly to setup for my fat fingers as initially I forgot to add the washers!

    As a point of reference that might help people. I tried this initially on a Raspberry Pi B+, had lots of lock ups and reboots, I figured a power issue. Tried the EXACT same setup on a ‘B’ model, (same GPIO Pins etc) with the same supply and the script worked fine. So if using a Raspberry Pi B+ I would advise using an external power source. I found using a power supply from a crappy powered hub I bought provides a reasonable 5V to use. (Just share the common ground!)

    Reply
    1. Alan Post author

      Hi all,

      I finally got a chance to test this tutorial with the new Model B+ this weekend, but didn’t get any lockups. The steps I took were

      • I started with the latest version of the Dawn Robotics SD Card image available here (2014-07-18). This is basically the June 2014 version of Raspbian with a lot of our open source software installed.
      • I installed RPIO and the pi_pan_tilt_control script as detailed in the tutorial.
      • Probably most importantly, I used this 1.2A power supply from RS Components

      With this setup I was able to run the control script and didn’t get any lock ups at all.

      It’s interesting that you were able to run the servos from the Pi on a Model B, but not on a B+ Mark, as in theory the Model B+ consumes less power, and so should have more to spare for the servos. :P Do you have a link to the wall adapter you used? As if it’s not too much I might buy one to check it out.

      Regards

      Alan

      Reply
      1. Mark Routledge

        Alan,

        I’m actually powering my Pi using a Pi-Hub by Pimoroni – http://shop.pimoroni.com/products/pihub

        It claims to be rated at 3000 mA.

        At the time nothing else other than keyboard and mouse were connected to it. I agree the results were not as expected.

        Thanks for the response.

        Mark

        Reply
  7. Steve

    I would like to say thanks for an excellent service Alan. I ordered the Pan/Tilt kit on a Sunday evening and it arrived Tuesday morning, the neatest packaged box I think I’ve ever received! I bet your Xmas parcels look great!!
    I’m also very pleased with the Pan/Tilt, requires some patience to put together but does ‘what it says on the tin’ once complete.

    Reply
    1. Alan Post author

      Cheers for the kind comments Steve. :) Can’t take the credit for the neat packaging, that’s my colleague Martin, who helps with packing and shipping the orders. But yes, he does take great pride in his work. :)

      Reply
  8. Sooraj

    Hi,

    I recently received this kit , thank you for the prompt delivery.
    I have a robot set up from 4tronix (http://4tronix.co.uk/store/index.php?rt=product/product&product_id=340) and wanted to add the servos to part of the kit. The controller I have plugged it to is MICROCON (http://4tronix.co.uk/store/index.php?rt=product/product&product_id=262). However it doesn’t recognise the GPIO number I use.. and get stuck on

    Using hardware: PWM
    PW increments: 10us
    Initializing channel 0…
    :

    when running the sample code.

    Could you please advise me what I can do ?

    Thank you.

    Reply
    1. Alan Post author

      Hi Sooraj,

      Thanks for your order. I’m afraid I don’t have any experience with the MICROCON so can’t give any specific advice. The servos in the pan/tilt kit are standard RC servo motors so should work fine with it. Probably best if you email 4tronix directly for advice. Gareth Davies, head of 4tronix is very friendly. :)

      Regards

      Alan

      Reply
  9. Dennis

    I guess that the ‘torque’ properties of these small servos denotes what I am looking for, but as mechanics incompetent I wondered whether it was easier to ask the question …. what is the maximum weight these small servos are capable of supporting whilst still performing without stress?

    My reason for asking this is that I have the idea of mounting a new Pi A+ plus camera module onto the top servo, thus eliminating any problems with camera ribbon and GPIO lead drag or fowling.

    Cheers :-)

    Reply
    1. Alan Post author

      Hi Dennis,

      It might be possible to get this to work, but I would advise getting some stronger servos (if possible with metal gears). Very rough calculations here, but if something was mounted on the metal bracket then it’ll be about 4cm away from the motor axis. At 6V the motor would stall when trying to move a weight of approximately 1.4kg/4 = 0.35kg. Now the weight of an A+ (23g) and a camera (3g) looks well inside this, but if you were to move the motor quickly then at some point the motor may have to counteract the Pi’s weight, and its inertia from moving at speed. If the combined force is too high then you may strip the servo gears.

      Regards

      Alan

      Reply
      1. Dennis

        thanks for that Alan. Since I want to try and keep the voltage supply coming from the pi, it’s either not to do it or risk it :-)

        Reply
    1. Alan Post author

      Hi Bill,

      Sorry for the delayed reply.

      The Mini Driver is Arduino compatible, so can be used with the Arduino IDE, which should be installed by default on your Pi, or can be downloaded for Windows etc. We have a blog post for getting started programming the Mini Driver here. Also, the Arduino IDE comes with lots of demo programs, including a Servo example which can be found by selecting File > Examples > Servo > Sweep.

      So to answer your question, you’d need to write a sketch for the Mini Driver to control the servos, and then if needed, use serial communication to send commands from the Pi.

      Hope that helps.

      Regards

      Alan

      Reply
  10. Chris

    Hi Alan,

    I really like the setup for the pan/tilt camera here. Would it be possible to extend this setup to use a 2-axis joystick for input (controlling the camera, eg http://www.adafruit.com/products/512) with the necessary parts and code?

    I’m guessing a ADC would need to be used to convert the analog signal to digital.

    The end result would be that the user did not have to type any commands but could use the joystick to control the servos.

    Thanks,

    Chris

    Reply
    1. Alan Post author

      Hi Chris,

      Yes, this sounds like it would be a very cool addition to the project.

      You’re right that you’d need an ADC, which the Raspberry Pi currently lacks. We are hoping to stock an ADC for the Pi soon, but in the meantime a good alternative might be an Arduino compatible such as the Mini Driver board we sell. I think that adding an Arduino to a Raspberry Pi project can be a really good option as it can be much easier to hook up sensors (analog and 5V) to an Arduino, and you have a little bit of onboard processing power that can be used to monitor sensors and control motors in realtime whilst the Pi gets on with more computationally intensive tasks.

      Regards

      Alan

      Reply
      1. Chris

        Thanks for the reply.

        I think I’ll have to wait until the ADC is available since I’d like to avoid using an Arduino on top. I’m planning on deploying the system where there is no mains power and so it would need to run on a (big) battery. The smaller the power consumption will be the better.

        Chris

        Reply
  11. Fred

    Hi,

    I found your exemple very helpful but i have a problem. I don’t understand how to get angle and pulse width.

    Regards

    Reply
    1. Alan Post author

      Hi Fred,

      Sorry for the delayed reply. The exact relationship between angle and pulse width will vary a bit depending upon which brand of servo you’re using. In general the correspondence will usually be 1000us = 0 degrees, 1500us = 90 degrees, and 2000us = 180 degrees, although you’ll need to experiment to find the exact relationship for your servos, and also you may find that some servos don’t go all the way left or right. This article on the Society of Robots website gives a good explanation of servos and PWM signals. Although they quote times in millisections (ms) where 1 ms = 1000us.

      Hope that helps.

      Regards

      Alan

      Reply
      1. Fred

        Hi Alan,

        Thanks for the reply.

        I understand a little better the relationship between angles and pulse.

        Regards.

        Reply
  12. Roddy

    I have a Pi2 Model B and I’m going round in circles trying to get this to work (I have previously had it working on a model B Pi, which has sadly expired).
    The error message I’m getting is :

    Traceback (most recent call last):
    File "pi_pan_tilt_control.py", line 4, in
    import RPIO
    File "/usr/local/lib/python2.7/dist-packages/RPIO/__init__.py", line 115, in
    import RPIO._GPIO as _GPIO
    SystemError: This module can only be run on a Raspberry Pi!

    I’ve tried advice which uses apt-get to purge “python{,3}-rpi.gpio” and then to install it again, but to no avail. Can anyone advise if the script works on a Pi2 and if not how might I use the pan and tilt module on my Pi2

    (base install is 2015-05-05-raspbian-wheezy)

    Thanks
    Roddy

    Reply

Leave a Reply to Fred Cancel reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>