#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_ADDR 0x3C
// NTC Temperature Sensor Pin (use an analog pin on STM32, e.g., PA0)
#define NTC_PIN PA0
// NTC Parameters (from Wokwi reference)
const float BETA = 3950; // Beta coefficient of the thermistor
const float SERIES_RESISTOR = 10000; // 10K series resistor
const float NOMINAL_RESISTANCE = 10000; // Resistance at 25°C
const float NOMINAL_TEMPERATURE = 298.15; // 25°C in Kelvin
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
// thermostat icon
const unsigned char temp_icon [] PROGMEM = {
0x00, 0x3c, 0x00, 0x00, 0x7e, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00, 0x42, 0x00, 0x00,
0x42, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a,
0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a, 0x00, 0x00, 0x5a, 0x00, 0x00, 0xdb, 0x00, 0x01, 0x99, 0x80,
0x01, 0x18, 0x80, 0x01, 0x3c, 0x80, 0x01, 0x3c, 0x80, 0x01, 0x3c, 0x80, 0x01, 0x18, 0x80, 0x01,
0x81, 0x80, 0x00, 0xe7, 0x00, 0x00, 0x3c, 0x00
};
// drop icon
const unsigned char drop_icon[] PROGMEM = {
0x01, 0x80, 0x01, 0x80, 0x03, 0xC0, 0x03, 0xC0, 0x07, 0xE0, 0x07, 0xE0,
0x0F, 0xF0, 0x0F, 0xF0, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8, 0x1F, 0xF8,
0x0F, 0xF0, 0x0F, 0xF0, 0x07, 0xE0, 0x03, 0xC0, 0x01, 0x80, 0x00, 0x00
};
// Function to read temperature from NTC sensor
float readTemperature() {
int analogValue = analogRead(NTC_PIN);
// Prevent division by zero
if (analogValue == 0) return -999; // Error value
// Convert analog reading to temperature using Steinhart-Hart equation
// Formula from Wokwi reference: celsius = 1 / (log(1 / (1023. / analogValue - 1)) / BETA + 1.0 / 298.15) - 273.15
float celsius = 1.0 / (log(1.0 / (1023.0 / analogValue - 1.0)) / BETA + 1.0 / 298.15) - 273.15;
return celsius;
}
void displayData(float temp, int hum) {
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
// ===== TEMPERATURE =====
display.drawBitmap(4, 4, temp_icon, 24, 24, SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(32, 4);
display.print("TEMPERATURE");
display.setTextSize(2);
display.setCursor(32, 16);
// Check for sensor error
if (temp < -100) {
display.print("ERR");
} else {
display.print(temp, 1);
display.print("C");
}
// Divider
display.drawLine(0, 32, 127, 32, SSD1306_WHITE);
// ===== HUMIDITY =====
display.drawBitmap(6, 40, drop_icon, 16, 16, SSD1306_WHITE);
display.setTextSize(1);
display.setCursor(32, 36);
display.print("HUMIDITY");
display.setTextSize(2);
display.setCursor(32, 48);
display.print(hum);
display.print("%");
display.display();
}
void setup() {
Serial.begin(115200);
// Initialize I2C for STM32 (PB7=SDA, PB6=SCL)
Wire.begin(PB7, PB6);
// Initialize OLED
if (!display.begin(SSD1306_SWITCHCAPVCC, OLED_ADDR)) {
Serial.println(F("SSD1306 allocation failed"));
while (true);
}
// Set analog reference if needed (STM32 specific)
analogReadResolution(10); // Set to 10-bit for Wokwi compatibility, use 12 for real STM32
Serial.println("NTC Temperature Sensor Initialized");
}
void loop() {
// Read real temperature from NTC sensor
float temperature = readTemperature(); // or use readTemperatureSTM32() for hardware
// Simulated humidity (add DHT11/22 if you want real humidity)
int humidity = 60; // Placeholder - replace with actual sensor if needed
// Debug output to Serial
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" C");
// Display on OLED
displayData(temperature, humidity);
delay(1000);
}