// *** Thermostat with sensor DHT22 v1.1 ***
// *** Massimo Gostinelli - 27/01/2018 ***
// *** Freely editable and distributable program. ***
// *** All links are related to Arduino Nano v3.0 ***
// *** Pinout DHT22 ***
// Pin 1: Vcc (+5V)
// Pin 2: Data (SDA)
// Pin 3: NC
// Pin 4: GND
#include <SimpleDHT.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Display I2C wiring
// SDA = pin A4 (pin 23)
// SCL = pin A5 (pin 24)
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C shield address = 0x27 (0x3F); display = 16 columns, 2 rows
SimpleDHT22 dht22; // sensor type (DTH11 or DTH22)
int pinDHT22 = 4; // pin D4 data from sensor [Ta-Hr] (pin 7)
int err = SimpleDHTErrSuccess;
#define pinPOT 14 // pin A0 analog potentiometer input [Ts] (pin 19)
#define pinOUT 5 // pin D5 relay output (pin 8)
#define pinLED 13 // LED built into the Arduino board
float Ta = 99.9; // room temperature detected by the sensor, starting from 99.9 on restart the output will be OFF
float Hr = 0; // relative humidity detected by the sensor
float Tset; // value read from ADC and remapped
float Ts; // switching temperature set with the potentiometer
const float Th = 0.5; // hysteresis temperature
unsigned long previousMillis = 0; // variable to create a delay without the "delay" function
const long interval = 2500; // Delay in ms used for sensor reading that occurs every 2000 ms
// degree symbol map °
byte Grado[8] = {
0b01110,
0b01010,
0b01110,
0b00000,
0b00000,
0b00000,
0b00000,
0b00000
};
void setup() {
Serial.begin(9600);
pinMode(pinLED, OUTPUT);
pinMode(pinOUT, OUTPUT);
lcd.init(); // initializes the LCD display
lcd.createChar(0, Grado); // creates the degree symbol °
lcd.backlight(); // turns on the background light
lcd.setCursor(0, 0); // positions the cursor (column, row), starting from 0 (zero)
lcd.print("Termostato DHT22"); // print the splash-screen
lcd.setCursor(6, 1);
lcd.print("v1.1");
delay(3000); // 3000 ms delay for splash-screen
lcd.clear(); // cleans the display
}
void loop() {
//*** loop that create a delay without the "delay" function *******************************
if (millis() - previousMillis >= interval) {
previousMillis = millis(); // saves the updated value of the current milliseconds
// reads the data and reports a possible error in the flow reading coming from the sensor
if ((err = dht22.read2(pinDHT22, &Ta, &Hr, NULL)) != SimpleDHTErrSuccess) {
Serial.print("Lettura dati DHT22 fallita, err=");
Serial.println(err);
}
}
//*****************************************************************************************
Tset = map(analogRead(pinPOT), 0, 1023, 120, 250); // remapping that brings the analog value read between 120 and 250
Ts = Tset / 10; // division by 10, the settable temperature will be between 12.0 and 25.0 °C
// if the room temperature drops below the set temperature minus the hysteresis, turns on the boiler
if (Ta < Ts - Th) {
digitalWrite(pinLED, HIGH); // turns on the built-in LED
digitalWrite(pinOUT, HIGH); // turns on the boiler
lcd.setCursor(13, 1);
lcd.print("ON "); // writes ON on the display
}
// if the room temperature rises above the set temperature, it will turn off the boiler
if (Ta > Ts) {
digitalWrite(pinLED, LOW); // turns off the built-in LED
digitalWrite(pinOUT, LOW); // turns off the boiler
lcd.setCursor(13, 1);
lcd.print("OFF"); // writes OFF on the display
}
lcd.setCursor(0, 0);
lcd.print("Ta:");
lcd.print(String(Ta, 1)); // environment temperature with 1 decimal place
lcd.print((char)0); // special character, degree symbol °
//lcd.write(223); // standard degree symbol °
lcd.print("C");
lcd.setCursor(10, 0);
lcd.print("Hr:");
lcd.print(String(Hr, 0)); // relative humidity detected by the sensor with 0 decimal place
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Tc:");
lcd.print(String(Ts, 1)); // switching temperature with 1 decimal place
lcd.print((char)0); // special character, degree symbol °
//lcd.write(223); // standard degree symbol °
lcd.print("C");
}