#include <Adafruit_GFX.h> // Core graphics library
#include <SPI.h> // this is needed for display
#include <Adafruit_ST7735.h> // Hardware-specific library for ST7735
#include <Arduino.h> // this is needed for FT6206
# define WOKWI_DISPLAY 1
// The display also uses hardware SPI
#define TFT_CS 10
#define TFT_DC 9
#define TFT_RST 4
Adafruit_ST7735 display = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_RST);
#define PIN_ANALOG_IN 5
const int buttonA = 1;
const int buttonB = 2;
const int buttonC = 3;
int buttonStateA = 0;
int buttonStateB = 0;
int buttonStateC = 0;
int lastButtonState = LOW;
unsigned long lastDebounceTime = 0; // the last time the output pin was toggled
unsigned long debounceDelay = 500; // the debounce time; increase if the output flickers
// Thermistor parameters
const float R = 100000.0; // Resistance of the series resistor (100k ohm)
const float Vcc = 3.3; // Voltage supplied to the thermistor
// Steinhart-Hart coefficients
float A, B, C;
void setup()
{
Serial.begin(115200);
Serial.println("Press the button.");
pinMode(buttonA, INPUT);
pinMode(buttonB, INPUT);
pinMode(buttonC, INPUT);
analogReadResolution(10);
display.initR(INITR_BLACKTAB);
display.fillScreen(ST7735_BLACK);
display.setTextColor(ST7735_WHITE);
// display.setFont(&FreeSans9pt7b); // FreeFont
display.setTextSize(1);
// display.setRotation(3);
// Correct Wokwi rotation
uint8_t madctl = 0x28;
display.sendCommand(0x36, &madctl, 1);
initScreen();
calculateSteinhartHartCoefficients();
delay(1000); // Delay for readability
}
void loop()
{
int rawValue = analogRead(PIN_ANALOG_IN);
float temperatureC = calculateTemperatureCelsius(rawValue);
float temperatureF = convertCelsiusToFahrenheit(temperatureC);
Serial.print("ADC Value: ");
Serial.println(rawValue);
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
Serial.print(temperatureF);
Serial.println(" °F");
display.fillRect(54, 20, 80, 7, ST7735_BLACK);
display.setCursor(54, 20);
display.print(temperatureF);
display.print(" ");
display.write(0xF8);
display.print("F");
delay(1000);
}
// Function to calculate the resistance from ADC value
float calculateResistance(int adcValue) {
float Vout = (adcValue / 1023.0) * Vcc; // 10-bit ADC
return R * (Vcc / Vout - 1.0);
}
void calculateSteinhartHartCoefficients() {
int i = 0;
Serial.println("Please place the thermistor in known temperatures (e.g., ice water, room temperature, boiling water).");
Serial.println("Then enter the corresponding raw ADC values.");
Serial.println("For accurate results, ensure the temperatures are sufficiently apart (e.g., ice water and boiling water).");
// Read raw ADC values for known temperatures
int rawValues[3];
float temperatures[3] = {0.0, 25.0, 100.0};
while (i < 3) {
if ((millis() - lastDebounceTime) > debounceDelay) {
// for (int i = 0; i < 3; ++i) {
Serial.print("Enter raw ADC value for temperature for Celsius ");
Serial.print(i + 1);
Serial.print(": ");
Serial.println(temperatures[i]);
buttonStateA = LOW;
buttonStateB = LOW;
buttonStateC = LOW;
while (digitalRead(buttonA) == LOW && digitalRead(buttonB) == LOW && digitalRead(buttonC) == LOW);
buttonStateA = digitalRead(buttonA);
buttonStateB = digitalRead(buttonB);
buttonStateC = digitalRead(buttonC);
if (buttonStateA == HIGH) {
lastDebounceTime = millis();
temperatures[i] = temperatures[i] - 1.0;
} else if (buttonStateB == HIGH) {
lastDebounceTime = millis();
rawValues[i] = analogRead(PIN_ANALOG_IN);
Serial.print("ADC Value: ");
Serial.println(rawValues[i]);
i++;
} else if (buttonStateC == HIGH) {
lastDebounceTime = millis();
temperatures[i] = temperatures[i] + 1.0;
}
}
buttonStateA = LOW;
buttonStateB = LOW;
buttonStateC = LOW;
// delay(500);
}
Serial.println("ADC Values and Temps:");
for (int i = 0; i < 3; ++i) {
Serial.print("ADC ");
Serial.print(i);
Serial.print(": Value: ");
Serial.print(rawValues[i]);
Serial.print(" Temp: ");
Serial.println(temperatures[i]);
}
float L1 = log(calculateResistance(rawValues[0]));
float L2 = log(calculateResistance(rawValues[1]));
float L3 = log(calculateResistance(rawValues[2]));
float Y1 = 1.0 / (temperatures[0] + 273.15);
float Y2 = 1.0 / (temperatures[1] + 273.15);
float Y3 = 1.0 / (temperatures[2] + 273.15);
float gamma2 = (Y2 - Y1) / (L2 - L1);
float gamma3 = (Y3 - Y1) / (L3 - L1);
C = ((gamma3 - gamma2) / (L3 - L2)) * pow((L1 + L2 + L3), -1);
B = gamma2 - (C * (pow(L1, 2) + L1 * L2 + pow(L2, 2)));
A = Y1 - (B * L1) - (C * pow(L1, 3));
Serial.print("Steinhart-Hart Coefficients:\nA = ");
Serial.println(A, 10);
Serial.print("B = ");
Serial.println(B, 10);
Serial.print("C = ");
Serial.println(C, 10);
}
// Function to convert Celsius to Fahrenheit
float convertCelsiusToFahrenheit(float temperatureC) {
return (temperatureC * 9.0 / 5.0) + 32.0;
}
// Function to calculate temperature using Steinhart-Hart equation
float calculateTemperatureCelsius(int adcValue) {
float resistance = calculateResistance(adcValue);
float logR = log(resistance);
float invT = A + B * logR + C * pow(logR, 3);
float temperatureK = 1.0 / invT;
return temperatureK - 273.15; // Convert Kelvin to Celsius
}
void initScreen()
{
display.setCursor(0, 0);
display.print("8 Channel BBQ Monitor");
for (int i = 1; i < 9; i++)
{
display.setCursor(0, (i * 10) + 10);
display.print("Probe ");
display.print(i);
display.print(": 0 ");
display.write(0xF8);
display.print("F");
}
}