Skip to content
Divan Visagie edited this page Aug 17, 2013 · 9 revisions

The Servo class constructs objects that represent a single Servo attached to the physical board. This class is designed to work well with both Standard and Continuous motion servos.

Parameters

  • pin A Number or String address for the Servo pin (PWM).
var servo = new five.Servo(11);

Tinkerkit:

// Attached to "Output 0"
var servo = new five.Servo("O0");
  • options An object of property parameters.
Property Name Type Value(s) Description Required
pin Number, String Any PWM Pin The address of the pin the servo is attached to, ie. 11 or "O1" (if using TinkerKit) yes
range Array [ lower, upper ] The range of motion in degrees. Defaults to [0, 180] no
type String "standard", "continuous" The type of servo being created. Defaults to "standard" no
startAt Number Any number between 0-180 Degrees to initialize the servo at. no
center Boolean true or false Optionally center the servo on initialization. Defaults to `false` no
```js // Create a standard servo... // // - attached to pin 11 // - limited range of 45-135 degrees // var temp = new five.Sensor({ pin: "A0", freq: 250, threshold: 5 }); ```

Shape

{ 
  id: A user definable id value. Defaults to a generated uid
  pin: The pin address that the Servo is attached to
  range: The range of motion in degrees. Defaults to [0, 180]
  history: An array containing records of each movement 
  interval: A reference to the current interval, if one exists
  isMoving: A boolean flag, true when moving, false when still
  last: The last movement record. READONLY
}

Usage

Standard Servo

var five = require("johnny-five"), 
    board = new five.Board();

board.on("ready", function() {

  var servo = new five.Servo(11);

  // Sweep from 0-180 and repeat.
  servo.sweep();
});

Continuous Servo

var five = require("johnny-five"), 
    board = new five.Board();

board.on("ready", function() {

  var servo = new five.Servo({
    pin: 11, 
    type: "continuous"
  });

  // Clockwise, top speed.
  servo.cw(1);
});

API

  • move(degrees 0-180 [, lapse]) Move a servo horn to specified position in degrees, 0-180 (or whatever the current valid range is). Support for a lapse param is reserved.
var servo = new five.Servo(11);

// Set the horn to 90degrees
servo.move(90);
  • to(degrees 0-180 [, lapse]) Alias to move
var servo = new five.Servo(11);

// Set the horn to 90degrees
servo.to(90);
  • min() Set Servo to minimum degrees. Defaults to 0deg, respects explicit range.
var servo = new five.Servo(11);

// Set horn to 0degrees
servo.min();

Or

var servo = new five.Servo({
  pin: 11, 
  range: [ 45, 135 ]
});

// Set horn to 45degrees
servo.min();
  • max() Set Servo to maximum degrees. Defaults to 180deg, respects explicit range.
var servo = new five.Servo(11);

// Set horn to 135degrees
servo.max();

Or

var servo = new five.Servo({
  pin: 11, 
  range: [ 45, 135 ]
});

// Set horn to 135degrees
servo.max();
  • center() Set Servo to center point. Defaults to 90deg, respects explicit range.
var servo = new five.Servo(11);

// Set horn to 90degrees
servo.center();

Or

var servo = new five.Servo({
  pin: 11, 
  range: [ 40, 80 ]
});

// Set horn to 60degrees
servo.center();
  • sweep() Sweep the servo between min and max or provided range
var servo = new five.Servo(11);

// Repeated full range movement
servo.sweep();
  • stop() Stop a moving servo.
var servo = new five.Servo(11);

servo.stop();
  • cw(speed) Move a continuous servo clockwise at speed, 0-1
var servo = new five.Servo({
  pin: 11, 
  type: "continuous"
});

servo.cw(1);
  • ccw(speed) Move a continuous servo counter clockwise at speed, 0-1
var servo = new five.Servo({
  pin: 11, 
  type: "continuous"
});

servo.ccw(1);

Events

It's recommended to use a rotary potentiometer as the mechanism for determining servo movement.

Examples

Additional Information

Although servos can run on digital pins, this can sometimes cause issues. For this reason servos are forced to use only PWM under debug mode and will emit an error if used on a digital pin.

Servo debug option:

Property Name Type Value(s) Description Required
debug Boolean true or false Set servo to debug mode no
Clone this wiki locally