/*
Arduino | project-showcase
Check out my Arduino project on Wokwi! What do you think?
THe boy March 27, 2025 — 7:19 AM
Hey everyone!
I just put together an Arduino project on Wokwi and wanted
to share it with you all!
https://wokwi.com/projects/421250116844781569
Would love to hear your thoughts!
Any suggestions to make it better?
Let me know!
*/
#include <Wire.h>
#include <dht.h>
#include <LiquidCrystal_I2C.h>
// interval constant
const unsigned long TWO_SECONDS = 2000;
// pin constants
const int BLU_LED_PIN = 6;
const int GRN_LED_PIN = 5;
const int RED_LED_PIN = 4;
const int DHT_PIN = 2;
// global variable
unsigned long lastTime = 0;
// object creation
dht myDht;
LiquidCrystal_I2C myLcd(0x27, 16, 2);
void updateDisplay(int t, int h) {
char buffer[16];
// show temperature
snprintf(buffer, 16, "Temp : %3d%cC", t, char(223));
myLcd.setCursor(0, 0);
myLcd.print(buffer);
// show humidity
snprintf(buffer, 16, "Humid : %3d%%", h);
myLcd.setCursor(0, 1);
myLcd.print(buffer);
}
void updateLeds(int t) {
// set all LEDs off
digitalWrite(BLU_LED_PIN, LOW);
digitalWrite(GRN_LED_PIN, LOW);
digitalWrite(RED_LED_PIN, LOW);
if (t > 30) {
digitalWrite(RED_LED_PIN, HIGH);
} else if (t < 20) {
digitalWrite(BLU_LED_PIN, HIGH);
} else {
digitalWrite(GRN_LED_PIN, HIGH);
}
}
void setup() {
Serial.begin(9600);
myLcd.init();
myLcd.backlight();
pinMode(BLU_LED_PIN, OUTPUT);
pinMode(GRN_LED_PIN, OUTPUT);
pinMode(RED_LED_PIN, OUTPUT);
// splash screen
myLcd.setCursor(3, 0);
myLcd.print("DHT22 Demo");
myLcd.setCursor(6, 1);
myLcd.print("V1.0");
delay(2000);
myLcd.clear();
}
void loop() {
if (millis() - lastTime >= TWO_SECONDS) {
lastTime = millis();
int chk = myDht.read22(DHT_PIN);
int temp = (int)myDht.temperature;
int humidity = (int)myDht.humidity;
updateDisplay(temp, humidity);
updateLeds(temp);
}
}