In our previous guides, we have shown how to control single LED depending on light with Arduino. Also, we have shown that without Arduino, can control LED depending on the level of light. The ways are different but essentially both involves a basic component – LDR. In this guide, we will show how to control multiple LED depending on the level of light With Arduino. It means – with increasing darkness higher number of LED will light up. In case of our Automatic LED Control Using LDR project, we had different logic. It’s practical usage can be indicator of luminescence or lighting up more light on increasing dark, which happens when sets.
Principal to Control Multiple LED Depending on the Level of Light
We can get an arbitrary value of light on serial monitor of Arduino IDE with LDR and Arduino. If we set each LDR to light up at different values with increasing darkness, our goal will be reached. Pin 13 is Arduino UNO’s board LED. With one LED, our code will become :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | const int ldrPin = A0; const int ledPin1 = 13; int ldrValue = 0; int ldrlevel1=800; // arbitrary value depending on serial monitor void setup() { Serial.begin(9600); pinMode(ledPin1, OUTPUT); } void loop() { ldrValue = analogRead(ldrPin); Serial.println(ldrValue); if (ldrValue < ldrlevel1) { digitalWrite(ledPin4, HIGH); } else { digitalWrite(ledPin1, LOW); } } |
Notice the value on serial monitor of Arduino IDE, note down values with dimming light of your room. Now, if we increase the number of LED and adjust the program, that will do the job. It is easy.
---
Code and Program to Control Multiple LED Depending on the Level of Light
We will use 4 LDR in this project. We need the following parts :
Arduino UNO or similar board – ONE
LDR – ONE
10K Ohm resistor – ONE
LED – FOUR
220 Ohm resistors for LEDs – FOUR
Breadboard – ONE
Jumpers – Few
This will be the circuit diagram (better to say connection) :
This is example code with 4 LED and serial output for debugging :
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 | 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); } } |
Notice the logic :
1 2 3 4 5 6 7 8 9 | ... if (ldrValue < ldrlevel1) { digitalWrite(ledPin4, HIGH); } else if (ldrValue < ldrlevel2) { digitalWrite(ledPin4, LOW); digitalWrite(ledPin3, HIGH); } ... |
5th pin is ledPin4
and ldrlevel
is lowest. ldrlevel1
is second lowest, ldrlevel1
is third lowest and so on.
When ldrlevel1 value will be greater than ldrlevel, then glow LED number 4.
When ldrlevel2 value will be greater than ldrlevel1, then glow LED number 3.
And so on. Response will be slower, not superb. LDR just a component, the circuit is just easy.
Tagged With arduino logic to control multiple led