Multiple LEDs using Johnny-Five

I’m controlling Spark wirelessly with Javascript via johnny-five ( and spark-io ( using VoodooSpark.

setup & goal: I have 5 LEDs connected to the pins D0 to D4. I am trying to get all the LEDs to light up.

problem: I seem to only be able to get the first LED to light up. If I write code to address each LED individually, such as

var led = new five.Led("D0");
var led = new five.Led("D1");
var led = new five.Led("D2");
var led = new five.Led("D3");
var led = new five.Led("D4");

only the first one will fire. So if I comment out the first line above (var led = new five.LED(“D0”)), only the LED on pin D1 will turn on.

I’m pretty new to all of this, so I’m sure I’m missing something. My actual code with a loop is below. I’d appreciate any help you can provide!

var five = require("../lib/johnny-five.js");
var Spark = require("spark-io");

var board = new five.Board({
    io: new Spark({
        token: "mytokengoeshere",
        deviceId: "mydeviceidgoeshere"

board.on("ready", function() {        
    for (var i = 0; i < 5; i++) {
        var pin = "D" + i;
        var led = new five.Led(pin);

Hey Elimon8,

In your first code example, you keep overwriting the variable led with each line. So if you were to do led.on();, led would only be set to whatever is on D4,

You had the right idea with the loop, I have modified it to this:

    board.on("ready", function() {
      var leds = [];
      for (var i = 0; i < 5; i++) {
          var pin = "D" + i;
      var array = new five.Led.Array(leds);

Now array.on(); will switch on all the LEDs. Or if you wanted to control a specific LED, you can now use array[1].on();, etc.

I’ve tested this with my Spark Core and it works. Hope this helps.

1 Like

hi juliancheal!

first of all, thank you for the help! I really appreciate it! :smile:

Unfortunately, pushing the LEDs to an array and turning the array on( ) as you did isn’t working on my board. Neither the array.on( ) or array[0].on( ) turn on any of the lights.

If I do my original loop, the first light (D0) turns on but not any light after that. It’s as if the request to trigger the first light is never ‘closed’ to allow other lights to trigger. I’m not sure what’s going on, but thanks again for the help!

Hmn, I’m not sure why yours isn’t working :frowning: Do you have a wiring diagram of how you’ve wired it up?

Unfortunately I don’t have a wiring diagram, but it’s just each LED directly connected to the pins D0 - D4, with all of them connected to common ground (I can probably provide a picture if it’ll help).

Also I just noticed my error in the first post! My code actually looks more like this (but still doesn’t work):

var led1 = new five.Led("D0");

var led2 = new five.Led("D1");

var led3 = new five.Led("D2");

var led4 = new five.Led("D3");

var led5 = new five.Led("D4");

If I do each LED individually they work. So if I do

var led = new five.Led("D0");

it works to light up the first LED. Then I change the code to read

var led = new five.Led("D1");
led.on( );

and that also works for the second LED. I can keep doing that for each LED individually and they all work, so my connections are working and my LEDs aren’t burned out.

If I try to run all the lines together though, it doesn’t work properly.