#include <LiquidCrystal.h>
#include <Adafruit_GFX.h>
#include <Adafruit_LEDBackpack.h>
#include <DHT.h>
// Constants
#define DHT_PIN 2 // Pin connected to DHT sensor
#define LED_BAR_GRAPH_PIN A0 // Pin connected to LED bar graph (analog input)
#define BUZZER_PIN 4 // Pin connected to the buzzer
#define LCD_RS 7 // RS pin of the LCD
#define LCD_EN 8 // EN pin of the LCD
#define LCD_D4 9 // D4 pin of the LCD
#define LCD_D5 10 // D5 pin of the LCD
#define LCD_D6 11 // D6 pin of the LCD
#define LCD_D7 12 // D7 pin of the LCD
#define LCD_COLUMNS 16 // Number of columns on the LCD
#define LCD_ROWS 2 // Number of rows on the LCD
// Initialize DHT sensor
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// Initialize LCD display
LiquidCrystal lcd(LCD_RS, LCD_EN, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Initialize LED bar graph
Adafruit_8x16minimatrix matrix = Adafruit_8x16minimatrix();
void setup() {
// Initialize serial communication
Serial.begin(9600);
// Initialize DHT sensor
dht.begin();
// Initialize LCD display
lcd.begin(LCD_COLUMNS, LCD_ROWS);
lcd.clear();
// Initialize LED bar graph
matrix.begin(0x70); // Address of the LED bar graph (adjust if needed)
matrix.clear();
matrix.display();
}
void loop() {
// Read temperature and humidity from the DHT sensor
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
// Display temperature and humidity on the LCD
displayTemperatureAndHumidity(temperature, humidity);
// Update LED bar graph based on temperature threshold
updateLEDBarGraph(temperature);
// Activate the buzzer if the temperature exceeds 50°C
if (temperature > 50.0) {
activateBuzzer();
} else {
deactivateBuzzer();
}
delay(2000); // Delay for 2 seconds before updating again
}
void displayTemperatureAndHumidity(float temp, float humidity) {
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
}
void updateLEDBarGraph(float temp) {
int numLedsToLight = map(int(temp), 0, 100, 0, 8); // Map temperature to LEDs
matrix.clear();
for (int i = 0; i < numLedsToLight; i++) {
for (int j = 0; j < 8; j++) {
matrix.drawPixel(i, j, LED_ON);
}
}
matrix.display();
}
void activateBuzzer() {
digitalWrite(BUZZER_PIN, HIGH);
}
void deactivateBuzzer() {
digitalWrite(BUZZER_PIN, LOW);
}