EER99
Published © GPL3+

Arduino Tutorial - Controlling LEDS with LDR Sensor

How to control LEDS with LDR sensor.

BeginnerFull instructions provided14,480
Arduino Tutorial - Controlling LEDS with LDR Sensor

Things used in this project

Hardware components

Breadboard (generic)
Breadboard (generic)
×1
Resistor 220 ohm
Resistor 220 ohm
4 Pcs
×1
Resistor 10k ohm
Resistor 10k ohm
×1
Arduino UNO
Arduino UNO
×1
Jumper wires (generic)
Jumper wires (generic)
×1
LED (generic)
LED (generic)
4 pcs
×1
LDR, 5 Mohm
LDR, 5 Mohm
×1

Software apps and online services

Arduino IDE
Arduino IDE

Hand tools and fabrication machines

Plier, Needle Nose
Plier, Needle Nose

Story

Read more

Schematics

Diagram

Code

The code

Arduino
/*
   Roy Ben Avraham
   Last modification: 31/3/2020
   Project: Controlling LEDS with LDR sensor
*/

// variables ( You can change the pins as you wish)
const int ldrPin = A0;    
const int ledPin1 = 12; 
const int ledPin2 = 11; 
const int ledPin3 = 10; 
const int ledPin4 = 9; 

int ldrValue = 0;
int ldrlevel1=600; 
int ldrlevel2=700; 
int ldrlevel3=750; 
int ldrlevel4=800;

void setup() {
  Serial.begin(9600);    
  pinMode(ledPin1, OUTPUT);
  pinMode(ledPin2, OUTPUT);
  pinMode(ledPin3, OUTPUT);
  pinMode(ledPin4, OUTPUT);
}

void loop() {
  ldrValue = analogRead(ldrPin); 
  Serial.println(ldrValue);
  if (ldrValue < ldrlevel1) {
    digitalWrite(ledPin4, HIGH);
  }
  else if (ldrValue < ldrlevel2) {
    digitalWrite(ledPin4, LOW);
    digitalWrite(ledPin3, HIGH);
  }
  else if (ldrValue < ldrlevel3) {
    digitalWrite(ledPin3, LOW);
    digitalWrite(ledPin2, HIGH);
  }
  else if (ldrValue < ldrlevel4) {
    digitalWrite(ledPin2, LOW);
    digitalWrite(ledPin1, HIGH);
  }
  else {
    digitalWrite(ledPin1, LOW);
      }
   }

Credits

EER99

EER99

12 projects • 23 followers
B.Sc. Electrical & Electronics Engineer

Comments