#include <Wire.h>
#include <LiquidCrystal_I2C.h>
// Define LCD address and dimensions
#define LCD_ADDR 0x27
#define LCD_COLS 20
#define LCD_ROWS 4
LiquidCrystal_I2C lcd(LCD_ADDR, LCD_COLS, LCD_ROWS);
// Define potentiometer pin
const int tempSetpointPotPin = A0;
// Define temperature sensor pin
const int tempSensorPin = A1;
// Define fan output pin
const int fanPin = 9;
// Define temperature thresholds
const int temp20CResistance = 2000; // 2K ohm
const int temp4CResistance = 4000; // 4K ohm
// Define potentiometer resistance
const int potResistance = 1200; // 1.2K ohm
// Variables for temperature control
int setpointTemp = 25; // Initial setpoint temperature
int hysteresis = 2; // Hysteresis temperature
int currentTemp = 0;
void setup() {
// Initialize LCD
lcd.init();
lcd.backlight();
// Set fan pin as output
pinMode(fanPin, OUTPUT);
}
void loop() {
// Read potentiometer value
int tempSetpointReading = analogRead(tempSetpointPotPin);
// Calculate setpoint temperature based on potentiometer reading
setpointTemp = map(tempSetpointReading, 0, 1023, 4, 25);
// Read temperature sensor
int sensorValue = analogRead(tempSensorPin);
// Convert sensor value to resistance
int sensorResistance = map(sensorValue, 0, 1023, 0, 5000);
// Convert resistance to temperature
currentTemp = map(sensorResistance, temp20CResistance, temp4CResistance, 20, 4);
// Display current temperature, setpoint temperature, hysteresis, and fan status on LCD
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(currentTemp);
lcd.print("C");
lcd.setCursor(0, 1);
lcd.print("Setpoint: ");
lcd.print(setpointTemp);
lcd.print("C");
lcd.setCursor(0, 2);
lcd.print("Hysteresis: ");
lcd.print(hysteresis);
lcd.print("C");
// Check if temperature is above setpoint + hysteresis
if (currentTemp > setpointTemp + hysteresis) {
digitalWrite(fanPin, HIGH); // Turn on fan
lcd.setCursor(0, 3);
lcd.print("Fan: ON ");
} else if (currentTemp < setpointTemp) {
digitalWrite(fanPin, LOW); // Turn off fan
lcd.setCursor(0, 3);
lcd.print("Fan: OFF");
}
delay(1000); // Update every second
}