#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <NewPing.h>
// Pin definitions
#define DHTPIN 15
#define RED_LED_PIN 2
#define GREEN_LED_PIN 18
#define POT_VOLTAGE_PIN 34
#define POT_CURRENT_PIN 35
#define TRIG_PIN 5
#define ECHO_PIN 18
#define MAX_DISTANCE 200
// Sensor and display initialization
DHT dht(DHTPIN, DHT22);
LiquidCrystal_I2C lcd(0x27, 16, 2);
NewPing sonar(TRIG_PIN, ECHO_PIN, MAX_DISTANCE);
bool dustDetected = false;
void setup() {
// Initialize pin modes
pinMode(RED_LED_PIN, OUTPUT);
pinMode(GREEN_LED_PIN, OUTPUT);
// Initialize serial communication
Serial.begin(115200);
// Initialize the LCD display
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
// Initialize the DHT sensor
dht.begin();
}
void displayTemperature() {
float t = dht.readTemperature();
if (isnan(t)) {
Serial.println("Failed to read temperature from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Temp: Error");
return;
}
Serial.print("Temperature: ");
Serial.print(t);
Serial.println(" °C");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temperature:");
lcd.setCursor(0, 1);
lcd.print(t);
lcd.print(" C");
}
void displayHumidity() {
float h = dht.readHumidity();
if (isnan(h)) {
Serial.println("Failed to read humidity from DHT sensor!");
lcd.setCursor(0, 0);
lcd.print("Humidity: Error");
return;
}
Serial.print("Humidity: ");
Serial.print(h);
Serial.println(" %");
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Humidity:");
lcd.setCursor(0, 1);
lcd.print(h);
lcd.print(" %");
}
void readPotentiometers() {
int potVoltage = analogRead(POT_VOLTAGE_PIN);
int potCurrent = analogRead(POT_CURRENT_PIN);
float voltage = potVoltage * (3.3 / 4095.0);
float current = potCurrent * (3.3 / 4095.0);
float power = voltage * current;
float energy = power * 2.0; // Energy calculation example (adjust as needed)
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Volt: ");
lcd.print(voltage);
lcd.print(" V");
lcd.setCursor(0, 1);
lcd.print("Curr: ");
lcd.print(current);
lcd.print(" A");
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V\t");
Serial.print("Current: ");
Serial.print(current);
Serial.print(" A\t");
Serial.print("Power: ");
Serial.print(power);
Serial.println(" W");
delay(2000); // Delay to keep the values on the LCD for readability
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Power: ");
lcd.print(power);
lcd.print(" W");
lcd.setCursor(0, 1);
lcd.print("Energy: ");
lcd.print(energy);
lcd.print(" J");
}
void checkDistanceSensor() {
unsigned int distance = sonar.ping_cm();
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
if (distance > 0 && distance < 10) { // Object detected within 10 cm
if (!dustDetected) {
dustDetected = true;
digitalWrite(RED_LED_PIN, HIGH);
digitalWrite(GREEN_LED_PIN, LOW);
// Display dust detected message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Dust detected!");
}
} else { // No object detected or object further away
if (dustDetected) {
dustDetected = false;
digitalWrite(RED_LED_PIN, LOW);
// Display no dust detected message on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("No dust detected");
}
digitalWrite(GREEN_LED_PIN, HIGH);
}
}
void loop() {
displayTemperature();
delay(2000);
displayHumidity();
delay(2000);
readPotentiometers();
delay(2000);
checkDistanceSensor();
delay(2000); // Adjust delay as needed for readability and performance
}