Daily Programmable Thermostat with LCD Display Interface

Daily programmable thermostat with LCD interface using LM35 sensor and relay control for temperature management in four 6-hour time slots.

Brief Introduction

Daily Programmable Thermostat with LCD Display Interface (arduino)
DESCRIPTION:
Digital thermostat that allows setting target temperatures for different time slots of the day, displaying information on a 16x2 LCD display. It uses an LM35 temperature sensor to read the ambient temperature and a relay to activate or deactivate a load (e.g., heating). The user interface is managed via two buttons to navigate and modify settings.

ADDITIONAL NOTES:
The project simulates an internal clock based on millis() to divide the day into 4 slots of 6 hours each. The buttons allow selecting the time slot and modifying the associated target temperature. The relay activates if the ambient temperature is below the current slot's target. The LCD display shows current temperature, selected slot, target temperature, and relay status. For a real clock, an external RTC module can be integrated.

EXPLANATIONS:
- LiquidCrystal_I2C library manages the LCD display with I2C interface to simplify wiring and code.
- The LM35 sensor provides a voltage proportional to temperature, converted via analogRead and calculations to obtain °C.
- The getFasciaOraria() function simulates the time by dividing 24 hours into 6-hour slots, using millis() to simulate time passing.
- Buttons are configured with external pull-down resistors and managed with software debounce to avoid false readings.
- The UP button changes the selected time slot for modification, while the DOWN button increments the target temperature of the selected slot, cycling between 10 and 30 °C.
- The LCD display shows on the first line the ambient temperature and current slot, on the second line the target temperature and relay status (ON/OFF).
- The relay activates only if the ambient temperature is below the current time slot's target, simulating heating activation.

Example Code: arduino

#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");
  }
}
×
×
×
Do you want to regenerate the answer?
×
Export chat
Choose export format
Generazione PDF in corso…
Allegati
×
You are about to close the chat and switch to the image generator. If you are not logged in, you will lose our chat. Do you confirm?
You are viewing a shared chat in temporary mode. It will not be saved.
×
Saved prompts
×
Private note
×
Label
×
Search all chats
×
Your insights
Analyzing…
×
AI Preferences
Assistant name 30
Answer length
AI model
Structured answers
Include examples
Use emojis
Guide me, don't solve Hints and questions instead of a ready answer
More settings →
×
Share this chat
Anyone opening this link can view the chat or add it to their profile as their own chat.
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
Shared chat
Someone shared a chat with you. Do you want to just view it or add it to your chats?
Note: the chat attachments will be shared too. Anyone who adds it will receive a copy of the files in their own folder.
×

Saved messages

Loading...

×

Chat History

example-programs · CHAT HISTORY

Loading...

AI Preferences

×
  • BasicQuick and essential answers for study
  • MediumHigher quality for study and programming
  • AdvancedComplex reasoning and detailed analysis
Explain Steps

What do you need?

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

Similar Topics