#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
#define ONE_WIRE_BUS 2 // Pin connected to the DS18B20 data line
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
// Define pins for the RGB LED
#define RED_PIN 9
#define GREEN_PIN 10
#define BLUE_PIN 11
void setup() {
// Initialize the OLED display
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { // Address 0x3C for 128x64
Serial.println(F("SSD1306 allocation failed"));
for (;;);
}
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
// Start the DS18B20 sensor
sensors.begin();
// Set RGB LED pins as outputs
pinMode(RED_PIN, OUTPUT);
pinMode(GREEN_PIN, OUTPUT);
pinMode(BLUE_PIN, OUTPUT);
// Start Serial communication for debugging
Serial.begin(9600);
}
void loop() {
// Request temperature from the sensor
sensors.requestTemperatures();
float temperatureC = sensors.getTempCByIndex(0);
// Display temperature on Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" °C");
// Display the temperature on the OLED screen
display.clearDisplay();
display.setCursor(0, 20);
display.print("Temperature: ");
display.print(temperatureC);
display.println(" C");
display.display();
// Change RGB LED color based on temperature
if (temperatureC < 20) {
setColor(0, 0, 255); // Blue for cold
} else if (temperatureC >= 20 && temperatureC <= 30) {
setColor(0, 255, 0); // Green for medium
} else {
setColor(255, 0, 0); // Red for hot
}
// Delay before the next reading
delay(1000);
}
// Function to set RGB LED color
void setColor(int red, int green, int blue) {
analogWrite(RED_PIN, red);
analogWrite(GREEN_PIN, green);
analogWrite(BLUE_PIN, blue);
}Loading
ssd1306
ssd1306
Loading
ds18b20
ds18b20