Skip to content

Instantly share code, notes, and snippets.

@xoff00
Created September 2, 2025 19:35
Show Gist options
  • Select an option

  • Save xoff00/a35f7b6e859117ffdb3964a75ab6ad6f to your computer and use it in GitHub Desktop.

Select an option

Save xoff00/a35f7b6e859117ffdb3964a75ab6ad6f to your computer and use it in GitHub Desktop.
ESP32 DS18B20 address
// Determine DS18B20 addresses (thanks ChatGPT)
//
// How to use
//
// In Arduino IDE, install OneWire and DallasTemperature (Library Manager).
//
// Wire all DS18B20 sensors in parallel on the same data line with a 4.7 kΩ pull-up from DQ to Vdd.
//
// Vdd → 3.3V or 5V
//
// GND → GND
//
// DQ → pin 2 (or change ONE_WIRE_BUS).
//
// Upload, open Serial Monitor at 115200 baud.
//
// You’ll get a numbered list of all addresses found. The loop then prints each sensor’s current temperature
// alongside its address every 5 seconds—super handy for tagging which probe is which.
#include <OneWire.h>
#include <DallasTemperature.h>
// ==== CONFIG ====
// Data pin for the DS18B20 bus:
#define ONE_WIRE_BUS 2
// Max number of sensors you expect on the bus (adjust if needed)
#define MAX_SENSORS 20
// =================
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
uint8_t foundAddrs[MAX_SENSORS][8];
int sensorCount = 0;
void printAddr(const uint8_t a[8]) {
for (uint8_t i = 0; i < 8; i++) {
if (a[i] < 16) Serial.print('0');
Serial.print(a[i], HEX);
if (i < 7) Serial.print(' ');
}
}
bool isValidDS18B20(const uint8_t a[8]) {
// Family code 0x28 for DS18B20
if (a[0] != 0x28) return false;
// Check CRC: last byte is CRC of first 7 bytes
return OneWire::crc8(a, 7) == a[7];
}
void scanBusForSensors() {
sensorCount = 0;
oneWire.reset_search();
uint8_t addr[8];
while (oneWire.search(addr)) {
if (sensorCount >= MAX_SENSORS) break;
if (!isValidDS18B20(addr)) {
Serial.print("Skipping non-DS18B20 or bad CRC: ");
printAddr(addr);
Serial.println();
continue;
}
// Store the address
for (uint8_t i = 0; i < 8; i++) {
foundAddrs[sensorCount][i] = addr[i];
}
sensorCount++;
}
}
void setup() {
Serial.begin(115200);
delay(200);
Serial.println("\n=== DS18B20 Multi-Sensor Address Finder ===");
Serial.println("Scanning 1-Wire bus...\n");
scanBusForSensors();
if (sensorCount == 0) {
Serial.println("No valid DS18B20 sensors found. Check wiring and pull-up (4.7k).");
} else {
Serial.print("Found ");
Serial.print(sensorCount);
Serial.println(" DS18B20 sensor(s):");
for (int i = 0; i < sensorCount; i++) {
Serial.print(" [");
Serial.print(i);
Serial.print("] ");
printAddr(foundAddrs[i]);
Serial.println();
}
}
// Init DallasTemperature so we can optionally read temps
sensors.begin();
// Optional: show if parasite power is detected
Serial.print("\nParasite power: ");
Serial.println(sensors.isParasitePowerMode() ? "ON" : "OFF");
Serial.println("\nReading temperatures every 5s (press reset to rescan)...\n");
}
void loop() {
if (sensorCount == 0) {
delay(3000);
return;
}
sensors.requestTemperatures();
for (int i = 0; i < sensorCount; i++) {
// Convert stored address into DeviceAddress type
DeviceAddress da;
for (uint8_t b = 0; b < 8; b++) da[b] = foundAddrs[i][b];
float c = sensors.getTempC(da);
Serial.print("[");
Serial.print(i);
Serial.print("] ");
printAddr(foundAddrs[i]);
Serial.print(" => ");
if (c == DEVICE_DISCONNECTED_C) {
Serial.println("Temp: DISCONNECTED");
} else {
Serial.print("Temp: ");
Serial.print(c, 2);
Serial.println(" °C");
}
}
Serial.println();
delay(5000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment