Introduction: IoT Workshop: Lab 2 - Reading an Analog Signal

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

In this lab you will use two resistors - a static resistor and a variable resistor - to create a voltage divider that enables you to effectively understand the intensity of light detected by a photoresistor - essentially a light meter. In the previous lesson you learned how to send OUTPUT and in this lesson you will learn to collect INPUT.

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

Step 1: Bill of Materials

What you will need:

Arduino Yun* (1)

Photoresistor (5528) (1)

10k-Ohm 1/4 Watt resistor (Brown-Black-Orange) (1)

*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: Wiring a Voltage Divider

The first step is to wire up the Arduino to read voltage as determined by the resistance created by the photoresistor. Wire your board according to the diagram (wire colors don't matter, but help with identification of purpose).

The A0-A5 pins on the board enable you to read from or write to analog sensors, such as photoresistors, knobs (potentiometers), and temperature sensors. Here is the description of the analog pins from the Arduino website:

The Arduino board contains a 6 channel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit.

A photoresistor, also known as light-dependent resistor (LDR) or a photocell, works by limiting the amount of voltage that passes through it based on the intensity of light detected. The resistance decreases as light input increases - in other words, the more light, the more voltage passes through the photoresistor.

In order to take advantage of the photoresistor you will create a voltage divider - a passive linear circuit that splits the input voltage amongst two or more components (similar to a Y-splitter).

To create the voltage divider needed for this lesson you will:

  • Connect the voltage from the 5-volt (input voltage) pin to a circuit (using a breadboard).
  • Connect the input voltage to a static resistor (10k Ohm).
  • Establish a voltage divider coming out of the static resistor:
    • One route to the analog pin (A0).
    • One route to a variable resistor (the photoresistor).
  • Completing the circuit out of the dynamic resistor to ground.

As the photoresistor increases its resistance (lower light intensity) more of the input voltage coming out of the 10k Ohm resistor is blocked and diverted to the A0 pin. That means that the less intense the light into the photoresistor the more resistance it creates, which in turn diverts more voltage to the A0 pin (the voltage has to go somewhere). Likewise, the more intense the light into the photoresistor, the less resistance it creates, which in turn means there is less voltage to divert to the A0 pin.

In short, the more voltage to the A0 pin, the darker it is.

Here are the specific wiring instructions (see the breadboard image attached to this lesson):


Insert a photoresistor into the breadboard as shown in the diagram.


Connect a 10k-Ohm resistor from one side of the photoresistor across a couple of rows.


Connect the wires as shown in the diagram:


  • Connect the 5V pin to the red/positive side-rail on the breadboard.
  • Connect the red/positive side-rail to the row where the resistor lead is connected but the photoresistor is not (this is the input voltage into the static resistor part of the voltage divider).


  • Connect the green wire from the other side of the static resistor (this should be in the same row as the static resistor lead and one of the photoresistor leads) to the A0 pin on the Arduino (this is one route of the voltage divider - the other route is through the photoresistor).


  • Connect the row holding the other lead from the photoresistor to the black/negative side-rail on the breadboard.
  • Connect the black/negative side-rail of the breadboard to the GND pin on the Arduino.

This completes the circuit.

Note: You could connect the 5V pin directly to the same row as the lone lead of the static resistor and the GND directly to the lead of the photoresistor, but I like building a habit of connecting the input voltage and ground pins from the Arduino to the side rails. This will come in handy in the future lessons.

Step 3: Writing the Code

For this lab we will create a new file named lab002.js in the same directory as we did in Lab 1. There are no additional dependencies, so we don't need to make any changes to the package.json file.

In the lab002.js file start by declaring the key objects, including a variable for the analog pin number you will use (A0 or 0).

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

Next, define the callback function in the Johnny-Five board initialization. For this lab you will use the analogRead() function, which takes the analog pin number in as input and calls a callback function when input is read off the pin. In the callback function, simply write the data to the console.

board.on("ready", function() {
    // read the input on analog pin 0:
    this.analogRead(analogPin, function(voltage) {
      console.log(voltage * (5.0 / 1024.0));

In this case, a value for the voltage coming in to the pin from the voltage divider is passed into the callback method. The value passed in is not the actual voltage, but rather a value from 0-1023 that represents the voltage. Since you are using a 5V power circuit you have to multiple the voltage value by 1024th of 5V (or 5.0 / 1024). The result is the actual voltage being read off of the analog pin.

Step 4: Run the Application

To run the application, plug the Linino ONE/Arduino Yun into your computer with the USB cable. Open a terminal window (Mac OS X) or Node.js command prompt (Windows) and execute the following commands (replace c:\Development\IoTWorkshop with the path that leads to your Workshop folder):

cd C:\Development\IoTWorkshop
node lab002.js

You should see some lights on the board blink a little as the app is initialized, and then you should see something like the following in the terminal/console window (the actual values will depend on how much light the photoresistor is receiving):

c:\Development\IoTLabs>node lab002.js
1429351937758 Device(s) COM3
1429351937782 Connected COM3
1429351942831 Repl Initialized
>> 1.69921875

While the application is running and you are seeing data in the console, try covering the photoresistor (thus decreasing the light and increasing the resistance from the photoresistor and pushing more voltage to pin A0) or shining a light on the photoresistor (thus increasing the light and decreasing the resistance from the photoresistor and allowing more voltage through to ground (effectively stealing voltage away from the A0 pin).

Press CTRL + C a couple of times to exit the program without closing the window.

Step 5: Conclusion and Next Steps

Congratulations! You have made your first device that responds to its environment, you learned about a voltage divider, how to read data from an input sensor, and how to use the serial monitor. In the next lab you will put together the two labs so far to make an LED that responds to the ambient light (and you will learn about Pulse-Width Modulation).

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 3: Controlling Output with Input.