#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#define DHTPIN A1 // Pin where the DHT22 is connected
#define DHTTYPE DHT22 // DHT 22 (AM2302)
// Setup display and temperature sensor
LiquidCrystal_I2C lcd(0x27, 16, 2); // Update the address if needed
// Create DHT object
DHT dht(DHTPIN, DHTTYPE);
// Declaring ALARMS TRIGGER pins
const int chrgeFail = 10;
const int lowVolt = 11;
const int highVolt = 12;
const int voltageRead = A5;
const int power = 13;
float voltage = 0; // Declaring variables to store values
int powerState = 0;
int chargeState = 0;
int voltage110 = 0;
int voltageState1 = 0;
int voltageState2 = 0;
int LED = 7; // Declaring LEDs
int LED2 = 8;
void setup() {
// put your setup code here, to run once:
pinMode(LED, OUTPUT);
pinMode(LED2, OUTPUT);
pinMode(chrgeFail, INPUT);
pinMode(power, INPUT);
pinMode(voltageRead, INPUT);
pinMode(highVolt, INPUT);
pinMode(lowVolt, INPUT);
Serial.begin(9600);
lcd.begin(20, 4);
dht.begin(); // Initialize DHT sensor
lcd.setCursor(0, 0);
lcd.print("UPS REG");
delay(500);
lcd.clear();
}
void loop() {
// put your main code here, to run repeatedly:
// Read DHT22 temperature and humidity
float DHT22_temperature = dht.readTemperature(); // Read temperature in Celsius
// Check if any reads failed and exit early (to try again)
if (isnan(DHT22_temperature)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
// Read voltage and calculate
voltage = analogRead(voltageRead);
voltage110 = (voltage / 1023.0) * 110; // Corrected calculation with floating-point division
// Read digital states
powerState = digitalRead(power);
chargeState = digitalRead(chrgeFail);
voltageState1 = digitalRead(highVolt);
voltageState2 = digitalRead(lowVolt);
lcd.setCursor(0, 0); // Display the actual temperature of the battery
lcd.print("UPS ROOM");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(DHT22_temperature);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("NO ALARM ");
digitalWrite(LED, LOW);
// Alarm logic
bool alarmActive = false;
if (chargeState == HIGH) {
digitalWrite(LED, HIGH);
lcd.setCursor(0, 2);
lcd.print("CHARGER FAIL ALARM");
alarmActive = true;
} else if (voltageState2 == HIGH) {
digitalWrite(LED, HIGH);
lcd.setCursor(0, 2);
lcd.print("LOW DC VOLTAGE ");
alarmActive = true;
} else if (voltageState1 == HIGH) {
digitalWrite(LED, HIGH);
lcd.setCursor(0, 2);
lcd.print("HIGH DC VOLTAGE ");
alarmActive = true;
} else if (powerState == HIGH) {
digitalWrite(LED, HIGH);
lcd.setCursor(0, 2);
lcd.print("POWER FAILURE ALARM");
alarmActive = true;
}
if (DHT22_temperature > 30) { // Activate the LED representing the fan when the temperature is above 30 degrees
digitalWrite(LED2, HIGH);
lcd.setCursor(0, 3);
lcd.print("OVERHEATING!!");
} else {
digitalWrite(LED2, LOW);
lcd.setCursor(0, 3);
lcd.print("");
}
if (DHT22_temperature < 30) { // Activate the LED representing the fan when the temperature is above 30 degrees
digitalWrite(LED2, LOW);
lcd.setCursor(0, 3);
lcd.print("");
} else {
lcd.setCursor(0, 3);
lcd.print("");
}
delay(1000);
}