Make a directory inside your “tessel-code” folder called “servo”, change directory into that folder, and initialize a tessel project:
mkdir servo; cd servo; t2 init
Plug servo into port “1” on the module as shown.
-
+
S
Plug 5V adapter into the barrel jack on the servo module, then plug into wall power.
Plug the servo module into Tessel port A with the hexagon/icon side down and the electrical components on the top, then plug Tessel into your computer via USB.
Install by typing npm install servo-pca9685
into the command line.
Rename “index.js” to “servo.js” and replace the file’s contents with the following:
// Any copyright is dedicated to the Public Domain.
// http://creativecommons.org/publicdomain/zero/1.0/
/*********************************************
This servo module demo turns the servo around
1/10 of its full rotation every 500ms, then
resets it after 10 turns, reading out position
to the console at each movement.
*********************************************/
var tessel = require('tessel');
var servolib = require('servo-pca9685');
var servo = servolib.use(tessel.port['A']);
var servo1 = 1; // We have a servo plugged in at position 1
servo.on('ready', function () {
var position = 0; // Target position of the servo between 0 (min) and 1 (max).
// Set the minimum and maximum duty cycle for servo 1.
// If the servo doesn't move to its full extent or stalls out
// and gets hot, try tuning these values (0.05 and 0.12).
// Moving them towards each other = less movement range
// Moving them apart = more range, more likely to stall and burn out
servo.configure(servo1, 0.05, 0.12, function () {
setInterval(function () {
console.log('Position (in range 0-1):', position);
// Set servo #1 to position pos.
servo.move(servo1, position);
// Increment by 10% (~18 deg for a normal servo)
position += 0.1;
if (position > 1) {
position = 0; // Reset servo position
}
}, 500); // Every 500 milliseconds
});
});
Save the file.
In your command line, t2 run servo.js
Watch your servo move!
Bonus: Make the servo turn all the way to position 1 in one fell swoop, and then back to position 0.
To see what else you can do with the servo module, see the module docs here.
What else can you do with a servo module? Try a community-created project.
What are you making? Share your invention!
If you run into any issues you can check out the servo forums.