#include <TM1637Display.h> // Library for TM1637 4-digit display
#include <OneWire.h> // Library for OneWire communication
#include <DallasTemperature.h> // Library for DS18B20 sensor
#define TM1637_CLK 1 // CLK pin for TM1637 (GPIO 6)
#define TM1637_DIO 0 // DIO pin for TM1637 (GPIO 7)
#define DS18B20_PIN 3 // Data pin for DS18B20 (GPIO 5)
// Initialize TM1637 display
TM1637Display display(TM1637_CLK, TM1637_DIO);
// Initialize OneWire and DallasTemperature for DS18B20
OneWire oneWire(DS18B20_PIN);
DallasTemperature sensors(&oneWire);
// Custom "C" symbol for Celsius
const uint8_t C[] = {
SEG_B | SEG_A | SEG_F | SEG_G, // First part of "C"
SEG_A | SEG_D | SEG_E | SEG_F // Second part of "C"
};
void setup() {
// Start serial communication for debugging
//Serial.begin(115200);
delay(1000); // Give time for Serial to initialize
// Initialize the DS18B20 sensor
sensors.begin();
// Check if sensor is detected
// Serial.print("Number of devices found: ");
// Serial.println(sensors.getDeviceCount());
// Initialize the TM1637 display
display.setBrightness(0x0f); // Max brightness
display.clear();
// Serial.println("ESP32-C3 Mini with DS18B20 and TM1637 initialized");
}
void loop() {
// Request temperature from DS18B20
//Serial.println("Requesting temperature...");
sensors.requestTemperatures();
delay(750); // Wait for conversion (DS18B20 needs ~750ms at 12-bit resolution)
float tempc = sensors.getTempCByIndex(0); // Get temperature in Celsius
// Debugging output
// Serial.print("Raw temperature reading: ");
// Serial.print(tempc);
// Serial.println(" °C");
// Check if reading is valid
if (tempc == DEVICE_DISCONNECTED_C || tempc < -55 || tempc > 125) {
display.showNumberDecEx(8888, 0, true); // Display error code
// Serial.println("Error: Sensor disconnected or invalid reading");
} else {
// Convert temperature to integer with one decimal place (e.g., 23.5 -> 235)
int tempDisplay = tempc * 1;
// Show temperature with decimal point
display.showNumberDecEx(tempDisplay, 0b0100, false, 2, 0);
display.setSegments(C, 2, 2); // Show "C" symbol
// Serial.print("Displayed temperature: ");
// Serial.print(tempc);
// Serial.println(" °C");
}
delay(1000); // Update every 1 second
}