// Intelligent lighting system
#define LDR_PIN_AO A0
#define LED_PIN 10
#define PIR_OUT_PIN 13
// Arduino ADC parameters
const float VREF = 5.0;
const float ADC_resolution = 10;
void setup() {
pinMode(LED_PIN, OUTPUT);
pinMode(PIR_OUT_PIN, INPUT);
Serial.begin(115200);
}
float get_ldr_voltage() {
int analog_value = analogRead(LDR_PIN_AO); // Reads the analog value from pin
// Voltage conversion formula
float voltage = (analog_value / pow(2,ADC_resolution)) * VREF;
return voltage;
}
bool motion_detection() {
// a logic HIGH in PIR OUT corresponds to motion
if (digitalRead(PIR_OUT_PIN) == HIGH) {
return true;
}
// a logic LOW in PIR OUT corresponds to no motion
else {
return false;
}
}
void loop() {
float voltage_val = get_ldr_voltage();
Serial.print("LDR voltage : ");
Serial.println(voltage_val);
bool poor_lighting;
// Voltage value for office lighting is 1.37V and for Stairway lighting is 2.5V
// Hence choosing a value of 2V between this range.
// Any value greater than 2V can be assumed to be poor lighting.
if (voltage_val > 2.0) {
poor_lighting = true;
} else {
poor_lighting = false;
}
bool motion = motion_detection();
// Turn ON LED if room if poorly lit and motion is detected
if (motion == true && poor_lighting == true) {
digitalWrite(LED_PIN, HIGH);
} else {
digitalWrite(LED_PIN, LOW);
}
delay(100);
}