0% found this document useful (0 votes)
12 views3 pages

Code For Tempt and Humidity (Enscie)

This document is an Arduino sketch that utilizes a DHT22 sensor to read temperature and humidity, displaying the data on a LiquidCrystal I2C LCD. It includes functionality to control two relays based on the state of two switches, allowing for different outputs depending on which switch is pressed. The program handles sensor errors and updates the display accordingly while maintaining an idle state when no switches are activated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views3 pages

Code For Tempt and Humidity (Enscie)

This document is an Arduino sketch that utilizes a DHT22 sensor to read temperature and humidity, displaying the data on a LiquidCrystal I2C LCD. It includes functionality to control two relays based on the state of two switches, allowing for different outputs depending on which switch is pressed. The program handles sensor errors and updates the display accordingly while maintaining an idle state when no switches are activated.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <DHT.

h>

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

#define DHTPIN 2

#define DHTTYPE DHT22

#define RELAY1_PIN 3

#define RELAY2_PIN 4

#define SWITCH1_PIN 5

#define SWITCH2_PIN 6

DHT dht(DHTPIN, DHTTYPE);

LiquidCrystal_I2C lcd(0x27, 16, 2);

void setup() {

lcd.begin(16, 2);

dht.begin();

pinMode(RELAY1_PIN, OUTPUT);

pinMode(RELAY2_PIN, OUTPUT);

pinMode(SWITCH1_PIN, INPUT_PULLUP);

pinMode(SWITCH2_PIN, INPUT_PULLUP);

digitalWrite(RELAY1_PIN, LOW);

digitalWrite(RELAY2_PIN, LOW);

lcd.setCursor(0, 0);
lcd.print("Temp & Humidity");

delay(2000);

void loop() {

float temperature = dht.readTemperature();

float humidity = dht.readHumidity();

if (isnan(temperature) || isnan(humidity)) {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Error reading sensor");

delay(2000);

return;

if (digitalRead(SWITCH1_PIN) == LOW) {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Temp: ");

lcd.print(temperature);

lcd.print(" C");

lcd.setCursor(0, 1);

lcd.print("Humidity: ");

lcd.print(humidity);

lcd.print(" %");

digitalWrite(RELAY1_PIN, HIGH);

digitalWrite(RELAY2_PIN, LOW);

}
else if (digitalRead(SWITCH2_PIN) == LOW) {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Switch 2 Pressed");

digitalWrite(RELAY2_PIN, HIGH);

digitalWrite(RELAY1_PIN, LOW);

else {

lcd.clear();

lcd.setCursor(0, 0);

lcd.print("Idle");

digitalWrite(RELAY1_PIN, LOW);

digitalWrite(RELAY2_PIN, LOW);

delay(2000);

You might also like