#include <TinyWireM.h> // I2C library for ATtiny
#include <Tiny4kOLED.h> // SSD1306 OLED library for ATtiny
#include <OneWire.h> // OneWire bus communication
#include <DallasTemperature.h> // DS18B20 temperature sensor library
// Pin definitions
#define ONE_WIRE_BUS 4 // DS18B20 data pin on PB4
#define SDA_PIN 0 // I2C SDA on PB0
#define SCL_PIN 2 // I2C SCL on PB2
// Setup OneWire and DallasTemperature objects
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Variables
float tempF;
void setup() {
// Initialize I2C
TinyWireM.begin();
// Initialize OLED display (128x64)
oled.begin();
oled.clear();
oled.on();
oled.setFont(FONT8X16); // Use larger font for big numbers
// Initialize temperature sensor
sensors.begin();
}
void loop() {
// Request temperature reading
sensors.requestTemperatures();
// Get temperature in Celsius and convert to Fahrenheit
float tempF = sensors.getTempFByIndex(0);
//tempF = (tempC * 9.0 / 5.0) + 32.0;
// Clear display
oled.clear();
// Position cursor
oled.setCursor(0, 0);
// Print temperature in big numbers
if (tempF >= 100) {
oled.print(tempF, 1); // One decimal place
} else if (tempF >= 10) {
oled.print(" "); // Add padding for alignment
oled.print(tempF, 1);
} else {
oled.print(" "); // More padding for single digits
oled.print(tempF, 1);
}
// Add degree symbol and F
oled.setCursor(100, 0);
oled.print((char)176); // Degree symbol
oled.print("F");
delay(100); // Update every second
}