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 and pca9685 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.

pca9685 To Pi photo

So, here is the code and the resources that finally allow me to move the servo:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
var i2cBus = require("i2c-bus");
var Pca9685Driver = require("pca9685").Pca9685Driver;

const pwm = new Pca9685Driver({
i2c: i2cBus.openSync(1),
address: 0x40,
frequency: 50,
debug: false
}, function(err) {
if (err) {

console.error("Error initializing PCA9685");
console.log(err)
process.exit(-1);
}
console.log("Initialization done");

doStuff(channel)
});

function doStuff (channel){
var timer;
const pulseLengths = [ 400, 2600 ];//400 // 2600
var pos=0;
function servoLoop() {
setTimeout(servoLoop, 2000);
pos+=1;
const value = pulseLengths[pos%pulseLengths.length]
pwm.setPulseLength(channel,value);
console.log(channel,pos, value)
}
servoLoop();
}

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.

resources:

back to developer log