In this lesson 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 the 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.

What you will need:
(1) Arduino Yun*

(1) Photoresistor (5528)

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

*For this lesson series you are using an Arduino Yun. The reason for using the Yun (vs. other less expensive Arduino boards) is because in future lessons you will make use of the fact that the Yun has on-board Wi-Fi and a Linux distribution. Neither of those are relevant for this lesson, so if you have a different Arduino board (e.g. an Arduino Uno) you can use it. The ARDX Starter Kit for Arduino from Seeed Studio 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 1: 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. You can simply wire your board according to the diagram (wire colors don't matter, but help with identification of purpose).

The A0-A5 pins on the Arduino 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:

  1. Connect the voltage from the Arduino 5V pin (input voltage) to a circuit (using a breadboard).
  2. Connect the input voltage to a static resistor (10k Ohm)
  3. Establish a voltage divider coming out of the static resistor:
    1. One route to the analog pin (A0)
    2. One route to a variable resistor (the photoresistor)
  4. 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 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:

  1. Red:
    1. Connect the 5V pin to the red/positive side-rail on the breadboard.
    2. 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).
  2. Yellow: Connect the yellow 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).
  3. Black:
    1. Connect the row holding the other lead from the photoresistor to the black/negative side-rail on the breadboard.
    2. 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 lone lead of the photoresistor, but I like building a habit of connecting the 5V and GND pins from the Arduino to the side rails. This will come in handy in the future lessons.

Step 2: Writing the Code

Using the Arduino IDE create a new sketch. The new sketch has two stubbed out methods.

void setup() {
    // put your setup code here, to run once:

void loop() {
    // put your main code here, to run repeatedly:

Prior to the setup method you will declare a variable for the analog pin that is connected to the photoresistor.

//Photoresistor Pin
int analogPin = 0;

void setup() {
  // put your setup code here, to run once:

As you recall from the previous lesson, the setup method runs once when the firmware starts. For this lesson the goal is to write the voltage value coming from the photoresistor to the serial monitor. In order to do that you will start the serial monitor using the Serial.begin method and pass in the baud rate (bits per second).

void setup() {
  // put your setup code here, to run once:

Next you will write the code to read the raw data coming in on A0 (remember it will be a value between 0 and 1023 which is 1024 steps or units) and convert it to a voltage reading (0.0V to 5.0V).

void loop() {
  // put your main code here, to run repeatedly:
  // read the raw data coming in on analog pin 0:
  int lightLevel = analogRead(analogPin);
  // Convert the raw data value (0 - 1023) to voltage (0.0V - 5.0V):
  float voltage = lightLevel * (5.0 / 1024.0);
  // write the voltage value to the serial monitor:

Step 3: Upload the Firmware

Before uploading the firmware it is always a good idea to verify the board targeted and the port connected (if you don't remember this, check the previous lesson).

Press the Upload button to compile the firmware and send it to the Arduino.

Step 4: Open the Serial Monitor

After the sketch has compiled and uploaded to the Arduino, click on the magnifying glass icon in the upper-right of the Arduino IDE. This will open the Serial Monitor.

Step 5: Read Voltage in Serial Monitor

While the firmware is running and the Serial Monitor is open you will see the data being read from the photoresistor (note: in the image above I used an Arduino Uno, not a Yun). While the firmware is running and you are seeing data in the serial monitor, 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).

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.

StevenS10 - good catch. The lightLevel should be multiplied by the voltage/units where the units is 1024 (0-1023 is 1024 units or steps). I'll correct the lesson.
<p>&quot;This yields a resolution between readings of: 5 volts / 1024 units&quot;</p><p>float voltage = lightLevel * (5.0 / 1024.0);</p><p>not 1023! 1023 is the highest value, but 1024 is your divisor. The highest value of a 10 bit a/d is 1023/1024 times the reference. Note that a 0 reading means the input is between 0 and .0049 v, and 1023 means the input is between 4.995 and 5.000 volts.</p>

About This Instructable




Bio: Director, Strategic Engagements, DX-TED, Microsoft
More by dougseven:IoT Workshop IoT Workshop: Lab 4 - Sending Data to the Cloud IoT Workshop: Lab 3 - Controlling Output with Input 
Add instructable to: