LEGO WeDo 2.0

LEGO WeDo 2.0) is a simple LEGO Bluetooth robotics platform with two ports for input/output.

It no longer appears to be sold by LEGO but Chinese clones are readily available for around £15 (including a motor and 2 sensors).

How to use

All you need is some code like this:

var wedo;
require("WeDo2").connect().then(w => {
  wedo=w;
  wedo.on("port", (port,dev) => print("Port",port,dev));
  wedo.on("sensor", (port,dev) => print("Sensor",port,dev));
  wedo.on("button", (b) => {
    print("Button",b);
    if (b) { // if button set
      wedo.setMotor1(100).then(() => { // set motor 1 max forward
        setTimeout(function() { // in 1 second
          wedo.setMotor1(0); // set motor 1 stopped
        }, 1000);
      });
    }
  });
});

You can also connect to more than one WeDo device if you wish to. If you want to connect to specific devices you can use NRF.connect and then require("WeDo2").attachDevice(device) with the connected device.

Reference

The WeDo class itself contains the following:

exports.log = function (txt) { ... }

// Given a connected Bluetooth device, attach all services
WeDo.prototype.constructor = function (d) { ... }

WeDo.prototype.attach = function () { ... }

// Disconnect from the device
WeDo.prototype.disconnect = function () { ... }

/* Set the LED to a specific color:

  off:0
  pink:1,
  purple:2,
  blue:3,
  cyan:4,
  light green:5,
  green:6,
  yellow:7,
  orange:8,
  red:9,
  light blue:10
*/
WeDo.prototype.setLED = function (col) { ... }

// Set the LED to an RGB color - r/g/b all 0..255
WeDo.prototype.setRGB = function (r, g, b) { ... }

// Set the speed of Motor 1 from -100 ... 100
WeDo.prototype.setMotor1 = function (speed) { ... }

// Set the speed of Motor 2 from -100 ... 100
WeDo.prototype.setMotor2 = function (speed) { ... }

// Find a WeDo device and connect - returns a WeDo device via a promise
exports.connect = function () { ... }

// Given a connected Bluetooth device, attach all services - returns a WeDo device via a promise
exports.attachDevice = function (device) { ... }

All the methods return promises which resolve when the write is complete.

It also fires the following events:

/** emitted when the button is pressed or released */
wedo.on("button", function(pressed) { ... });

/** emitted when a port is plugged in or not (device = unknown/motor/distance/tilt) */
wedo.on("port", function(portNo, device) { ... });

/** emitted when data comes from a sensor */
wedo.on("sensor", function(portNo, value) { ... });

Buying

These appear to be available from a variety of places, but sadly the original LEGO version no longer seems to be made.

Note

Right now the tilt sensor doesn't appear to send any readings when it's connected. The distance sensor works fine though.

This page is auto-generated from GitHub. If you see any mistakes or have suggestions, please let us know.