// Room Temperature Measurement with Alarm and Display on I2C LCD (NTC Thermistor Version)
// Updated with accurate NTC thermistor formula, alarm system, and button toggle using switch-case (Tinkercad compatible)
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define TEMP_SENSOR_PIN A1 // Analog pin for NTC thermistor
#define LED_PIN 12 // LED pin
#define BUZZER_PIN 4 // Buzzer pin
#define BUTTON_PIN 6 // Push button pin
#define TEMP_THRESHOLD 35.0 // Temperature threshold in Celsius
#define BETA 3950 // Beta coefficient of the thermistor
LiquidCrystal_I2C lcd(0x27, 16, 2); // I2C LCD initialization (address 0x27, 16x2)
bool buzzerEnabled = true; // State of buzzer (enabled by default)
int buttonState = HIGH; // Last button state
int lastButtonState = HIGH;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50; // Debounce time to avoid false triggers
float readTemperature() {
int analogValue = analogRead(TEMP_SENSOR_PIN); // Read raw analog value
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void setup() {
lcd.init(); // Initialize LCD
lcd.backlight(); // Turn on LCD backlight
pinMode(LED_PIN, OUTPUT);
pinMode(BUZZER_PIN, OUTPUT);
pinMode(BUTTON_PIN, INPUT_PULLUP); // Button with internal pull-up resistor
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.setCursor(0, 1);
lcd.print("Buzzer: ON ");
}
void handleButtonPress() {
int reading = digitalRead(BUTTON_PIN);
if (reading != lastButtonState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != buttonState) {
buttonState = reading;
if (buttonState == LOW) { // Button pressed
buzzerEnabled = !buzzerEnabled; // Toggle buzzer state
lcd.setCursor(0, 1);
if (buzzerEnabled) {
lcd.print("Buzzer: ON ");
delay(1000);
} else {
lcd.print("Buzzer: OFF ");
delay(1000);
}
}
}
}
lastButtonState = reading;
}
void loop() {
float temperature = readTemperature();
// Display temperature on LCD
lcd.setCursor(6, 0);
lcd.print(temperature, 1); // Display with one decimal place
lcd.print((char)223); // Degree symbol
lcd.print("C ");
handleButtonPress(); // Check button state and handle toggling
switch (buzzerEnabled) {
case true:
if (temperature >= TEMP_THRESHOLD) {
lcd.setCursor(0, 1);
lcd.print("WARNING: HOT! ");
digitalWrite(LED_PIN, HIGH);
digitalWrite(BUZZER_PIN, HIGH);
} else {
lcd.setCursor(0, 1);
lcd.print("Status: Normal");
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
}
break;
case false:
digitalWrite(LED_PIN, LOW);
digitalWrite(BUZZER_PIN, LOW);
lcd.setCursor(0, 1);
lcd.print("Buzzer: OFF "); // Ensure display shows "Buzzer: OFF" when buzzer is disabled
break;
}
delay(200); // Refresh rate for smoother readings
}