Photoresistor Example
In this example the photoresitor sends a on /off signal to an Arduino input dependent on ambient light conditions. When the Arduino detects darkness the LED is flashed.
Hardware Required
- 1 x Arduino Board
- 1 x LED
- 1 x 220 Ohm resistor
- 1 x 10 000 Ohm resistor
- 1 x Photoresistor
- 1 x Solderless breadboard
To build the circuit, connect one end of the resistor to Arduino pin 12. Connect the long leg of the LED (the positive leg, called the anode) to the other end of the resistor. Connect the short leg of the LED (the negative leg, called the cathode) to the Arduino GND, as shown in the diagram and the schematic below.
Arduino Code
After you build the circuit plug your Arduino board into your computer, start the Arduino IDE, and enter the code below.
/* Use photoresistor or photocell to teurn on an LED in the dark
Dev: Michalis Vasilakis // Date: 8/6/2015 // www.ardumotive.com */
//Constants
const int pResistor = A1; // Photoresistor at Arduino analog pin A1
const int ledPin=12; // Led pin at Arduino pin 12
//Variables
int value; // Store value from photoresistor (0-1023)
void setup(){
pinMode(ledPin, OUTPUT); // Set lepPin – 12 pin as an output
pinMode(pResistor, INPUT);// Set pResistor – A1 pin as an input (optional)
}
void loop(){
value = analogRead(pResistor);
//You can change value “25”
if (value > 300
){
digitalWrite(ledPin, LOW); //Turn led off
}
else{
digitalWrite(ledPin, HIGH); //Turn led on
}
delay(100); //Small delay
}