Close

We Get Signal

A project log for Servo Controller

Drive a dozen or more servos over serial or I²C, on the cheap

Radomir Dopieralski 01/12/2016 at 21:440 Comments

OK, so that went quick. I just simply wrote an I²C receiving function using the Wire library, and we have a functional servo controller for 18 servos. The code is all in the repository, before uploading it with Arduino IDE you should set the desired I²C address. Of course, you can have multiple such controllers, each with a different address, each controlling up to 18 servos!

To set servos to specific positions, just send block data to the address of the first servo, two bytes per servo, denoting the duty cycle in µs. For example, on a Raspberry Pi you would do:

import struct
import smbus

bus = smbus.SMBus(1)
address = 0x09
servo = 4
bus.write_i2c_block_data(address, servo, [ord(c) for c in struct.pack("<H", 1500)])

You can even send the positions of up to 16 servos at once:

bus.write_i2c_block_data(address, servo, [ord(c) for c in struct.pack("<HHH", 1500, 1000, 2400)])

And that's it. You can't read those positions, maybe I will add that later on, but honestly, why would you want to read them. To power down a servo, set the duty cycle to 0.

Discussions