Daily programmable thermostat with LCD interface using LM35 sensor and relay control for temperature management in four 6-hour time slots.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define I2C address of the 16x2 LCD display
LiquidCrystal_I2C lcd(0x27, 16, 2);
// LM35 temperature sensor pin
const int pinTemp = A0;
// Relay pin
const int pinRele = 8;
// Button pins
const int pinBtnUp = 2;
const int pinBtnDown = 3;
// Number of time slots (e.g., 4 slots: night, morning, afternoon, evening)
const int fasce = 4;
// Duration of each slot in hours (24/4=6 hours each)
const int durataFascia = 6;
// Target temperature array for each slot (in degrees Celsius)
int tempTarget[fasce] = {18, 21, 23, 20};
// Variable to store the selected slot for modification
int fasciaSelezionata = 0;
// Variables for button debounce management
unsigned long lastDebounceTimeUp = 0;
unsigned long lastDebounceTimeDown = 0;
const unsigned long debounceDelay = 50;
// Previous button states
int lastButtonStateUp = LOW;
int lastButtonStateDown = LOW;
void setup() {
// Initialize serial for debugging
Serial.begin(9600);
// Initialize LCD
lcd.init();
lcd.backlight();
// Configure pins
pinMode(pinRele, OUTPUT);
digitalWrite(pinRele, LOW); // Relay off at startup
pinMode(pinBtnUp, INPUT);
pinMode(pinBtnDown, INPUT);
// Display initial message
lcd.setCursor(0,0);
lcd.print("Programmable Thermostat");
lcd.setCursor(0,1);
lcd.print("Initializing");
delay(2000);
lcd.clear();
}
void loop() {
// Read ambient temperature
float tempAmb = leggiTemperatura();
// Get current time slot
int fasciaCorrente = getFasciaOraria();
// Check if relay should be on or off
if (tempAmb < tempTarget[fasciaCorrente]) {
digitalWrite(pinRele, HIGH); // Turn on heating
} else {
digitalWrite(pinRele, LOW); // Turn off heating
}
// Handle button input to modify target temperatures
gestisciPulsanti();
// Update display
aggiornaDisplay(tempAmb);
delay(200); // Small delay for stability
}
// Function to read temperature from LM35
float leggiTemperatura() {
int valoreAnalogico = analogRead(pinTemp);
// LM35: 10mV per degree, 10-bit ADC on 5V -> 5V/1024 = 4.88mV per unit
float tensione = valoreAnalogico * (5.0 / 1024.0);
float temperatura = tensione * 100.0; // Convert to °C
return temperatura;
}
// Function to determine current time slot (0..fasce-1)
int getFasciaOraria() {
// Obtain hour based on millis() simulating a clock (for simplicity)
// Every 1 real minute = 1 simulated hour
unsigned long minutiSimulati = millis() / 60000UL;
int oraSimulata = minutiSimulati % 24;
int fascia = oraSimulata / durataFascia;
return fascia;
}
// Function to handle button presses with debounce
void gestisciPulsanti() {
int readingUp = digitalRead(pinBtnUp);
int readingDown = digitalRead(pinBtnDown);
unsigned long currentTime = millis();
// UP button: change selected slot
if (readingUp != lastButtonStateUp) {
lastDebounceTimeUp = currentTime;
}
if ((currentTime - lastDebounceTimeUp) > debounceDelay) {
if (readingUp == HIGH) {
fasciaSelezionata++;
if (fasciaSelezionata >= fasce) fasciaSelezionata = 0;
}
}
lastButtonStateUp = readingUp;
// DOWN button: modify target temperature of selected slot
if (readingDown != lastButtonStateDown) {
lastDebounceTimeDown = currentTime;
}
if ((currentTime - lastDebounceTimeDown) > debounceDelay) {
if (readingDown == HIGH) {
tempTarget[fasciaSelezionata]++;
if (tempTarget[fasciaSelezionata] > 30) tempTarget[fasciaSelezionata] = 10; // cycle from 10 to 30 °C
}
}
lastButtonStateDown = readingDown;
}
// Function to update the LCD display
void aggiornaDisplay(float tempAmb) {
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Amb: ");
lcd.print(tempAmb,1);
lcd.print((char)223); // degree symbol
lcd.print("C Slot:");
lcd.print(fasciaSelezionata+1);
lcd.setCursor(0,1);
lcd.print("Target:");
lcd.print(tempTarget[fasciaSelezionata]);
lcd.print((char)223);
lcd.print("C");
// Show relay status
lcd.setCursor(12,1);
if (digitalRead(pinRele) == HIGH) {
lcd.print("ON ");
} else {
lcd.print("OFF");
}
}Arduino Uno (x1)
16x2 LCD Display with I2C interface (x1)
LM35 Temperature Sensor (x1)
5V Relay (x1)
Button (x2)
10kΩ Resistors (x2) for button pull-down
5V Power Supply
CONNECTIONS: LM35 Vout -> A0; LM35 Vcc -> 5V; LM35 GND -> GND
CONNECTIONS: Relay IN -> D8; Relay Vcc -> 5V; Relay GND -> GND
CONNECTIONS: Button1 -> D2 and GND (with 10kΩ pull-down resistor to GND)
CONNECTIONS: Button2 -> D3 and GND (with 10kΩ pull-down resistor to GND)
CONNECTIONS: LCD Display I2C SDA -> A4; SCL -> A5; Vcc -> 5V; GND -> GND