#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
const float BETA = 3950; // Beta Coefficient of the thermistor
// OLED display I2C address (usually 0x3C or 0x3D)
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
// Initialize the display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3D)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Infinite loop if display initialization fails
}
// Clear the display
display.clearDisplay();
display.setTextSize(1); // Normal 1:1 pixel scale
display.setTextColor(SSD1306_WHITE); // Draw white text
display.setCursor(0,0); // Start at top-left corner
pinMode(3, OUTPUT); // Green LED
pinMode(5, OUTPUT); // Red LED
}
void loop() {
int analogValue = analogRead(A0);
// Avoid division by zero
if (analogValue > 0) {
float celsius = 1 / (log(1 / (1023.0 / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15;
// Print temperature to Serial Monitor
Serial.print("Temperature (°C): ");
Serial.println(celsius);
// Clear the display and print the temperature
display.clearDisplay();
display.setCursor(0, 0);
display.print("Temperature: ");
display.print(celsius, 1); // Display temperature with 1 decimal place
display.print(" C");
display.display();
// Check if temperature is in the range 20 to 25
if (celsius > 20 && celsius < 25) {
digitalWrite(3, HIGH); // Turn green LED on
delay(100);
digitalWrite(3, LOW); // Turn green LED off
delay(100);
digitalWrite(5, LOW); // Turn red LED off
} else {
digitalWrite(5, HIGH); // Turn red LED on
delay(100);
digitalWrite(5, LOW); // Turn red LED off
delay(100);
digitalWrite(3, LOW); // Turn green LED off
}
} else {
Serial.println("Invalid analog value.");
}
delay(100); // Wait 1 second before repeating
}