#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 our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);
const int buttonPin = 3;
const int buzzerPin = 4;
const int ledPin = 5;
const int tempThreshold = 50; // Temperature threshold in degrees Celsius
int buttonState = 0;
void setup() {
pinMode(buttonPin, INPUT_PULLUP); // Enable internal pull-up resistor for the button
pinMode(buzzerPin, OUTPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
sensors.begin();
}
void loop() {
buttonState = digitalRead(buttonPin);
sensors.requestTemperatures(); // Send the command to get temperatures
float temperatureC = sensors.getTempCByIndex(0); // Index 0 for a single sensor
Serial.print("Temperature: ");
Serial.print(temperatureC);
Serial.println(" C");
if (temperatureC >= tempThreshold) {
tone(buzzerPin, 1000); // Activate buzzer
digitalWrite(ledPin, HIGH); // Turn on LED
} else {
noTone(buzzerPin); // Stop buzzer
digitalWrite(ledPin, LOW); // Turn off LED
}
delay(1000); // Adjust delay as needed
}
Loading
ds18b20
ds18b20