#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <DHT.h>
#include <EEPROM.h>
// Project label: LABORATORY_PROJECT_ED05
// Set the I2C address of the LCD
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Set the pin where the DS18B20 is connected
#define ONE_WIRE_BUS 7
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Set the pin where the PIR sensor is connected
#define PIR_PIN 8
// Set the pin and type of the DHT22 sensor
#define DHT_PIN 9
#define DHT_TYPE DHT22
DHT dht(DHT_PIN, DHT_TYPE);
// EEPROM addresses for storing sensor data
#define EEPROM_TEMP_DS18B20_ADDR 0
#define EEPROM_TEMP_DHT22_ADDR 4
#define EEPROM_HUMIDITY_ADDR 8
#define EEPROM_PIR_ADDR 12
void setup() {
// Initialize the LCD
lcd.begin(20, 4);
lcd.backlight();
// Display the message "Mi Cholo"
lcd.setCursor(6, 0);
lcd.print("Mi Cholo");
lcd.setCursor(2, 1);
lcd.print("Mis datos son:");
// Initialize the DS18B20 sensor
sensors.begin();
// Initialize the DHT22 sensor
dht.begin();
// Set the PIR sensor pin as input
pinMode(PIR_PIN, INPUT);
// Display initial labels
lcd.setCursor(0, 2);
lcd.print("dht:");
lcd.setCursor(0, 3);
lcd.print("hum:");
lcd.setCursor(11, 2);
lcd.print("tmp:");
lcd.setCursor(14, 3);
lcd.print("Pir:");
}
void loop() {
// Request temperature reading from DS18B20
sensors.requestTemperatures();
// Get the temperature in Celsius from DS18B20
float temperatureC_DS18B20 = sensors.getTempCByIndex(0);
// Display the temperature from DS18B20 on the LCD
lcd.setCursor(15, 2);
lcd.print(" "); // Clear previous content
lcd.setCursor(15, 2);
lcd.print(temperatureC_DS18B20);
// Read the state of the PIR sensor
int pirState = digitalRead(PIR_PIN);
// Display the PIR sensor state on the LCD
lcd.setCursor(18, 3);
lcd.print(" "); // Clear previous content
lcd.setCursor(18, 3);
if (pirState == HIGH) {
lcd.print("Yes");
} else {
lcd.print("No");
}
// Read the temperature and humidity from DHT22
float temperatureC_DHT22 = dht.readTemperature();
float humidity = dht.readHumidity();
// Display the temperature from DHT22 on the LCD
lcd.setCursor(5, 2);
lcd.print(" "); // Clear previous content
lcd.setCursor(5, 2);
if (isnan(temperatureC_DHT22)) {
lcd.print("Error");
} else {
lcd.print(temperatureC_DHT22);
}
// Display the humidity from DHT22 on the LCD
lcd.setCursor(5, 3);
lcd.print(" "); // Clear previous content
lcd.setCursor(5, 3);
if (isnan(humidity)) {
lcd.print("Error");
} else {
lcd.print(humidity);
lcd.print(" %");
}
// Store sensor data to EEPROM
EEPROM.put(EEPROM_TEMP_DS18B20_ADDR, temperatureC_DS18B20);
EEPROM.put(EEPROM_TEMP_DHT22_ADDR, temperatureC_DHT22);
EEPROM.put(EEPROM_HUMIDITY_ADDR, humidity);
EEPROM.put(EEPROM_PIR_ADDR, pirState);
// Wait 1 second before the next reading
delay(1000);
}
AREA DE SENSORES