Programming Examples

Arduino program representing smart street light concept


LDR Sensor

Write a program to interface LED at PWM pin and LDR, in such a way that when the light intensity falling on LDR rises the LED glow should be reduced and after a threshold value the LED should be put off. (representing smart street light concept) 

void setup() 
{
  pinMode(A0, OUTPUT);
  pinMode(9, OUTPUT);
  Serial.begin(9600);
}
void loop() 
{
  int ledBrightness = 0;
  int ldrValue = 0; 
  ldrValue = analogRead(A0); 
  Serial.println(ldrValue);
  if (ldrValue > 700) // Threshold
  {
    analogWrite(9, 0);
  } 
  else 
  {
    ledBrightness = map(ldrValue, 0, threshold, 255, 0);
    analogWrite(9, ledBrightness);
  }
  delay(200);
}
Output/ Explanation:

Latest Current Affairs 2026 Online Exam Quiz for One day Exam Online Typing Test CCC Online Test Python Programming Tutorials Best Computer Training Institute in Prayagraj (Allahabad) Online MBA 2 years Online MCA Online BCA Best Website and Software Company in Allahabad