Lesson Objectives: Create a smart LED that turns on in a dark room and off in a light room. Describe the chemistry and physics governing photoresistors (light dependent resistors). Utilize a voltage divider to read voltage in a circuit.
- Microprocessor with cable
- Resistors (100 and 10k Ohms)
- Photoresistor (light dependent resistor, mine is a GL5539)
- The below code.
Image of the breadboard:
Image of the schematic:
Code used for this project, which I entitled, “PhotoresistorData”:
This code is a modification of the code found here:
Read a photoresistor (light sensor) to detect "darkness" and turn on an LED when it
is "dark" and turn back off again when it is "bright. Print analog read to serial monitor.
Code was modified to allow viewer to see the value read at the voltage divider.
The code was also modified to include an output format that is more easily exported as a .csv.
AnalogRead values are sent to the serial monitor so values can be analyzed.
// As usual, we'll create constants to name the pins we're using.
// This will make it easier to follow the code below.
const int sensorPin = 0;
const int ledPin = 3;
// We'll also set up some global variables for the light level a calibration value and
//and a raw light value
int readingNumber = 0; //sets the first reading value to zero, so all future values are time in seconds from start
// We'll set up the LED pin to be an output.
lightCal = analogRead(sensorPin);
//we will take a single reading from the light sensor and store it in the lightCal
//variable. This will give us a prelinary value to compare against in the loop
Serial.begin(9600); //initializes serial port with baud rate of 9600, pretends the USB is an old school serial port
//baud rate is a way of confirming that the send rate equals the receive rate
Serial.println("Serial is initialized"); // verify that it does something, starts text on a new line after function
Serial.print("Initial light reading: "); //verify the initial reading, appends to previous line, quotes are not values but actual words to be included
Serial.println(lightCal); //adds the lightCal reading after the previous colon
Serial.println("ReadingNumber , LightValue"); //Headers for the data that will be collected, CSV
//Take a reading using analogRead() on sensor pin and store it in lightVal
lightVal = analogRead(sensorPin);
Serial.print(readingNumber); //prints the reading number to the serial monitor
Serial.print(" , "); //prints a comma in the serial monitor to aid in CSV data collection
Serial.println(lightVal); //prints the current photoresistor values
readingNumber++; //shorthand of saying new variable = previous variable +1
delay(1000); //sets data collection sampling rate to 1 second, remember time is in thousandths of a second
//if lightVal is less than our initial reading (lightCal) minus 50 it is dark and
//turn pin 9 HIGH. The (-50) part of the statement sets the sensitivity. The smaller
//the number the more sensitive the circuit will be to variances in light.You may need to adjust this value
//depending on environmental conditions, the amount of light in your room initially.
if (lightVal < lightCal - 50)
//else, it is bright, turn pin 3 LOW
Other helpful links that will assist with learning the content in this video or with production of circuit diagrams:
- I used this page to design the breadboard picture. The link should take you to the project that I designed. Circuit.io is a pretty handy little website that allows you to design and see your circuitry.
- I used this website to design the circuit schematic. This is one of the better free programs. https://www.circuit-diagram.org/editor/
- This website has a fantastic tutorial on voltage dividers as does this website.
- This YouTube video walks you through the concept of voltage dividers in an easy to understand manner.
- This tutorial was fantastic at explaining the process I outlined in the video. Their code doesn’t work in all lighting conditions, as it doesn’t have a way to calibrate based on native light conditions.
- The code used in this tutorial is a modification of the code found here.