#include #include "DFRobot_RGBLCD1602.h" #include // --- Xiao ESP32-S3 I2C Pins --- // !!! FROM YOUR CONFIRMED WORKING SETUP THAT SHOWED "Hello Xiao!" !!! // !!! PLEASE VERIFY AND UPDATE THESE GPIO NUMBERS !!! // Example: If SDA is on Xiao D4 (silkscreen) and SCL is on Xiao D5 (silkscreen) const int I2C_SDA_PIN = 5; // Placeholder: GPIO6 (Xiao D4) const int I2C_SCL_PIN = 6; // Placeholder: GPIO7 (Xiao D5) // --- LCD I2C Address for RGB Controller (from DFRobot example & your scan) --- const uint8_t LCD_RGB_ADDRESS = 0x60; // The DFRobot library uses this in the constructor // --- LCD Object --- // Using the constructor that takes (RGBAddr, lcdCols, lcdRows) DFRobot_RGBLCD1602 lcd(LCD_RGB_ADDRESS, 16, 2); // --- Thermistor Configuration --- const int THERMISTOR_PIN = 2; // ADC Pin for thermistor (D2 / GPIO2 on Xiao) const float NOMINAL_RESISTANCE = 10000.0; const float NOMINAL_TEMPERATURE = 25.0; const float B_COEFFICIENT = 3950.0; const float SERIES_RESISTOR = 10000.0; const float V_IN = 3.3; // --- ADC Reading Parameters --- const int ADC_MAX = 4095; const int NUM_SAMPLES = 10; const int SAMPLE_DELAY_MS = 10; // ... (includes and all constant definitions remain the same) ... // !!! ENSURE I2C_SDA_PIN and I2C_SCL_PIN ARE 100% CORRECT FOR YOUR WIRING !!! // Example: const int I2C_SDA_PIN = 6; const int I2C_SCL_PIN = 7; void setup() { delay(1000); // Increased initial delay to 1 second for maximum power stability Serial.begin(115200); unsigned long serialStartTime = millis(); while (!Serial && (millis() - serialStartTime < 3000)) { delay(10); } Serial.println("Thermistor + DFRobot LCD - AGGRESSIVE INIT TEST"); // ... (Serial prints for pins) ... Serial.println("Attempting I2C Bus Re-initialization..."); // No Wire.end() on ESP32 usually, begin just reconfigures Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); // First attempt to initialize I2C delay(200); // Give it a moment // Some suggest a second begin can sometimes help if the bus was stuck // Wire.end(); // Not typically available/needed for ESP32 Wire like this // Wire.begin(I2C_SDA_PIN, I2C_SCL_PIN); // Serial.println("Second Wire.begin() called."); // delay(200); lcd.init(); Serial.println("lcd.init() called (Attempt 1)."); lcd.setPWM(lcd.REG_RED, 0); lcd.setPWM(lcd.REG_GREEN, 0); lcd.setPWM(lcd.REG_BLUE, 255); Serial.println("LCD Backlight set to Blue."); lcd.setCursor(0, 0); lcd.print("Temp Sensor:"); lcd.setCursor(0, 1); lcd.print("Starting..."); Serial.println("LCD Initial message displayed."); delay(1500); } void loop() { // --- Read Thermistor --- float totalAdcValue = 0; for (int i = 0; i < NUM_SAMPLES; i++) { totalAdcValue += analogRead(THERMISTOR_PIN); delay(SAMPLE_DELAY_MS); } float averageAdcValue = totalAdcValue / NUM_SAMPLES; float vOut = (averageAdcValue / ADC_MAX) * V_IN; float thermistorResistance; if (V_IN - vOut <= 1e-6) { thermistorResistance = 1e9; } else if (vOut <= 1e-6) { thermistorResistance = 1; } else { thermistorResistance = SERIES_RESISTOR * (vOut / (V_IN - vOut)); } // --- Calculate Temperature --- float temperatureCelsius = -999.0; if (thermistorResistance > 0 && NOMINAL_RESISTANCE > 0 && B_COEFFICIENT > 0) { float t0_kelvin = NOMINAL_TEMPERATURE + 273.15; float steinhart_val = thermistorResistance / NOMINAL_RESISTANCE; if (steinhart_val > 0) { steinhart_val = log(steinhart_val); steinhart_val /= B_COEFFICIENT; steinhart_val += (1.0 / t0_kelvin); if (abs(steinhart_val) > 1e-9) { temperatureCelsius = (1.0 / steinhart_val) - 273.15; } } } // --- Print to Serial Monitor (useful for debugging even standalone) --- Serial.print("Temp C: "); Serial.println(temperatureCelsius, 1); // --- Display on LCD --- lcd.clear(); lcd.setCursor(0, 0); lcd.print("Oven Temp:"); lcd.setCursor(0, 1); if (temperatureCelsius > -273.0) { char tempStrC[7]; dtostrf(temperatureCelsius, 5, 1, tempStrC); // Format for up to XXX.X lcd.print(tempStrC); lcd.print((char)223); // Degree symbol ° lcd.print("C"); // Optional: Change backlight with PWM based on temperature // (You can keep this simple or make it more complex) if (temperatureCelsius < 50) { lcd.setPWM(lcd.REG_BLUE, 255); // Cool = Blue lcd.setPWM(lcd.REG_GREEN, 0); lcd.setPWM(lcd.REG_RED, 0); } else if (temperatureCelsius < 150) { lcd.setPWM(lcd.REG_GREEN, 200); // Warm = Greenish/Yellowish lcd.setPWM(lcd.REG_BLUE, 50); lcd.setPWM(lcd.REG_RED, 150); } else { // Hot lcd.setPWM(lcd.REG_RED, 255); // Hot = Red lcd.setPWM(lcd.REG_GREEN, 0); lcd.setPWM(lcd.REG_BLUE, 0); } } else { lcd.print("Sensor Err"); lcd.setPWM(lcd.REG_RED, 255); // Error color lcd.setPWM(lcd.REG_GREEN, 0); lcd.setPWM(lcd.REG_BLUE, 0); } delay(2000); }