#include <OneWire.h>
#include <DallasTemperature.h>
const int ds18b20Pin = 2; // Digital pin for DS18B20 DQ (data) connection
const int acPowerPin = 9; // Digital pin for AC power control
const int ledPin = 13; // Digital pin for LED
OneWire oneWire(ds18b20Pin);
DallasTemperature sensors(&oneWire);
void setup() {
pinMode(acPowerPin, OUTPUT);
pinMode(ledPin, OUTPUT); // Set LED pin as an output
Serial.begin(9600);
sensors.begin(); // Initialize the DS18B20 sensor
}
void loop() {
// Simulate temperature change (replace this with your method of temperature input)
float temperature = simulateTemperatureChange();
// Simulate AC control based on temperature
if (temperature > 25.0) {
digitalWrite(acPowerPin, HIGH); // Turn on AC
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
digitalWrite(acPowerPin, LOW); // Turn off AC
digitalWrite(ledPin, LOW); // Turn off LED
}
// Print temperature, AC status, and LED status to Serial Monitor
Serial.print("Temperature: ");
Serial.print(temperature);
Serial.print(" °C | AC Status: ");
Serial.print(digitalRead(acPowerPin) == HIGH ? "ON" : "OFF");
Serial.print(" | LED Status: ");
Serial.println(digitalRead(ledPin) == HIGH ? "ON" : "OFF");
delay(1000);
}
float simulateTemperatureChange() {
// Simulate a temperature change (replace this with your logic)
static float currentTemperature = 20.0; // Initial temperature
currentTemperature += 0.1; // Increase temperature
return currentTemperature;
}
Loading
ds18b20
ds18b20