Raspberry Pi Zero W and Servos

For a long while I've had ideas about building a raspberry-pi controlled buggy.  I'd already bought the servos and PWM controller module and they were just sitting in a box labelled 'pi buggy'.

Today, I got them out and spent a few happy (and frustrating) hours getting them to work.

First up was setting up the PCA9685 servo board.  I didn't even know which pins were what.  I found this document, which came in very handy for that, but I still ended up needing pointers on how to enable i2c on the pi.

Then came the joy of setting up the Adafruit libraries.  This should have been a piece of cake, but for some reason the pi wouldn't pick up my home wireless  (Probably because my wifi router is old and pants).  So I had to transfer the library on a USB stick, then install it to find out I was missing dependencies. :(

So I ended up downloading Adafruit_Python_... PureIO, GPIO and PCA9685 and installing them (sudo python3 setup.py install) in that order.

Then, I ran the example script and the servos moved!  Hooray!

...except they wouldn't STOP moving.

I couldn't seem to find any frequency/pulse width combination which stopped the servos from moving.  It was meant to be a simple 1.5ms pulse width.  It took me a long while to figure out what the hell was going wrong.

When I carefully read the datasheet, I discovered that I needed to carefully alter the frequency to allow the pulse width (1.3-1.7ms with 1.5ms=stationary) AND a 20ms pause in each cycle.  Just setting the frequency to 50Hz wasn't good enough. >_<

So I wrote a little python function to take the microseconds I wanted, add 20000 to work out the total period, and then the number of 1/4096ths of that period I needed for the desired pulse width.

And the final problem was that they weren't calibrated.  So I set up the program to set them to 1.5ms pulse widths and twiddled the calibration potentiometer on the servo until it was stationary.

And suddenly it worked! Stepping through 1300 to 1700 microsecond pulses got the servos moving from one direction maximum to the other!

I would whine a bit more about how the manufacturer should do better, but I did go for super-cheap servos, so I've only got myself to blame.  If only I wasn't so damn tight-fisted I'd have a much easier time of things like this. :)


Comments

Popular posts from this blog

Micro:Bit and SPI display

DCS World with TrackIR under Ubuntu

Cardboard Mock-up of USB Joystick