Introduction
A photoresistor or photocell is a light-controlled variable resistor. The resistance of a photoresistor decreases with increasing incident light intensity. A photoresistor can be applied in light-sensitive detector circuits, and light- and dark-activated switching circuits. It's also called light-dependent resistor (LDR).
In this tutorial you will learn how to use a photoresistor with and without arduino uno. |
How to use a photoresistor
Let's see how a photoresistor react in light. Build the circuit above and notice how led brightness change.
The resistance value becomes smaller when there is much light in the room. So in the dark the led remains off because the resistance has become very big.
The Arduino will help us to reverse this situation, let's see how in next step.
The resistance value becomes smaller when there is much light in the room. So in the dark the led remains off because the resistance has become very big.
The Arduino will help us to reverse this situation, let's see how in next step.
What you will need - Hardware
For this tutorial you will need:
- Arduino uno
- Breadboard
- Photoresistor
- LED
- 220 Ohm & 10KOhm resistors
The Circuit
The connections are pretty easy, see the image above with breadboard circuit schematic.
The code
Here's the 'led & photoresistor' code
How it works:
How it works:
- Read analog value from photoresistor/photocell
-> value=analogRead(pResistor)
- Check if value is bigger than e.g. 25
- Send command to turn on/off the led
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | /* Use a photoresistor (or photocell) to turn on an LED in the dark More info and circuit schematic: http://www.ardumotive.com/how-to-use-a-photoresistor-en.html Dev: Michalis Vasilakis // Date: 8/6/2015 // www.ardumotive.com */ //Constants const int pResistor = A0; // Photoresistor at Arduino analog pin A0 const int ledPin=9; // Led pin at Arduino pin 9 //Variables int value; // Store value from photoresistor (0-1023) void setup(){ pinMode(ledPin, OUTPUT); // Set lepPin - 9 pin as an output pinMode(pResistor, INPUT);// Set pResistor - A0 pin as an input (optional) } void loop(){ value = analogRead(pResistor); //You can change value "25" if (value > 25){ digitalWrite(ledPin, LOW); //Turn led off } else{ digitalWrite(ledPin, HIGH); //Turn led on } delay(500); //Small delay } |
Download the code from here and open it with Arduino IDE.
|
|
You can keep playing with that and start making your own modifications to the code.
For example, try to change "25" value and see how it changes the program.
For example, try to change "25" value and see how it changes the program.
Well done!
You have successfully completed one more Arduino "How to" tutorial and you learned how to use a photoresistor!
I hope you liked this, let me know in the comments.
I hope you liked this, let me know in the comments.