In this tutorial, we will learn to turn on light automatically by using LDR Sensor with Arduino.

»Read More about LDR Sensor

Components List

  • Arduino Board
  • LDR Sensor
  • Jumper Wires
  • 1 x Breadboard
  • 1 x LED

Circuit Diagram of LDR Sensor with Arduino

Circuit Diagram of LDR with Arduino

Code for Testing LDR Output

//Code from :- http://infiniteiotdevices.com/arduino-tutorials/ldr-with-arduino/

int sensorPin = A0;

void setup() 
{
  Serial.begin(9600);
}
void loop() 
{
  int sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
  delay(100);
}

Output Of LDR Testing

Output of LDR Testing

Cicuit Diagram of LDR and LED with Arduino

Cicuit Diagram of LDR and LED with Arduino

Code for Turn on LED

//Code from :- http://infiniteiotdevices.com/arduino-tutorials/ldr-with-arduino/


int sensorPin = A0;
int ledPin = 9;

void setup() 
{
  Serial.begin(9600);
  pinMode(sensorPin, INPUT);
  pinMode(ledPin, OUTPUT);
}
void loop() 
{
  int sensorValue = analogRead(sensorPin);
  if( sensorValue <= 300 )  // Change the value as per your requirement
  {
    digitalWrite(ledPin, HIGH);
    Serial.print("LED ON ");
    Serial.println(sensorValue);
    delay(100);
  }

  else
  {
    digitalWrite(ledPin, LOW);
    Serial.println("LED OFF");
  }
}

Output of LDR and LED with Arduino

Output of LDR and LED with Arduino

Our Youtube Channel