Motor - lyzadanger/johnny-five GitHub Wiki

The Motor class constructs objects that represent a single Motor. The motor may attach to the physical board or a motor controller. The controller may be a third party shield or custom built motor controller. This class works well with both Directional and Non-Directional motors. It also works well with 2-pin or 3-pin controllers.

Parameters

  • pin A Number or String address for the Non-Directional Motor pin (PWM).
var motor = new five.Motor(9);
  • pins An array of 2 or 3 Numbers or String addresses for the Bi-Directional Motor pins.
// Two elements passed [pwm, dir]
var motor = new five.Motor([3, 12]);

or

// Three elements passed [pwm, dir, cdir]
var motor = new five.Motor([3, 12, 11]);
  • options An object of property parameters.
Property Name Type Value(s) Description Required
pins Object A valid pins object or pins array yes
current Object A valid Sensor options object*
  </td>
  <td>no</td>
</tr>
*See [Sensor](https://github.com/rwldrn/johnny-five/wiki/Sensor) for valid options on the current object ```js // Create a motor with... // // - pwm (speed) on pin 3 // - dir (direction) on pin 12 // - and brake on pin 11 // // ... with a current sensor... // // - that gets the value from pin "A0" // - every 250ms // - and scales the raw data to value between 0 and 2000 // var motor = new five.Motor({ pins: { pwm: 3, dir: 12, brake: 11 }, current: { pin: "A0", freq: 250, range: [0, 2000] } }); ```

Shape

{ 
  isOn: A boolean flag, true when motor is moving or braking, false when not READONLY
}

Usage

Non-Directional Motor

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

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

  var motor = new five.Motor(5);

  // Start the motor at maximum speed, wait 2 seconds and stop.
  motor.start(255);

});

Directional Motor

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

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

  var motor = new five.Motor([3, 12]);

  // Reverse the motor at maximum speed
  motor.reverse(255);

});

Directional Motor with Brake

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

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

  var motor = new five.Motor({
    pins: {
      pwm: 3,
      dir: 12,
      brake: 9
    }
  });

  motor.on("forward", function(err, timestamp) {
    // demonstrate braking after 5 seconds
    board.wait(5000, function() {
      motor.brake();
    });
  });

  motor.on("brake", function(err, timestamp) {
    // Release the brake after .1 seconds
    board.wait(100, function() {
      motor.stop();
    });
  });

  // Start the motor at maximum speed
  motor.forward(255);

});

Directional Motor with Current Sensing

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

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

  var motor = new five.Motor({
      pins: [3, 12]
    },
    current: {
      pin: "A0",
      freq: 250,
      threshold: 10
    }
  });

  // Log current mA every 250ms if that value has changed by 10 or more since the last log
  motor.current.scale([0, 3030]).on("change", function() {
    console.log("Motor A: " + this.value.toFixed(2) + "mA");
  });

  // Start the motor at maximum speed
  motor.forward(255);

});

API

  • forward(speed 0-255) Set a motor moving forward
  • fwd(speed 0-255) Alias to forward()
var motor = new five.Motor([11, 12]);

// Forward at half speed
motor.forward(128);
  • reverse(speed 0-255) Set a motor moving in reverse
  • rev(speed 0-255) Alias to reverse()
var motor = new five.Motor([11, 12]);

// Reverse at full speed
motor.reverse(255);
  • start([speed 0-255]) Set a motor moving in the current direction
var motor = new five.Motor([11, 12]);

// Forward at half speed
motor.forward(128);

// Stop
motor.stop();

// Resume forward at half speed
motor.start();

// Continue forward at full speed
motor.start(255);
  • stop() Let the motor coast to a stop
var motor = new five.Motor([11, 12]);

// Forward at full speed
motor.forward(255);

// Roll to stop
motor.stop();
  • brake() Force a motor to stop (as opposed to coasting). Please note that this only works on boards with a dedicated brake pin. Other boards and interfaces will simply coast.
var motor = new five.Motor([11, 12]);

// Forward at full speed
motor.forward(255);

// Stop fast
motor.brake();

board.wait(100, function() {
  motor.stop();
});
  • release() Release the brake and resume current speed and direction
var motor = new five.Motor([11, 12]);

// Forward at full speed
motor.forward(255);

// Stop fast
motor.brake();

// Wait five seconds and release the brake
board.wait(5000, function() {
  motor.release();
});

Examples

Additional Notes

The PWM pins on an Arduino Uno only output about 40mA. That is barely enough to power a humble hobby motor. You are going to need a motor controller between your Arduino and your motor(s) to deliver power from an external source. Most motor controllers are based on the H-Bridge circuit which uses a set of four switches to direct the voltage being sent through each pole of the motor. Different shields handle different input voltages and output currents. Use the Motor Control Shield Survey below to find a shield that works for you.

Forward and Reverse are Interchangeable

Keep in mind that "forward" and "reverse" are arbitrary labels. If your motor is turning in the wrong direction you can just switch the poles on the motor. Consider a robot with two motors connected directly to the drive wheels. For your bot to go forward, one should turn clockwise and the other should turn counter-clockwise. Switch the poles on one of those motors so that you can use forward() on both and have them work together.

Differences Between 2 and 3 pin Directional Motor Controllers

Controllers that use 2 pins instead of 3 are essentially the same. Both arrangements use one PWN pin to control speed. The switches on the H-Bridge work in pairs. With 3-pin controllers you control the state of each pair. With 2-pin controllers the pairs are toggled for you based on the state of that one digital pin.

Motor Control Shield Survey

This is by no means exhaustive

2 Pin

Manufacturer Name Motor A pins Motor B pins Operating Voltage(1) Max Current per Channel Stackable(2)
Arduino Motor Shield R3 pwm:3,
dir:12,
[brake:9,]
[current:A0]
pwm:11,
dir:13,
[brake:8,]
[current:A1]
7-12V 2A No
DF Robot 1A pwm:6,
dir:7
pwm:5,
dir:4
7 - 12V 1A No
DF Robot 2A pwm:6,
dir:7
pwm:5,
dir:4
4.8 - 35V 2A No
NKC Electronics Motor Control Shield Kit pwm:9,
dir:12
pwm:10,
dir:13
6 - 15V shared 1A No
Rugged Circuits Rugged Motor Driver pwm:3,
dir:12
pwm:11,
dir:13
8-30V 2.8A Yes
Rugged Circuits Basic Motor Driver pwm:3,
dir:12
pwm:11,
dir:13
8-30V 2A Yes
Sparkfun Ardumoto pwm:3,
dir:12
pwm:11,
dir:13
6 - 15V shared 2A No

3 Pin

Manufacturer Name Motor A pins Motor B pins Operating Voltage(1) Max Current Stackable(2)
Seeed Studios Motor Shield V1 pwm:9,
dir:8,
cdir: 11
pwm:10,
dir:12,
cdir: 13
6-15V 2A No
Freetronics Dual Channel H-Bridge Motor Driver Shield pwm:6,
dir:4,
cdir: 7
pwm:5,
dir:3,
cdir: 2
8-40V 2A No
  1. Beware of shared voltage, the shield may be able to handle higher voltages than your Arduino.
  2. Configurable indicates that the pins can be reconfigured so that you can stack multiple shields of this type or other shields that use the same pins.
⚠️ **GitHub.com Fallback** ⚠️