#include <Wire.h>
#include <LiquidCrystal.h>
#include <DHT.h>
// Pin definitions for LCD (Parallel connection)
#define LCD_RS A0
#define LCD_E A1
#define LCD_D4 A2
#define LCD_D5 A3
#define LCD_D6 A4
#define LCD_D7 A5
// Pin definitions for sensors
#define DHT_PIN PB0 // DHT22 (Temperature & Humidity)
#define MQ2_PIN A6 // MQ2 (Gas sensor)
#define PIR_PIN A7 // PIR (Motion detection)
#define TRIG_PIN PB1 // Ultrasonic Trigger
#define ECHO_PIN PB10 // Ultrasonic Echo
// Constants for RGB LEDs
#define LED_RED PB12 // Red LED
#define LED_GREEN PB13 // Green LED
#define LED_BLUE PB14 // Blue LED
// Initialize LCD
LiquidCrystal lcd(LCD_RS, LCD_E, LCD_D4, LCD_D5, LCD_D6, LCD_D7);
// Initialize DHT22 sensor
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
void setup() {
// LCD setup
lcd.begin(16, 2);
lcd.print("Env Monitor");
delay(2000);
// Initialize sensors
dht.begin();
pinMode(PIR_PIN, INPUT);
pinMode(TRIG_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
pinMode(LED_RED, OUTPUT);
pinMode(LED_GREEN, OUTPUT);
pinMode(LED_BLUE, OUTPUT);
// Serial for debugging
Serial.begin(9600);
}
// Function to read distance from the Ultrasonic sensor
long readDistance() {
digitalWrite(TRIG_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIG_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIG_PIN, LOW);
long duration = pulseIn(ECHO_PIN, HIGH, 30000); // timeout 30ms
if (duration == 0) {
return 400; // Default max distance in cm
}
return duration * 0.034 / 2;
}
void loop() {
// Read sensor values
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
int gasLevel = analogRead(MQ2_PIN);
long distance = readDistance();
bool motion = digitalRead(PIR_PIN);
// Display temperature and humidity
if (isnan(temperature) || isnan(humidity)) {
lcd.setCursor(0, 0);
lcd.print("DHT Error ");
lcd.setCursor(0, 1);
lcd.print("Check Sensor ");
} else {
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display with 1 decimal
lcd.print(" C ");
lcd.setCursor(0, 1);
lcd.print("Hum: ");
lcd.print(humidity, 1); // Display with 1 decimal
lcd.print(" % ");
}
// Serial output for debugging
Serial.print("Temp: "); Serial.print(temperature);
Serial.print(" C, Hum: "); Serial.print(humidity);
Serial.print(" %, Gas: "); Serial.print(gasLevel);
Serial.print(", Dist: "); Serial.print(distance);
Serial.print(" cm, Motion: "); Serial.println(motion ? "Yes" : "No");
// RGB LED Logic for alerting based on different conditions
// Condition 1: High Temperature
if (temperature > 30) {
lcd.setCursor(0, 0);
lcd.print("High Temp! ");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display the temperature
digitalWrite(LED_RED, HIGH); // Turn Red LED on
digitalWrite(LED_GREEN, LOW); // Turn Green LED off
digitalWrite(LED_BLUE, LOW); // Turn Blue LED off
}
// Condition 2: Gas level too high (MQ2)
else if (gasLevel > 400) {
lcd.setCursor(0, 0);
lcd.print("High Gas Level ");
lcd.setCursor(0, 1);
lcd.print("Gas: ");
lcd.print(gasLevel); // Display the gas level
digitalWrite(LED_RED, HIGH); // Turn Red LED on
digitalWrite(LED_GREEN, HIGH); // Turn Green LED off
digitalWrite(LED_BLUE, HIGH); // Turn Blue LED off
}
// Condition 3: Distance too short (proximity alert)
else if (distance < 20) {
lcd.setCursor(0, 0);
lcd.print("Close Object! ");
lcd.setCursor(0, 1);
lcd.print("Dist: ");
lcd.print(distance); // Display the distance
digitalWrite(LED_RED, HIGH); // Turn Red LED on
digitalWrite(LED_GREEN, LOW); // Turn Green LED off
digitalWrite(LED_BLUE, LOW); // Turn Blue LED off
}
// Condition 4: Motion detected
else if (motion) {
lcd.setCursor(0, 0);
lcd.print("Motion Detected ");
lcd.setCursor(0, 1);
lcd.print("Dist: ");
lcd.print(distance); // Display the distance
digitalWrite(LED_RED, HIGH); // Turn Red LED on
digitalWrite(LED_GREEN, LOW); // Turn Green LED off
digitalWrite(LED_BLUE, LOW); // Turn Blue LED off
}
// Condition 5: Distance is being monitored (mid-range)
else if (distance > 20 && distance < 400) {
lcd.setCursor(0, 0);
lcd.print("Monitoring Dist ");
lcd.setCursor(0, 1);
lcd.print("Dist: ");
lcd.print(distance); // Display the distance
digitalWrite(LED_RED, LOW); // Turn Red LED off
digitalWrite(LED_GREEN, LOW); // Turn Green LED off
digitalWrite(LED_BLUE, HIGH); // Turn Blue LED on
}
// Default condition: Normal status
else {
lcd.setCursor(0, 0);
lcd.print("All OK ");
lcd.setCursor(0, 1);
lcd.print("Temp: ");
lcd.print(temperature, 1); // Display the temperature
lcd.print(" C ");
digitalWrite(LED_RED, LOW); // Turn Red LED off
digitalWrite(LED_GREEN, HIGH); // Turn Green LED on
digitalWrite(LED_BLUE, LOW); // Turn Blue LED off
}
delay(1000); // 1-second delay to update readings
}
Loading
stm32-bluepill
stm32-bluepill