DS18B20 temperature sensor
Digital temperature sensor: it sends the value already converted and checked by CRC over a single data wire (1-Wire bus). Each sensor has a 64-bit serial number written at the factory, so several can share the same pin. It uses the onewire and ds18x20 modules, which already come in the firmware. Request the conversion, wait 750 ms and read. It needs a 4.7 kΩ resistor between DQ and 3V3.
Pinout
| Pin | Connect to | Note |
|---|---|---|
| GND (1) | GND | looking at the flat face with the legs down, it is the one on the left |
| DQ (2) | GPIO (e.g. 4) and a 4.7 kΩ resistor to 3V3 | 1-Wire data; several sensors can share the same GPIO |
| VDD (3) | 3V3 | accepts 3.0 to 5.5 V |
From the syllabus to a planned semester
- Teaching plan and calendar built automatically
- Class diary ready to paste into SIGA
- Slides and exercise lists with answer keys
Free Community edition · Windows
MicroPython example
import onewire, ds18x20
from machine import Pin
import time
# DQ on GPIO4, with a 4.7 kΩ resistor from DQ to 3V3
ds = ds18x20.DS18X20(onewire.OneWire(Pin(4)))
roms = ds.scan() # one ROM (8 bytes) for each sensor on the wire
print("sensors:", len(roms))
while True:
ds.convert_temp() # all sensors measure at the same time
time.sleep_ms(750) # 12 bits: up to 750 ms to convert
for rom in roms:
print(ds.read_temp(rom), "°C")
time.sleep(1)
Specifications
| Item | Value | Source |
|---|---|---|
| Power supply (VDD) | 3.0–5.5 V | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| Range | −55 to +125 °C; ± 0.5 °C from −10 to +85 °C | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| Resolution | 9 to 12 bits (0.5 to 0.0625 °C); 12 bits from the factory | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| Conversion time (max.) | 93.75 / 187.5 / 375 / 750 ms at 9 / 10 / 11 / 12 bits | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| At power-up | reads +85 °C (0550h) until the first conversion | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| DQ pull-up | ~4.7 kΩ to VDD (the text says ~5 kΩ) | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| Pin order (TO-92, flat face) | 1 GND, 2 DQ, 3 VDD | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf |
| 64-bit ROM | family 28h + 48-bit serial + CRC | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf — in the simulator the serial number comes from the part id (it is not from a real sensor) |
| Scratchpad bytes 5 to 7 | FFh, 0Ch, 10h (memory map figure) | Datasheet DS18B20 (Maxim Integrated), user-web.icecube.wisc.edu/~kitamura/NK/Datasheets/Dallas_1Wire/DS18B20.pdf — Under review: the text says "all 1s"; check on a lab sensor |
| Factory TH and TL | 75 and 70 °C (4Bh and 46h) | PyPlaca component catalog — Under review: the datasheet does not give them; values from published readings of new sensors |
| Parasite mode (VDD to GND) | not simulated | Under review: needs the "strong pull-up" during the conversion |
| Driver | onewire and ds18x20 (frozen into the firmware) | MicroPython v1.29.0: ports/esp32/boards/manifest.py and micropython-lib (onewire.py, ds18x20.py) |
Values marked "Under review" will still be checked against the parts in the lab.