#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
// Define the pin for the DS18B20 sensor
#define ONE_WIRE_BUS 2
// OLED display dimensions
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define OLED_RESET -1
// Create instances
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
const int analogPin = A0;
void setup() {
// Start the serial communication
Serial.begin(9600);
// Initialize the DS18B20 sensor
sensors.begin();
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
Serial.println(F("SSD1306 allocation failed"));
for (;;); // Halt if display initialization fails
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0, 0);
display.display();
}
void loop() {
// Read the analog value from A0 (0 to 1023)
int analogValue = analogRead(analogPin);
// Convert the analog value to voltage (0 to 5V)
float voltage = analogValue * (5.0 / 1023.0);
// Convert the voltage to temperature (0 to 100°C)
float temperature = voltage * 20.0; // Scale factor: 5V = 100°C
// Display the temperature on the OLED
display.clearDisplay();
display.setCursor(0, 0);
display.print("Voltage: ");
display.print(voltage);
display.print(" V");
display.setCursor(0, 20);
display.print("Temperature: ");
display.print(temperature);
display.print(" C");
// Display the temperature on the serial monitor
Serial.print("Voltage: ");
Serial.print(voltage);
Serial.print(" V, Temperature: ");
Serial.print(temperature);
Serial.println(" C");
// Render the display
display.display();
// Wait for a short time before updating
delay(500);
}