Home/ Projects/ Temperature and humidity alarm
Project 5 of 16 Sensing Intermediate

Temperature and humidity alarm

Show live readings on the LCD and sound the buzzer when the room crosses a threshold you set. The interesting part is what happens at the boundary: a naive version chatters on and off as the reading wobbles by a tenth of a degree. You fix it with hysteresis — switch on at one value, off at a lower one — which is the same technique a real thermostat uses.

ESP32 & ESP8266 IoT Automation Kit
One of 16 projects in the ESP32 & ESP8266 IoT Automation Kit — the parts below are from that kit.

What this project uses

Every item is in the kit — nothing extra to buy.

Everything for this project

ESP32 & ESP8266 IoT Automation Kit

All 24 parts in one box, with this project and 15 more to build from them.

AED 180.00 See the kit
Need only these parts? Order them loose · from AED 133.04
Used in this project tick what you still need
Also needed untick if you already have them
Selected parts AED 133.04

One of each part. Packs (resistor assortment, jumper wires) already hold more than a single build needs. Backorder parts ship once they arrive — the kit above has every part boxed today. Delivery is free on orders AED 150.00+.

Sketch for this project

Written for the board in the kit. Copy it into the Arduino IDE; change the pin numbers if you wire it differently.

05 Temperature and humidity alarm Arduino C++
/*
  05 — Temperature and humidity alarm
  Board: ESP32.  Parts: DHT11, buzzer module, LCD 1602 (I2C backpack).
  Libraries: "DHT sensor library" (Adafruit) + Adafruit Unified Sensor,
             "LiquidCrystal I2C" (Frank de Brabander).

  Wiring:
    DHT11:  VCC -> 3V3, GND -> GND, DATA -> GPIO 4
    Buzzer: VCC -> 3V3 (or VIN), GND -> GND, IO -> GPIO 23   (active buzzer: HIGH = sound)
    LCD:    VCC -> VIN (5 V), GND -> GND, SDA -> GPIO 21, SCL -> GPIO 22

  The alarm switches ON above ALARM_ON and OFF only below ALARM_OFF. That gap
  (hysteresis) is what stops it chattering when the reading wobbles by 0.1 C —
  the same trick a real thermostat uses.
*/

#include "DHT.h"
#include <Wire.h>
#include <LiquidCrystal_I2C.h>

const int DHT_PIN    = 4;
const int BUZZER_PIN = 23;

const float ALARM_ON  = 30.0;   // C — set for your room
const float ALARM_OFF = 28.5;   // must be lower than ALARM_ON

DHT dht(DHT_PIN, DHT11);
LiquidCrystal_I2C lcd(0x27, 16, 2);
bool alarm = false;

void setup() {
  Serial.begin(115200);
  pinMode(BUZZER_PIN, OUTPUT);
  digitalWrite(BUZZER_PIN, LOW);
  dht.begin();
  lcd.init();
  lcd.backlight();
  lcd.print("Warming up...");
}

void loop() {
  delay(2000);
  float t = dht.readTemperature();
  float h = dht.readHumidity();
  if (isnan(t) || isnan(h)) {
    lcd.setCursor(0, 1); lcd.print("sensor error    ");
    return;
  }

  if (!alarm && t >= ALARM_ON)  alarm = true;
  if ( alarm && t <= ALARM_OFF) alarm = false;
  digitalWrite(BUZZER_PIN, alarm ? HIGH : LOW);

  lcd.setCursor(0, 0);
  lcd.print("T:"); lcd.print(t, 1); lcd.print("C  H:"); lcd.print(h, 0); lcd.print("%  ");
  lcd.setCursor(0, 1);
  lcd.print(alarm ? "ALARM  >" : "ok     >");
  lcd.print(ALARM_ON, 1); lcd.print("C      ");

  Serial.print(t); Serial.print(" C  "); Serial.print(h); Serial.print(" %  ");
  Serial.println(alarm ? "ALARM" : "ok");
}
Install: DHT sensor library + LiquidCrystal I2C. Set ALARM_ON for your room.

More from this kit

All projects →

Root
Online — usually replies instantly
Catalog assistant: verify final stock/price on the product page before checkout.