Photoresistor
Big Question
How can you change output based on light input?
Investigate
You will need the following components:
- Arduino UNO Board
- Breadboard
- 1 LED of any color
- 1 Photoresistor
- 4 Jumper Wires
- Medium-Length: 1 red, 1 black, 1 same color as the LED, 1 color different than the LED
- 1 1KΩ Resistor
- 1 220Ω Resistor
A photoresistor is a resistor that changes its resistance based on light. These used to make devices respond to different levels of light. One example is a screen dimming in bright light. When the light level is low the user needs to be able to see the screen, so it's brighter. In brighter light there isn't as much need to have a backlight, so the screen dims to save power.
A photoresistor has a photocell with two wires connected to it. The photocell detects the amount of light and changes the resistance accordingly. The Arduino receives the level of resistance as a value between 0 and 1023. We cannot simply use the photoresistor by itself because it will not give an accurate reading, therefore we will use a 1KΩ resistor with it. This divides the voltage up so we can actually get a reading from the photoresistor.
Procedure:
- Use the red and black wires to connect 5V the + rail and GND to the - rail on the breadboard
- Connect one wire of the photoresistor to the + rail on the breadboard and the other wire on the photoresistor to one row on the breadboard (toward the middle of the breadboard gives plenty of space)
- Connect the 1KΩ resistor between the row on the breadboard with one wire from the photoresistor and the - rail on the breadboard (close to the rails to give space between the photoresistor the 1KΩ resistor)
- Connect the other color wire (not the LED color) to the row with the photoresistor and 1KΩ resistor, in between the two, and one analog input on the Arduino board (A0 for example)
- Wire up an LED using the - rail, a row on the breadboard, the 220Ω resistor and the corresponding color wire to a digital pin on the Arduino (you should know how to do this since it was part of Unit 1)
- Go to the Arduino IDE and open a new sketch
- Add integers for the photoresistor pin (use A and the number) and the LED pin
- Setup the photoresistor pin for input and the LED pin for output
- In the main part of the program, check the value from an photoresistor pin and...
- If the value is at least 512, turn the LED on
- Otherwise (the value is less than 512), turn the LED off
- Plug the USB cable into the Arduino and your computer
- Upload the sketch and observe the results when you...
- Leave the photoresistor alone
- Increase the light intensity (use the flashlight on your phone)
- Decrease the light intensity (cover the photoresistor with your hand)
Once the LED is turning on and off depending on the light intensity, save the sketch in your sketchbook as photoresistor_LED
Questions to answer:
- What happened to the LED when you left the photoresistor alone?
- What happened to the LED when you increased the light intensity?
- What happened to the LED when you decreased the light intensity?
- What is the relationship between the light intensity and the value received by the Arduino board?
Share the answers to these questions with your teacher before you move on.
Try Yourself
You will need to add the following components:
- 2 LEDs, different colors
- 2 Jumper Wires
- Medium-Length: 1 color of each LED
- 2 220Ω Resistors
Add circuits for each of the following:
- Each of the LEDs, a 220Ω resistor and a digital pin
Open a new sketch in the Arduino IDE. Using the Arduino IDE reference:
- Create integers for the photoresistor and LEDs (4 total)
- Setup the photoresistor pin as input and the LED pins as output
- For the main code, check the value from the photoresistor pin and...
- If the light intensity is below 340, turn on one LED
- Otherwise, if the light intensity is below 680, turn on two LEDs
- Otherwise, turn on all three LEDs
Some common problems you may encounter:
- The incorrect number of LEDs turn on depending on the light intensity
- Assuming LEDs are already going to be turned on when a higher light intensity is detected
- Assuming LEDs are already going to be turned off when a lower light intensity is detected
When your LEDs are correctly showing the light intensity from the photoresistor pin, save the sketch in your sketchbook as photoresistor_three_LEDs
Extensions
Try each of these extensions building on what you just learned and what you learned in Unit 1. Save the sketches that you create in each extension separately.
Extension 1: Finer-Controlled Light Meter
Challenge: Use 8 LEDs (2 each of 4 different colors) to display the light intensity level from the photoresistor
Extension 2: Light Alarm
Challenge: Make a buzzer go off when the light is too intense, but stop when the light is less intense
Advanced Extensions
Try these extensions after completing other lessons in this unit.
Extension 3: RGB LED Brightness Control
Challenge: Use an RGB LED to display the light intensity level from the photoresistor (all three color channels)
Extension 4: RGB LED Color Control
Challenge: Use 3 photoresistors to control the color of an RGB LED