#include <OneWire.h>
#include <DallasTemperature.h>
// Data wire is connected to pin 2 on the Arduino
#define ONE_WIRE_BUS 2
// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);
// Pass the oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
// Define the RGB LED pins
const int redPin = 3;
const int greenPin = 5;
const int bluePin = 6;
void setup() {
// Start serial communication for debugging (optional)
Serial.begin(9600);
// Set the RGB LED pins as outputs
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
// Start the DS18B20 sensor
sensors.begin();
}
void loop() {
// Call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
sensors.requestTemperatures();
// Get the temperature in Celsius
float temperature = sensors.getTempCByIndex(0);
// Print temperature to serial monitor (optional)
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.println(" °C");
// Map temperature value to RGB color
int redValue;
int blueValue;
if (temperature <= 0) {
// If temperature is 0 or below, set RGB to blue
redValue = 0;
blueValue = 255;
} else {
// If temperature is above 0, gradually transition from blue to red
redValue = map(temperature, 0, 100, 0, 255);
blueValue = map(temperature, 0, 100, 255, 0);
}
// Constrain color values to stay within the defined range
redValue = constrain(redValue, 0, 255);
blueValue = constrain(blueValue, 0, 255);
// Set RGB LED color
analogWrite(redPin, redValue);
analogWrite(greenPin, 0); // Green component is kept off
analogWrite(bluePin, blueValue);
// Delay between readings (adjust as needed)
delay(1000); // Read temperature every second
}