Getting started with Nodejs and Arduino UNO Tutorial

NodeBots Day is just a couple of weeks away, so I want to share what I’ve been doing with Node and Arduino in recent times.

I had no idea what Arduino even was until recently, I didn’t really understand it, or how to get started with it.

Note: this is an adapted version of the guide at  http://node-ardx.org/ – thanks to SparkFun and .:oomlout:.
I’m still quite a newbie at this, so do let me know if you see  anything that I can improve on or do differently.

Getting started with Arduino UNO

The first thing you notice about the UNO is that it’s really small.

It’s pretty much like a small version of the board that you’ve no doubt seen if you’ve ever taken a desktop or laptop computer apart.

Without going into the nitty gritty, the thing to note is that this UNO board can be plugged and controlled by a USB port on your computer.

You can literally cut a few lines of JavaScript, and make the UNO board do as you command (within reason), just with a USB cable!

To get started with these examples, you need to collect the following:

Hardware:

  • 1 x Arduino UNO
  • 1 x USB cable to plug into the UNO
  • 8 x 5mm LED (the lights)
  • 8 x 330 ohm resistor (orange-orange-brown)
  • 10 x jumper wires
  • 1 x Solderless Breadboard
Bits and Bobs - (you wont need all of this)

Bits and Bobs – (you wont need all of this)

Software:

 

Set up the foundations – by loading Firmata onto your Arduino UNO

  1. Download Arduino IDE
  2. Connect your Arduino UNO via USB to your computer
  3. Launch Arduino IDE and open the Firmata sketch via the menu: File > Examples > Firmata > StandardFirmata
  4. Select your Arduino board type (e.g. Arduino UNO) via Tools > Board
  5. Select the port for your board via Tools > Serial Port > (the comm port of your Arduino)
  6. Upload the program by selecting File > Upload

At this point, you’ll notice some activity on the UNO. You can also have a look at some examples just to test the UNO out.

Test out the UNO using Arduino IDE 

  1. Set up the UNO with a single Led – see gallery above
  2. Open an example sketch via the menu: File > Examples > 01.Basics > Blink
  3. Select the Verify button
  4. Upload the program by selecting File > Upload

That’s it for Arduino IDE ! Now we move onto the Node side of things.

Set up Node and Johnny-five

  1. Download and Install Node if you don’t have it already.
  2. Open up a command line tool, I’m using Windows, so me that’s Windows Powershell
  3. Navigate to an appropriate directory location: cd C:\Users\Jane\Projects
  4. Clone the johnny-five project from GitHub:  git clone git://github.com/rwldrn/johnny-five.git 
  5. Install johnny-five: npm install johnny-five 
  6. Navigate into the examples folder:  cd C:\Users\Jane\Projects\johnny-five/eg 

Example 1: Set up a single led

  1. Start the program on pin 13:  node led (or node led.js) 
  2. Pat yourself on the back if your led blinks!
  3. Check out some of the options available for led:  led. 'tab key' will show you options – but look at the johnny-five docs for more details.
  4. Stop the led led.off()
  5. Start the ledled.on()

Check out the strope settings:

  1. First, we need to change from port 13 to a PWM port:  led.stop() or just Ctrl+C twice  pull out the USB and change the wire from 13 to 3.
  2. Start Node on port 3: node led 3 
  3. Tell the program to strobe/blink at 300 milliseconds:  led.strobe(300) 

Example 2: Set up 8 leds

  • 2 pin header x 4
  • 5mm LED x 8
  • 330 ohm resistor (orange-orange-brown) x 8
  • jumper wires

Follow the pictures below, or check out this site for the circuit layout

Lets get into it:

  1. Create a new .js file called “code-leds.js”.
  2. In code-led.js enter the following details
    var j5 = require("johnny-five");
    var board, leds=[], ledPins = [2,3,4,5,6,7,8,9];
    board = new j5.Board();
    board.on("ready", function() {
    
      // initialize LEDs using a for loop
      for (var i = 0; i < ledPins.length; i++){
          var myLed = new j5.Led(ledPins[i]);
          leds.push(myLed);
      }     
      function allOn(){
        for (var i = 0; i < leds.length; i++) {
            leds[i].on();
        }
      } 
      function allOff(){
        for (var i = 0; i < leds.length; i++) {
            leds[i].off();
        }
      }
      function oneAfterAnother() {
          var delay = 1;
          board.counter = 0;
          for (var i = 0; i < leds.length; i++) {
            var led = leds[i];
            board.wait(delay,function(){
                console.log(this.counter + " on");
                leds[this.counter].on();
            });
            board.wait(delay + 200,function(){
                console.log(this.counter + " off");
                leds[this.counter].off();
                this.counter = (this.counter + 1) % leds.length;
            });
            delay += 500;
          }
      }
      // allOn();
      // board.wait(1000,allOff);
      oneAfterAnother();
      board.loop(4500, oneAfterAnother);
    });
            
  3. Scenario 1 – turn on one led after another: node code-leds.js
  4. Scenario 2 – flash all of the leds on and off:

    swap the commented lines – from this

      // allOn();
      // board.wait(1000,allOff);
      oneAfterAnother();
      board.loop(4500, oneAfterAnother);
               

    To this – you can change 1000 to 3000 milliseconds to change the wait time

     
       allOn();
       board.wait(1000,allOff);
      //oneAfterAnother();
      //board.loop(4500, oneAfterAnother);
               

    run: node code-leds.js

 

I’m really excited to be mentoring at NodeBots Day this year, (on the 26th of July in Melbourne!). Hopefully we’ll start to see some more women entering this very cool area of tech!

Category: Node Robotics, Tech
Tags: ,

Let me know what you think

This site uses Akismet to reduce spam. Learn how your comment data is processed.