Auto Street Light

Aim

To control the street light using a light sensor.

Components required for the experiment

Arduino UNO Board, Breadboard, Resistors (1K Ohms and 220 Ohms), LED and LDR.

What is LDR and where it is used?

LDR stands for Light Dependent Resistor, which is a passive electronic component that changes its resistance based on the amount of light that falls on it. LDRs are used in a variety of applications, such as light sensors for automatic lighting controls, outdoor lighting controls, and photographic light meters. They can also be used in burglar alarms and other security systems to detect the presence or absence of light. LDRs are commonly found in consumer electronics products, such as cameras, smartphones, and digital displays.

Circuit Diagram

To practice, make the circuit diagram with the exact pin-to-pin connection as shown below. This will help you to complete the experiment successfully. Click on the image to enlarge.

Code

const int LED = 13;
const int LDR = A0;
int value;
void setup() {
  Serial.begin (9600);
  pinMode(LED, OUTPUT);
  pinMode(LDR, INPUT);
}
void loop() {
  Serial.println (value);
  value = analogRead (LDR);
  delay (500);
 int input = analogRead(LDR);
  if (input > 780)
  {
     digitalWrite(LED, HIGH);
  }
  else {
     digitalWrite(LED, LOW);
  }
}

Code Explained

This code turns ON LED depending on the value of LDR.

const int LED = 13;
const int LDR = A0;
int value;

The first two lines declare two constant integer variables LED and LDR and assign them the values 13 and A0, respectively. The third line declares an integer variable named "value" without assigning it a value.

void setup()
{
Serial.begin (9600);
pinMode(LED, OUTPUT);
pinMode(LDR, INPUT);
}

The setup() function is a built-in function in the Arduino programming language that runs once at the start of the program.

In this code, the Serial.begin(9600) line initializes the serial communication between the Arduino board and the computer at a baud rate of 9600 bits per second.

The pinMode(LED, OUTPUT) line sets the LED pin (pin 13) as an output pin. The pinMode(LDR, INPUT) line sets the LDR pin (analog pin 0) as an input pin.

void loop() {
  Serial.println (value);
  value = analogRead (LDR);
  delay (500);
 int input = analogRead(LDR);
  if (input > 780)
  {
     digitalWrite(LED, HIGH);
  }
  else {
     digitalWrite(LED, LOW);
  }
}

The "loop" function is another built-in function that runs repeatedly as long as the Arduino board has power.

In this code, the "Serial.println(value)" line prints the value of the "value" variable to the serial monitor.

The "value = analogRead(LDR)" line reads the value from the LDR and assigns it to the "value" variable.

The "delay(500)" line creates a delay of 500 milliseconds (half a second) between each loop iteration.

The "input = analogRead(LDR)" line reads the value from the LDR and assigns it to the "input" variable.

The "if (input > 780)" line checks if the value of "input" is greater than 780.

If the condition is true, the "digitalWrite(LED, HIGH)" line turns ON the LED (pin 13).

If the condition is false, the "digitalWrite(LED, LOW)" line turns OFF the LED.

Output and Serial Monitor.

  1. Press the start simulation button in the interface.

  2. Move the mouse cursor to the LDR sensor.

  3. Click on the LDR sensor to activate it.

  4. Once activated, a slide bar will appear on top of the LDR sensor.

  5. Adjust the slider to change the LDR value.

  6. Observe the corresponding changes in the Serial monitor as you modify the LDR value.

  7. Additionally, pay attention to the LED, which will turn on when the LDR value reaches a certain threshold as specified in the code.

 

 

 

 

 

 

 

 

 

 

 

 

Conclusion

We have observed that when there is a light falling on LDR the LED is in an OFF state and when there is no light the LED turns ON.

Assignment

Change the code to turn ON the LED for an LDR value above 600, and print "LED is Turned ON" on the next line.