Tutorial: Photoresistors
Using a Photoresistor
In this example we use a variable resistor (a photoresistor), we read its value using one analog input of an Arduino board and we change the blink rate of an LED. The variable resistor's analog value is read as a voltage because this is how the analog inputs work.
You Need:
- 10K ohm photoresistor and 10K ohm resistor
- 220 ohm resistor and red LED
10K has a red stripe 2nd from the end. 220 has 2 red stripes on one end
photoresistors are variable resistors because they take in different amounts of light.
NOTE:
• We are powering the + row on the breadboard with 5V and also going to the A0 pin to get the value of the photoresists.
• This circuit uses a variable resistor, a fixed resistor and the measurement point is in the middle of the resistors.
Your LED should blink faster and slower based the amount of light it gets.
int sensorPin = A0; // select the input pin
int ledPin = 13; // select the internal pin for the LED
int sensorValue = 0; // variable to store the value coming from the sensor
void setup() {
// declare the ledPin as an OUTPUT:
pinMode(ledPin, OUTPUT);
}
void loop() {
// read the value from the sensor:
sensorValue = analogRead(sensorPin);
// turn the ledPin on
digitalWrite(ledPin, HIGH);
// stop the program for <sensorValue> milliseconds:
delay(sensorValue);
// turn the ledPin off:
digitalWrite(ledPin, LOW);
// stop the program for for <sensorValue> milliseconds:
delay(sensorValue);
}