Controlling servo motors on the #RaspberryPi

Better PWM on the Raspberry Pi

As part of my “SpacePi” Raspberry Pi astronomy project, I want to be able to control the orientation of the camera module remotely. I want to be able to sit away from the camera rig and control where it is pointing using a web interface and (optionally, and because I damn well can) via a Wii remote.

So, I’m currently doing research into how best to do this.

I’ve got the actual mechanism sorted by buying one of these: Dagu Pan/Tilt which comes with two of these motors. I’m planning on building some kind of LEGO base for it as I don’t fancy trying to balance it on the bottom of the servo! 🙂

The motors operate at a known rate at around 4.8v, which is perfect because that’s what comes off the Pi. Except I don’t think it’s safe to do this. I know the 3.3v rails have a max power draw of 50ma, but I think that the amps available on the 5V is just whatever is left after the Pi takes it’s ~700ma. That might mean I have enough amps left, but I don’t fancy trying this out on one of my beloved Pis! So that means an extra power supply for the servo motors. Okay, I can live with that. I’d rather not, you understand, so if anyone knows that it is safe to power from the Pi, please let me know!

Control of the servos is via PWM, aka Pulse Width Modulation. This is something that the Arduino does very well, but that the Raspberry Pi does less well. This is because the Arduino is what is known as a “real time system”, meaning that when it is running it’s program loop nothing else can use up the CPU. Literally, the only thing running is what you’ve told it to run. The Raspberry Pi, on the other hand, is not a real time system as Linux itself is always beavering away in the background taking up CPU cycles. However, I have come across some ways of getting the Pi to give the impression that it is a real time system and to do PWM ‘properly’.

So, some web pages to look at:

After looking at all these, I would have tried out the Python version first. I like Python, it’s my sort of language. But the author of PyZiPi has said that it’s not suitable for servo control.

If Python fails me, I’ll move onto C/C++ with PigPio first as it seems the simplest, but will probably end up using ServoBlaster and jumping through the required hoops to get it working. (There are all kinds of things mentioned like ‘userland’ which I’ve heard of and frightens me!).

The other thing to try, of course, is to try the WiringPi-Python library which means you can use the WiringPi library from within Python. Certainly seems as though it might work, as Gordon Henderson has hinted in the comments section of this blog page.

More soon! (Once I’ve got the pan/tilt servo rig in my hands)

4 comments for “Controlling servo motors on the #RaspberryPi

  1. You’ve missed my blog from today Mike. The Guzunty could be perfect for what you’re after. I’m using it for a project involving servos. (Only 90 degree movement at the moment, but that will be sorted I think).

    Don’t even think about running a servo directly from the Pi’s 5 Volt line. It won’t cope with it – not to mention the idea of running an inductive load from the Pi. Someone thought I was doing that with my direct servo control the other week (I was using a separate 5V supply) and shot me a disparaging message. 😉

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.