#include <Arduino.h>
#define DS18B20_PIN 2 // Replace with the actual pin where your DS18B20 sensors are connected
void writeByte(uint8_t data) {
for (int i = 0; i < 8; i++) {
digitalWrite(DS18B20_PIN, LOW);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, data & 1 ? HIGH : LOW);
delayMicroseconds(60);
digitalWrite(DS18B20_PIN, HIGH);
delayMicroseconds(2);
data >>= 1;
}
}
uint8_t readByte() {
uint8_t data = 0;
for (int i = 0; i < 8; i++) {
digitalWrite(DS18B20_PIN, LOW);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, HIGH);
delayMicroseconds(2);
data |= digitalRead(DS18B20_PIN) << i;
delayMicroseconds(60);
}
return data;
}
void readROM(uint8_t* romID) {
writeByte(0x33); // Read ROM command
for (int i = 0; i < 8; i++) {
romID[i] = readByte();
}
}
void selectROM(const uint8_t* romID) {
writeByte(0x55); // Match ROM command
for (int i = 0; i < 8; i++) {
writeByte(romID[i]);
}
}
float readTemperature() {
digitalWrite(DS18B20_PIN, LOW);
delayMicroseconds(500);
digitalWrite(DS18B20_PIN, HIGH);
delayMicroseconds(30);
pinMode(DS18B20_PIN, INPUT);
while (digitalRead(DS18B20_PIN) == LOW);
delayMicroseconds(100);
if (digitalRead(DS18B20_PIN) == LOW) {
return -127.0; // Indicates a problem with communication
}
delayMicroseconds(100);
writeByte(0xCC); // Skip ROM
writeByte(0x44); // Convert T
delay(750); // Wait for conversion to complete (750ms for 12-bit resolution)
digitalWrite(DS18B20_PIN, LOW);
delayMicroseconds(2);
digitalWrite(DS18B20_PIN, HIGH);
delayMicroseconds(2);
pinMode(DS18B20_PIN, INPUT);
while (digitalRead(DS18B20_PIN) == LOW);
delayMicroseconds(100);
if (digitalRead(DS18B20_PIN) == LOW) {
return -127.0; // Indicates a problem with communication
}
writeByte(0xCC); // Skip ROM
writeByte(0xBE); // Read Scratchpad
int16_t rawTemperature = readByte() | (readByte() << 8);
// The DS18B20 returns the temperature in 1/16 degrees Celsius
return static_cast<float>(rawTemperature) * 0.0625;
}
bool checkCRC(const uint8_t* data, int length) {
uint8_t crc = 0;
for (int i = 0; i < length; i++) {
uint8_t inbyte = data[i];
for (int j = 0; j < 8; j++) {
uint8_t mix = (crc ^ inbyte) & 0x01;
crc >>= 1;
if (mix) {
crc ^= 0x8C;
}
inbyte >>= 1;
}
}
return (crc == 0);
}
void setup() {
Serial.begin(9600);
}
void loop() {
uint8_t romID1[8], romID2[8];
float temperature1, temperature2;
Serial.println("Reading IDs...");
// Read the ROM IDs of the sensors
readROM(romID1);
readROM(romID2);
// Check CRC for the ROM IDs
if (!checkCRC(romID1, 8) || !checkCRC(romID2, 8)) {
Serial.println("CRC check failed. Skipping reading temperatures.");
delay(5000);
return;
}
Serial.print("Sensor 1 ID: ");
for (int i = 0; i < 8; i++) {
Serial.print(romID1[i], HEX);
Serial.print(" ");
}
Serial.println();
Serial.print("Sensor 2 ID: ");
for (int i = 0; i < 8; i++) {
Serial.print(romID2[i], HEX);
Serial.print(" ");
}
Serial.println();
// Select and read temperature from Sensor 1
Serial.println("Reading temperature from Sensor 1...");
selectROM(romID1);
temperature1 = readTemperature();
// Select and read temperature from Sensor 2
Serial.println("Reading temperature from Sensor 2...");
selectROM(romID2);
temperature2 = readTemperature();
// Display the temperatures
if (temperature1 != -127.0) {
Serial.print("Temperature from Sensor 1: ");
Serial.print(temperature1);
Serial.println(" °C");
} else {
Serial.println("Error reading temperature from Sensor 1");
}
if (temperature2 != -127.0) {
Serial.print("Temperature from Sensor 2: ");
Serial.print(temperature2);
Serial.println(" °C");
} else {
Serial.println("Error reading temperature from Sensor 2");
}
delay(5000); // Delay before the next reading
}