import ds18b20
The ds18b20 module reads Dallas/Maxim DS18B20 digital temperature sensors over a OneWire data pin. It provides a sensor-friendly API on top of the raw OneWire bus: scan attached DS18B20 ROM IDs, read Celsius/Fahrenheit values, and adjust sensor resolution.
Quick example
import ds18b20
ds18b20.begin(5)
reading = ds18b20.read()
print(reading["c"])
sensors = ds18b20.scan()
for rom in sensors:
print(rom, ds18b20.temperature(rom))
importds18b20
DS18B20 temperature sensor module.
The ds18b20 module reads Dallas/Maxim DS18B20 digital temperature sensors over a OneWire data pin. It provides a sensor-friendly API on top of the raw OneWire bus: scan attached DS18B20 ROM IDs, read Celsius/Fahrenheit values, and adjust sensor resolution.
ds18b20.begin(pin: int, parasite_power: bool = None) -> None
ds18b20.begin(pin: int, parasite_power: bool = None) -> NoneAttach DS18B20 support to a GPIO pin.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | GPIO number connected to the DS18B20 DQ data line. |
parasite_power |
bool |
positional or keyword | No | Optional bool. Set True only when the sensor is powered from the data line instead of VDD. Defaults to False. |
None None.
ds18b20.scan() -> list
ds18b20.scan() -> listFind DS18B20 sensors on the OneWire bus.
Only ROM IDs with the DS18B20 family code and valid Dallas CRC are returned. Each ROM is a 16-character lowercase hex string.
list List of DS18B20 ROM hex strings.
ds18b20.count() -> int
ds18b20.count() -> intCount DS18B20 sensors on the OneWire bus.
int Integer number of discovered DS18B20 sensors.
ds18b20.convert(rom: str = None, wait: bool = None) -> bool
ds18b20.convert(rom: str = None, wait: bool = None) -> boolStart a temperature conversion.
Use this when you want to start conversion manually, do other work, and read the scratchpad later. Most scripts should use ``read()`` or ``temperature()`` instead because they start conversion and wait automatically.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
rom |
str |
positional or keyword | No | Optional DS18B20 ROM ID. Omit when exactly one sensor is on the bus. |
wait |
bool |
positional or keyword | No | Optional bool. Wait for conversion to finish before returning. Defaults to True. Keep True when parasite_power is enabled. |
bool True when the conversion command was accepted and, if waiting, completed before timeout.
ds18b20.read_scratchpad(rom: str = None) -> bytes
ds18b20.read_scratchpad(rom: str = None) -> bytesRead and validate the DS18B20 scratchpad.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
rom |
str |
positional or keyword | No | Optional DS18B20 ROM ID. Omit when exactly one sensor is on the bus. |
bytes Bytes-like native value containing the 9-byte scratchpad.
ds18b20.read(rom: str = None) -> dict
ds18b20.read(rom: str = None) -> dictRead temperature and sensor metadata.
This starts a temperature conversion, waits for it to complete, reads the scratchpad, checks its CRC, and returns a dict.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
rom |
str |
positional or keyword | No | Optional DS18B20 ROM ID. Omit when exactly one sensor is on the bus. |
dict Dict with ``ok``, ``rom``, ``c``, ``f``, ``raw``, ``resolution``, ``pin``, and ``parasite_power`` fields.
ds18b20.temperature(rom: str = None) -> float
ds18b20.temperature(rom: str = None) -> floatRead temperature in Celsius.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
rom |
str |
positional or keyword | No | Optional DS18B20 ROM ID. Omit when exactly one sensor is on the bus. |
float Float temperature in degrees Celsius.
ds18b20.set_resolution(resolution: int, rom: str = None, persist: bool = None) -> bool
ds18b20.set_resolution(resolution: int, rom: str = None, persist: bool = None) -> boolSet DS18B20 conversion resolution.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
resolution |
int |
positional or keyword | Yes | Resolution in bits. Must be 9, 10, 11, or 12. |
rom |
str |
positional or keyword | No | Optional DS18B20 ROM ID. Omit when exactly one sensor is on the bus. |
persist |
bool |
positional or keyword | No | Optional bool. When True, copy the setting to the sensor's EEPROM. Defaults to False. |
bool True when the setting was written.
ds18b20.crc8(data: list) -> int
ds18b20.crc8(data: list) -> intCalculate the Dallas/Maxim OneWire CRC-8.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
list |
positional or keyword | Yes | Bytes, string, or list of integer byte values. |
int Integer CRC-8 value from 0 to 255.
ds18b20.info() -> dict
ds18b20.info() -> dictReturn DS18B20 module state.
dict Dict with ``initialized``, ``pin``, ``parasite_power``, and ``default_resolution`` fields.