This will be a fairly short post, and it’ll continue with the theme of using servos with the Raspberry Pi 3. Previously I’ve been trying to drive a single small servo from my Raspberry Pi – it has been powerful enough to drive this, but if I was to add more than one servo, I’d expect to see odd behaviour, such as brown-outs.

The solution to this is to use a servo driver for the Raspberry Pi, on a kind of shield known as a “Hat” (which is an abbreviation for “Hardware Attached on Top“).

I chose to use the Adafruit servo driver. This connects to the GPIO pins, and provides an alternative set of output pins and sockets. It’s also driven by its own power supply, which is strong enough to power a number of servos.

My servo hat arrived in parts which needed to be soldered together.

WP_20160514_16_18_14_Pro

Adafruit supply a helpful C# solution to get you up and running – this includes a sample project, and a library to assist with PWM control. Interestingly, Adafruit use a frequency of 60Hz in their servo sample (rather than what I thought was the more usual 50Hz).

After soldering the parts to the board, I secured it to the Raspberry Pi 3, and connected four 9g Tower Pro servos.

WP_20160517_22_10_21_Pro

After that, I connected it to my PC by USB, my router by a CAT-5 cable, and finally I connected an external 6v power supply to the Servo Hat.

I had to remember to change my Raspberry Pi 3’s settings – I had previously configured the Default Controller Driver in the web interface Devices tab to be the Direct Memory Mapped Driver, but for this hat to work I needed to convert it back to the standard Inbox Driver.

screenshot.1462663123

I wanted to connect 4 servos to the servo hat, so my code looked like the code below. This causes 4 servos to wipe from side to side for 10 seconds, with a delay of 1 second between wipes:

using (var hat = new Adafruit.Pwm.PwmController())
{
    DateTime timeout = DateTime.Now.AddSeconds(10);
    hat.SetDesiredFrequency(60);
    while (timeout >= DateTime.Now)
    {
        hat.SetPulseParameters(0, 290false);
        hat.SetPulseParameters(1290false);
        hat.SetPulseParameters(2, 290false);
        hat.SetPulseParameters(3290false);
 
        Task.Delay(TimeSpan.FromSeconds(1)).Wait();
 
        hat.SetPulseParameters(3530false);
        hat.SetPulseParameters(2530false);
        hat.SetPulseParameters(1530false);
        hat.SetPulseParameters(0530false);
 
        Task.Delay(TimeSpan.FromSeconds(1)).Wait();
    }
}

That’s it – I had expected it to be more complex, but I literally cloned the code from here, compiled it and deployed it to my Pi 3, and it just worked.

Next time, I’m going to blog with Part 5 of my series about building a robotic hand – I now have the ability to control 4 servos using C#, so hopefully I’ll be able to get 4 fingers on the hand working.