#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
// Include the Custom Font for a professional UI
#include <Fonts/FreeSans9pt7b.h>
// OLED Setup
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
// DS18B20 Setup
#define ONE_WIRE_BUS 2
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress tempDeviceAddress;
void setup() {
Serial.begin(9600);
// Initialize OLED
if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
for(;;); // Don't proceed, loop forever if display fails
}
sensors.begin();
display.clearDisplay();
display.setTextColor(SSD1306_WHITE);
display.setRotation(2);
}
void loop() {
display.clearDisplay();
// 1. Header with Custom Font
display.setFont(&FreeSans9pt7b);
display.setCursor(0, 15); // Custom fonts use baseline (y) coordinates
display.println("Sensor ID:");
// Draw a decorative line
display.drawLine(0, 18, 120, 18, SSD1306_WHITE);
// 2. Search for the sensor serial number
oneWire.reset_search();
if (oneWire.search(tempDeviceAddress)) {
// Switch back to standard font for the long serial string (better fit)
display.setFont();
display.setCursor(0, 28);
display.print("HEX: ");
// Print the 8-byte Serial Number
for (uint8_t i = 0; i < 8; i++) {
if (tempDeviceAddress[i] < 16) display.print("0");
display.print(tempDeviceAddress[i], HEX);
if (i < 7) display.print(":");
}
// 3. Display Temperature for verification
sensors.requestTemperatures();
float tempC = sensors.getTempC(tempDeviceAddress);
display.setFont(&FreeSans9pt7b);
display.setCursor(0, 63);
display.print("Temp: ");
display.print(tempC, 1);
display.print(" C");
} else {
// Error state if no sensor is plugged in
display.setFont();
display.setCursor(0, 35);
display.println("NO SENSOR DETECTED");
display.println("Check Pin D2 & 4.7k");
}
display.display(); // Push buffer to hardware
delay(1500); // Refresh rate
}