Files
Teplotni-cidlo/dallas.ino
2026-02-27 09:37:27 +01:00

65 lines
1.3 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 8 // Pin, kde je připojen DS18B20
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
DeviceAddress sensorAddress;
bool sensorFound = false;
void printAddress(DeviceAddress deviceAddress) {
for (uint8_t i = 0; i < 8; i++) {
if (deviceAddress[i] < 16) Serial.print("0");
Serial.print(deviceAddress[i], HEX);
}
}
void setup() {
pinMode(8, INPUT_PULLUP);
Serial.begin(115200);
delay(2000);
Serial.println("Hledám DS18B20...");
sensors.begin();
if (sensors.getDeviceCount() == 0) {
Serial.println("Žádné čidlo nenalezeno!");
return;
}
if (sensors.getAddress(sensorAddress, 0)) {
sensorFound = true;
Serial.print("Čidlo nalezeno, adresa: ");
printAddress(sensorAddress);
Serial.println();
sensors.setResolution(sensorAddress, 12); // 912 bitů
} else {
Serial.println("Nepodařilo se získat adresu čidla.");
}
}
void loop() {
if (!sensorFound) {
delay(2000);
return;
}
sensors.requestTemperatures();
float temperature = sensors.getTempC(sensorAddress);
if (temperature == DEVICE_DISCONNECTED_C) {
Serial.println("Čidlo odpojeno!");
} else {
Serial.print("Teplota: ");
Serial.print(temperature);
Serial.println(" °C");
}
delay(2000);
}