This lesson introduces the photoresistor component. The potentiometer is a commonly used analog input component.
map()
function is used with the photoresistor.You’ve worked with analog input with the potentiometer component. In this project, use a new analog input component called a photoresistor.
Photoresistors are similar to potentiometers in that they are both types of variable resistors and that they can be used in projects as analog inputs. The values of a photoresistor are based on the ambient light, where the amount of resistance decreases as the intensity of ambient light increases.
The photoresistor has only two pins instead of the three on the potentiometer. You’ll need to create a voltage divider circuit to use the photoresistor. You don’t need to worry about how to calculate such a circuit from scratch, as most projects that require one will provide guidance on how to create one.
The photoresistor will be used to gather analog input in a similar way that the potentiometer did. Instead of changing the resistance by turning a knob, the level of ambient light will change the resistance amount produced by the photoresistor.
The photoresistor has two pins and has a white colored top with red “squiggly” lines running across the head. Here is a picture of a photoresistor:
The polarity of the legs doesn’t matter – there isn’t a positive or negative leg like the LED. However, you’ll need to make sure that you’re building out the circuit correctly as the voltage divider circuit setup is precise.
The best way to understand the photoresistor is to jump into a project that uses it! You’re going to make a nightlight: an LED that turns on once the photoresistor registers that the room is dark.
Reminder: The 10k Ohm resistor is the brown/black/orange resistor. Here is an image:
Use this breadboard diagram as your guide:
The connections are the same as the potentiometer project, except in this the photoresistor replaces the potentiometer. On the broadboard diagram, this box represents the photoresistor:
You can run the same sketch as you did for the potentiometer! This works because the photoresistor is sending output to the A0 analog input on the Arduino. The only difference is that the values are being sent from the photoresistor instead of the potentiometer. If you’re having any difficulties, double-check to make sure that you’ve placed the photoresistor correctly and that it’s connected to A0 on the Arduino.
None! This project introduced a new component with no additional code for the sketch. This demonstrates how you’re able to use a variety of components once you become comfortable with the core Arduino setup.