Pradeep Singh | 18th Nov 2017
In this article, you will find, how to create a light sensor that can act as the basis for some other automation, for example, a light bulb that turns on at the dusk and turns off at the dawn etc. So let’s get started and create the Light Sensor.
1. What do you need:
Before starting with this article, you should make sure that you have the following parts ready –
- Arduino Uno
- 10K Ohm Resistor
- Light Dependent Resistor (LDR)
- Breadboard
- Male to Male Jumper Wires
2. The Circuit:
Connect the Arduino Uno with the LDR as shown in the following diagram –
With the breadboard, your setup should look similar to the following image –
3. The Code / Sketch:
Once you are ready with your circuit, you can open your Arduino IDE and use the following sketch to read the light readings from the LDR –
int photocellPin = 0; // LDR and 10K pulldown registor are connected to A0 int photocellReading; // Variable to read the analog value void setup(void) { // Initialize Serial Monitor Serial.begin(9600); } void loop(void) { photocellReading = analogRead(photocellPin); if (photocellReading <= 200) { Serial.println("DARK : Analog Value = " + String(photocellReading)); } else if (photocellReading > 200 && photocellReading <= 500) { Serial.println("DIM LIGHT : Analog Value = " + String(photocellReading)); } else if (photocellReading > 500 && photocellReading <= 800) { Serial.println("BRIGHT LIGHT : Analog Value = " + String(photocellReading)); } else if (photocellReading > 800) { Serial.println("FULL DAY LIGHT : Analog Value = " + String(photocellReading)); } delay(100); }
4. The Result:
After pushing the sketch to the Arduino Uno, you can open the Serial Monitor (Tools –> Serial Monitor). In the Serial Monitor window, you should see the values coming from your LDR light sensor.
Try blocking the LDR with your finger, flash some light on it, and see the changing values on the Serial Monitor.
Now you can change the sketch and add the additional logic required for your project.