HomePi Friday › Pi Friday – May Edition

Pi Friday – May Edition

A very popular thing to do with a Pi is make some variety of robot; maybe a crawler or wheeled vehicle or a manipulator arm or perhaps something a bit wilder like a humaniod or even an eight armed pogo-stick-using nightmare from the depths of your id.

The common thread between almost all of these projects is a need to make model servos move around to drive those arms and legs in a suitably threatening manner. Today I thought I’d explain one good way of controlling servo positions using commonly available parts.

First, a word about how typical model servos actually work. Because of the history of model radio control and how we used to generate the radio signals and decode them, model servos use a Pulse Width Modulation or PWM signal.

That means that the position of the servo arm is set by measuring the width of a pulse sent out by the driver – typically a radio receiver built into your model plane, for example. The industry ‘standard’ is for a pulse to each servo roughly once every 20 milliSeconds, although more modern systems are moving to a roughly 10mSec cycle. If the pulse is ‘on’ for 1.5 mSec then the servo should move to the middle of its range; at 1.0 mSec it should be at one extreme and at 2.0 mSec it should be at the other extreme. Most servos move the arm clockwise as seen from the top as the pulse length increases but you can’t rely on that, so do test your servos! Again, more modern servo designs tend to allow a wider range, sometimes going as far as 0.5 mSec to 2.5 mSec. And again, test your servos to find out what they will actually allow.

The obvious question is “how do I make those weird pulses?”. You might be thinking that having some kind of timing loop in your code and tickling the Pi GPIO pins just the right way will be required – and in truth you can actually do that if you really want to. Raspbian is not a Real Time OS though and so that isn’t a very reliable way to move servos.

A nice simple approach is to use a PWM HAT and fortunately there are two similar and readily available options available: The Adafruit PWM Servo HAT and the SparkFun Pi Servo HAT. Both boards are based on the PCA9685 PWM chip. The PWM HATs handle the timing independent of the Pi, and rather usefully separate the Pi power from the Servo power as well. (Do NOT try to drive and power servos directly from the Pi GPIO pins or you risk letting out the magic smoke that powers all out electronics!)

Adafruit and SparkFun both provide tutorials, but more advanced users may want to consider trying the simple Linux Daemon I wrote. The Daemon can talk to the HAT and set up quite a few options and lets you write commands to a simple device file. To download and install this you need only go to the github repository https://github.com/timrowledge/pca9685servodaemon and download the code. The README that you will see describe the commands fairly comprehensively, along with what you must do to install the daemon.

Tags: ,

Leave a Reply