Introduction: Twilight Switch With Levels
We intend to make a twilight switch. In this case, instead of a single LED, we will use 4 LEDs, depending on the level of light that we capture with the LDR sensor.
For the realization of this project, we used a small LDR range, in order to more easily visualize the results (in particular between 600 and 800).
These LEDs simulate, for example, lighting of parts of the house or of different devices such as heating, etc: In this case we will have:
Level 1 (lowest light): 4 LEDs on
Level 2: 3 LEDs on
Level 3: 2 LEDs on
Level 4: 1 led on
Level 5: All LEDs off, enough light.
Step 1: Components You Will Need
Step 2: How to Connect Them
Step 3: The Arduino Code
/*
Interruptor Crepuscular con distintos niveles:
Sistema para controlar el encendido de 4 leds
en funcion de varios intervalos de intensidad luminosa
LDR pin A0
LED1 pin 12
LED2 pin 11
LED3 pin 10
LED4 pin 9
Abril 2018
Rafael Salvador Jiménez
*/
// variables constantes para los pines
const int ldrPin = A0; // establece el pin de la LDR
const int ledPin1 = 12; // establece el pin del LED1 rojo
const int ledPin2 = 11; // establece el pin del LED2 rojo
const int ledPin3 = 10; // establece el pin del LED3 rojo
const int ledPin4 = 9; // establece el pin del LED4 rojo
// variables que pueden cambiar
int ldrValue = 0; // variable para almacenar el valor de la LDR
int ldrlevel1=600; // nivel1 de cambio del valor de la LDR
int ldrlevel2=700; // nivel2 de cambio del valor de la LDR
int ldrlevel3=750; // nivel3 de cambio del valor de la LDR
int ldrlevel4=800; // nivel4 de cambio del valor de la LDR
void setup() {
// establece el LED como una salida
Serial.begin(9600);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);
pinMode(ledPin3, OUTPUT);
pinMode(ledPin4, OUTPUT);
}
void loop() {
ldrValue = analogRead(ldrPin); // lee el valor del sensor
Serial.println(ldrValue);
// si el valor es menor en cada nivel enciende los LEDs correspondientes
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);
}
// si no los apaga
else {
digitalWrite(ledPin1, LOW);
}
}