IoT Bytes

Bits and Bytes of IoT

LDR (Light Dependent Resistor) Based Light Sensor using Arduino

Pradeep Singh | 18th Nov 2017

lightbulb

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 –

lightSensorCircuit

With the breadboard, your setup should look similar to the following image –

lightSensorSrduino1

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.

lightSensor

Now you can change the sketch and add the additional logic required for your project.


 

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: