Introduction: IoT Workshop: Lab 1 - Digital Output

About: Director, Strategic Engagements, DX-TED, Microsoft

In this lab you will wire-up an Arduino with a single LED and a 330 Ohm resistor then code and run a small Node.js/Johnny-Five application that loops and writes sequential on/off commands to make the LED blink.

If you haven't already done so, please follow the instructions in the Prep-Work module of this Workshop.

In this lab you will use one of the digital output pins to send a signal to an LED. You will use digital pin 13, which also has an LED on the board connected to it (so you will be able to see it turn on and off even if you mis-wire something). While this lesson could be done using only the onboard LED, one of the objectives of this lab is to familiarize you with connecting input and output devices (an LED is an output device) to the board and controlling them with software.

Step 1: Bill of Materials

What you will need:

*For this lesson series you are using an Arduino Yun. The reason for using this vs. other less expensive Arduino boards is because in future lessons you will make use of the fact that the Arduiono Yuin has on-board Wi-Fi and a Linux distribution. Neither of those capabilities are needed for this lesson, so if you have a different Arduino board (e.g. an Arduino Uno) you can use it. The SparkFun Inventor's Kit (for Arduino Uno) is a good kit with lots of parts (LEDs, resistors, servos, etc.), but it ships with an Arduino Uno instead of the Yun (the Uno doesn't have onboard Wi-Fi or the Linux distribution we will use in later lessons).

Step 2: Upload the Standard Firmata to Your Arduino

For this lab series you will use tools that require your Arduino to have the Standard Firmata loaded. Fortunately, this is easy to do using the Arduino IDE.

Connect your Arduino board via USB to your computer.

Open the Arduino IDE.

Using the Tools menu, select Board and make sure Arduino Yun is selected (assuming you are using the Arduino Yun or Linino ONE as recommended for this lab series).

Using the Tools menu, select Port and make sure your board is selected on a COM port.

Using the File menu, select Examples > Firmata > StandardFirmata. This will open a new Arduino IDE window with the code for the Standard Firmata loaded.

Using the File menu, select Upload. This will upload the firmware to your Arduino (you should see the lights on the Arduino blink while this happens).

Close all Arduino IDE windows.

Step 3: Wiring the LED

Next you need to wire up the Arduino board so that it can send on and off commands to the LED. You can wire your board according to the diagram (wire colors don't matter, but help with identification of purpose).

LED

Insert a LED into the breadboard as shown in the diagram. For reference, an LED has one lead that is longer than the other. The longer lead is the positive (+) lead, and the shorter lead is the negative (-) lead.

Wires

Connect the wires as shown in the diagram:

  • Black: Connect the GND pin to the negative (-) side-rail on the breadboard.

  • Yellow: Connect pin 13 to the breadboard in the same row as the positive (longer) lead from the LED.

Resistor

Connect a 330-Ohm resistor from the row that the negative (shorter) lead from the LED is in to the negative (-) side-rail.

Step 4: Write the Code

Since we are using Node.js and Johnny-Five for this lab we can take advantage of the dependency management capabilities that Node.js provides. We need to let our application know that it has a dependency on the Johnny-Five framework so that when the application is prepared for execution, it can fetch the required dependencies for us. In Node.js this is done with a package.json file. This file provides some basic meta-data about the application, including any dependencies on packages that can be retrieved using the Node Package Manager (NPM).

Using your favorite/preferred text/code editor (I prefer Visual Studio Code), create a file in your Workshop folder named package.json and add the following:

{
  "name": "labs",
  "repository": {
    "type": "git",
    "url": ""
  },
  "version": "0.1.0",
  "private": "true",
  "dependencies": {
    "johnny-five": "^0.8.0"
  }
}

With the package.json file created you can use NPM to pull down the necessary Node modules. Open a terminal window (Mac OS X) or Node.js command prompt (Windows) and execute the following commands (replace c:\Development\IoTLabs with the path that leads to your Workshop folder):

cd C:\Development\IoTLabs
npm install

Next you will create the application code to make the LED turn on and off.

Create another file in the same directory named lab001.js.

The first thing you need to do is define the objects you will be working with in the application. The three things that matter are a Johnny-Five framework object, an object to represent the Arduino, and the output pin the LED will be connected to.

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

Now that the objects are created, you can get to the meat of the application. Johnny-Five provides a board initialization construct that makes a callback when the board is on and initialized (booted). In the callback function is where the application code executes.

Johnny-Five provide a collection of objects that represent the board, the pins on the board, and various types of sensors and devices that could be connected to the board. For this lab you are going to write code that is fairly true to the base Arduino C programming model (we'll get into what Johnny-Five provides us later). This will help you understand some of the basic concepts for how an Arduino Yun.

In the following code you will create a callback function that is invoked when the Arduino is initialized and ready (this is a Johnny Five concept). You will set digital pin 13 (the LEDPIN variable above) as an output pin (vs. an input pin), meaning the application is expecting to send voltage out from the pin as opposed to read the voltage coming in to the pin. Then you will create a loop that runs once per second and inside that loop you will write out to the pin either LOW or HIGH voltage. Since pin 13 is a digital pin, its only options are 0 and 1 - in the world of Arduino that is LOW and HIGH. When you send 0 (or LOW) to the pin, that is equivalent to off (sending no voltage). When you send 1 (or HIGH) to the pin that is equivalent to on (sending full voltage).

board.on("ready", function(){
  // Set pin 13 to OUTPUT mode
  this.pinMode(LEDPIN, five.Pin.OUTPUT);
  // Create a loop to "flash/blink/strobe" an led
  var val = 0;
  this.loop( 1000, function() {
    this.digitalWrite(LEDPIN, (val = val ? 0 : 1));
  });
});

Johnny-Five actually has an object model for an LED and we could also have simply done the following, but I wanted you to see how the digitalWrite() function works before abstracting it away.

board.on("ready", function() {
  var led = new five.Led(LEDPIN);
  led.blink(1000);
});

Step 5: Run the App

When you run the application is will execute on your computer, and thanks to Johnny-Five, it will connect with your board and work directly with it. Basically, instead of running this Node.js application on the Linux distribution on the board, you are running it on your computer while you test it out (don't worry, we will deploy an application onto the Linux distribution and enable your board to run without being tethered to your computer soon enough).

Open a terminal window (Mac OS X) or Node.js command prompt (Windows) and execute the following commands (replace c:\Development\IoTLabs with the path that leads to your Workshop folder):

cd C:\Development\IoTLabs
node lab001.js

You should see some lights on the board blink a little as the app is initialized, and then the on-board LED and the green LED you connected should start blinking in unison at one blink per second.

Step 6: Conclusion and Next Steps

In this lab you learned how to write a Node.js/Johnny-Five application that writes LOW and HIGH signals to a digital pin (designated for output) to make an LED blink. In itself this may not be very exciting, but the core concept is necessary - writing to a digital output pin.

In the next lab you will learn how to read voltage coming in on an analog pin, and you will learn how to use a voltage divider to capture the variable resistance provided by a photoresistor.

Once you've completed this lab, be sure to click the "I Made This" button at the top of the page. That helps me know how many people are working on this lab - the more people using the lab, the more lab modules I will create.
Next, go to Lab 2: Read an Analog Signal.