Emergency Light Controller with backup battery and automatic hourly tests using Arduino for reliable power outage response and system status indication.
// Emergency light controller with backup battery and automatic test
// Arduino Uno
// Pin definitions
const int pinLedStato = 13; // Red status LED
const int pinLuce1 = 8; // Emergency light control 1
const int pinLuce2 = 9; // Emergency light control 2
const int pinPulsanteTest = 2; // Button for manual test
const int pinBuzzer = 10; // Buzzer for acoustic signals
const int pinSensoreRete = A0; // Mains voltage sensor (divider)
// Thresholds and timings
const int sogliaRete = 100; // Minimum analog value to consider mains present (to calibrate)
const unsigned long intervalloTestAutomatico = 3600000UL; // 1 hour in milliseconds
const unsigned long durataTest = 10000UL; // Duration of automatic and manual test (10 seconds)
// State variables
bool retePresente = true;
bool testInCorso = false;
unsigned long ultimoTest = 0;
unsigned long inizioTest = 0;
void setup() {
pinMode(pinLedStato, OUTPUT);
pinMode(pinLuce1, OUTPUT);
pinMode(pinLuce2, OUTPUT);
pinMode(pinBuzzer, OUTPUT);
pinMode(pinPulsanteTest, INPUT_PULLUP); // Button with internal pull-up
digitalWrite(pinLedStato, LOW);
digitalWrite(pinLuce1, LOW);
digitalWrite(pinLuce2, LOW);
digitalWrite(pinBuzzer, LOW);
Serial.begin(9600);
ultimoTest = millis();
}
void loop() {
// Read mains voltage sensor
int valoreRete = analogRead(pinSensoreRete);
// Serial.println(valoreRete); // Debug
// Check mains presence
if (valoreRete > sogliaRete) {
retePresente = true;
} else {
retePresente = false;
}
// Manage emergency lights
if (!retePresente) {
// Mains absent: turn on emergency lights
accendiLuciEmergenza(true);
digitalWrite(pinLedStato, HIGH); // Red LED on indicates active emergency
} else {
// Mains present: turn off emergency lights
accendiLuciEmergenza(false);
digitalWrite(pinLedStato, LOW);
}
// Check button for manual test
if (digitalRead(pinPulsanteTest) == LOW && !testInCorso) {
// Button pressed, start test
avviaTest();
}
// Check automatic test
if (!testInCorso && (millis() - ultimoTest >= intervalloTestAutomatico)) {
avviaTest();
}
// If test in progress, manage duration and signals
if (testInCorso) {
unsigned long tempoTrascorso = millis() - inizioTest;
if (tempoTrascorso < durataTest) {
// During test: turn on lights and blinking buzzer
accendiLuciEmergenza(true);
lampeggiaBuzzer(tempoTrascorso);
digitalWrite(pinLedStato, HIGH); // Red LED on during test
} else {
// End test
testInCorso = false;
ultimoTest = millis();
accendiLuciEmergenza(!retePresente); // Restore lights state based on mains
digitalWrite(pinLedStato, retePresente ? LOW : HIGH);
digitalWrite(pinBuzzer, LOW);
}
}
delay(50); // Small delay for stability
}
// Function to turn emergency lights on or off
void accendiLuciEmergenza(bool stato) {
digitalWrite(pinLuce1, stato ? HIGH : LOW);
digitalWrite(pinLuce2, stato ? HIGH : LOW);
}
// Function to start automatic/manual test
void avviaTest() {
testInCorso = true;
inizioTest = millis();
Serial.println("Starting emergency lights test");
}
// Function to blink buzzer during test
void lampeggiaBuzzer(unsigned long tempo) {
// Blink frequency 2Hz (on 250ms, off 250ms)
if ((tempo / 250) % 2 == 0) {
digitalWrite(pinBuzzer, HIGH);
} else {
digitalWrite(pinBuzzer, LOW);
}
}Arduino Uno (x1)
Red 5mm LED (x1) - Status LED
White 5mm LEDs (x2) - Emergency lights
220Ω Resistor (x3) - for LEDs
Button (x1) - for manual test start
Piezo buzzer (x1) - acoustic signals
AC voltage sensor module (or resistive divider) for mains detection (x1)
12V backup battery power supply (x1)
NPN Transistor (e.g. 2N2222) or MOSFET to drive emergency lights (x2)
10kΩ transistor base resistor (x2)
CONNECTIONS:Mains voltage sensor connected to analog input A0 (via divider for safety, max 5V Arduino input)
Red status LED connected to D13 via 220Ω resistor
White emergency LEDs connected to D8 and D9 via transistor and 220Ω resistors
Button connected to D2 and GND (configured with internal INPUT_PULLUP)
Buzzer connected to D10
Transistor base for lights connected to D8 and D9 via 10kΩ resistors
Transistor collector connected to negative pole of lights, emitter to GND
Emergency lights powered by 12V backup battery, transistor acts as low-side switch to turn them on