Introduction: Tutorial 6: Light Sensor and Light

This is the 6th tutorial in the series. This is the first time we look at the analog light sensor. Unlike digital sensors that either output HIGH (button is being pushed) or LOW (button is not being pushed), analog sensors return a value (often between 0 and 1023) depending on some variable (e.g. how much light there is, how hot it is, how loud it is etc.). We can code some agreed threshold value, at which point when the sensor reading goes over it, we execute some code. Here, when the light sensor value gets too low, we will make a "night light" (LED) come on.


The link for the Light Sensor's Wiki can be found here: https://docs.google.com/document/d/1j0lO5tisv1Pt-KesIf79s6sBoXzv1dohcPAP7uk_E9g/edit?usp=sharing

The link for the LED's Wiki can be found here: https://docs.google.com/document/d/16PjgJa6EOTU2Pw7gFAdgUt8tYK8eJsXtQvOHXAlh_6w/edit?usp=sharing


If you need the LED Library, make a folder called LED in your Arduino Libraries folder, and add the 2 LED files below to that folder.

If you need the Light Sensor Library, make a folder called LightSensor in your Arduino Libraries folder, and add the 2 LightSensor files below to that folder.

Supplies

FROM YOUR KIT:

1 x Arduino with Grove Shield

1 x USB-B (Arduino end) to USB-A (laptop end).

1 x USB-A to USB-C adaptor (if using a MAC with no USB-A port)

1 x Grove LED

1 x Light Sensor

2 x Grove cables

4 x rickets/screws from accessories (optional if you want to fix modules onto cardboard)


BYO:

1 x Laptop with Arduino IDE software

1 x piece of cardboard (optional)

1 x screwdriver (optional)

Step 1: Build

Use the PDF below to help construct the circuit.

Step 2: Code

The code for this project is as follows:

//Add Libraries
#include "Led.h"
#include "LightSensor.h"

//Global Variables
int sensorValue = 0;

//Create Objects
Led led1(2);
LightSensor lightSensor1(A0);

//Setup
void setup() {
 Serial.begin(9600);
}

//Main Loop
void loop() {
 sensorValue = lightSensor1.getReading();
 Serial.println(sensorValue);

 if (sensorValue < 300) {
  led1.on();
 }
 else {
  led1.off();
 }

 delay(500);
}


Let's take a look at each part.


#include "Led.h"
#include "LightSensor.h"

This includes the 2 libraries needed (one for the LED, one for the Light Sensor)


int sensorValue = 0;

As we take readings, we will need to store the value of the light sensor. As it gives integer values between 0 and 1023, it makes sense to make this variable an integer. I set it equal to 0, but it doesn't matter what you set it to because it will update as soon as the program runs.


As it is good to sometimes get a sense of the kind of values an analog sensor gives you, we use a Serial line between the Arduino and computer, so that the arduino can keep letting the computer know the values. To do this, we have to add a Serial.begin to the setup. The 9600 inside is the baud rate. Typically, we set it to 9600, but certain devices will require a higher baud rate... but will also specify in the wiki.

So

void setup() {
 Serial.begin(9600);
}

This starts the serial communication at the very start (in the setup).


The main look is broken into 2 parts.

In part 1, we take a reading from the sensor, and store the value in our variable.

sensorValue = lightSensor1.getReading();
Serial.println(sensorValue);

We also print this value on our serial line, so we can see it in the Serial Monitor (to get this up, when in Arduino, go to TOOLS // Serial Monitor) and will show up. We'll have to wait until the program begins running before values will start flashing onto it.


In Part 2, we conditionally make the light turn on or off, depending on how light it is around the sensor. We use the light sensor reading of '300' here as a threshold of what makes it 'dark', but you can arbitrarily change this to whatever you wish, so that it suits your purpose.

 if (sensorValue < 300) {
  led1.on();
 }
 else {
  led1.off();
 }


Finally, in the main loop, we have a half-second delay before checking the sensor again. This is for quick feedback... for a real device when things darken very slowly over the day, you could probably get away with checking every couple of seconds instead.

Step 3: Upload and Test

You'll need to upload this program to the Arduino (after saving it with an appropriate name into your Arduino Project Folder). Make sure you have gone to TOOLS / Serial Monitor, while in the Arduino IDE software and that a little white window popped up. Once you have uploaded the program to the Arduino, values should start appearing in this window. These are 'live' values of our light sensor data.


Note, in normal light, the values should be pretty high. As you move your hand over the light sensor and lower it, to block more and more light, note how the light values go down. We used 300 as a cut-off point. How much do you have to cover this for it to work? If the values are never going less than it, then maybe make the threshold higher (say 400 or 500 instead of 300). If the light is permanently on because even in full light it is less than 300, change it to some lower value. A trick is to note the value in normal conditions, and move into a dark place (or cover sensor with hand) and note the other value. You can make your threshold value somewhere between these 2 values.