Running 1000 Servo Motors with a Raspberry Pi via i2c and pca9585
On Wednesday I received a package with some electronic parts.
- a servo moto sg90 180degree
- white breadboard to connect some electric components
- a gpio extender, to put all raspberry pi GPIO pins on the breadboard
- the pca9685, a 16 channel controller for servos via the I2C protocol. I got two one shielded and one not shielded?
I think it is really cool how cheap these components are. but of cause, for an awesome project, you likely need lots and lots of them.
On Thurseday, I plugged the components together with a tutorial and a schematics picture found online.
- enabling I2C on the raspberry pi
- starting a node.js project(of cause I want to control devices via node.js)
- installing the
i2c-bus
andpca9685
driver module.
Follow some examples but could not get the servo motor to spin.
Now this saturday, I finally made it move and I am a little proud to have found my problem. I was debugging but could not find the issue, there was no error in node, (there have been errors but I fixed them all with simple debugging). I try some different parameter
- change the i2c device address, (that caused an error showing the correct one was already used.)
- use all the different methods that the libs had to offer, but when looking into the modules code, most where just a variation of the same.
- searching all over my apartment, but could not find my small electric multimeter.
- the controller show it had power, and double checked that all cables are right.
- checking online for some more descriptions, I found that some add an extra power supply 5v to the controller. But i did not have one. some use a 4xAA battery tray, others,a dedicated breadboard power supply. I have neither.
- knowing that USB is 5v, I gave this a try. Cutting off the end of an old micro usb cable. online was described that usually red and black cable are for power 5v and ground. After un isolating and connecting the cable, the servo finally moved.
- sitting on the ground with notebook, starting the program seeing the motor move, i feel happy like a child.
So, here is the code and the resources that finally allow me to move the servo:
1 | var i2cBus = require("i2c-bus"); |
In this code the two used libs are imported, then initialized and doStuff
is where we can move the motor as we want using the pwm.setPulseLength
method. In this case I move the motor back and forward every two seconds. The min and max position values for the sg90
motor are 400 and 2600. I just try different values and looked at what value the motor still moved.