import onewire
The onewire module drives a generic Dallas/Maxim OneWire bus from firmware. It handles bus reset, ROM discovery, device selection, byte transfers, and CRC-8. Device-specific behavior, such as DS18B20 temperature conversion, belongs in a separate sensor module layered on top of this bus API.
Quick example
import onewire
onewire.begin(5)
devices = onewire.search()
for rom in devices:
print(rom)
if len(devices) > 0:
onewire.reset()
onewire.select(devices[0])
onewire.write_byte(0x44)
importonewire
OneWire native Berry module.
The onewire module drives a generic Dallas/Maxim OneWire bus from firmware. It handles bus reset, ROM discovery, device selection, byte transfers, and CRC-8. Device-specific behavior, such as DS18B20 temperature conversion, belongs in a separate sensor module layered on top of this bus API.
onewire.begin(pin: int) -> None
onewire.begin(pin: int) -> NoneAttach OneWire to a GPIO pin.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
pin |
int |
positional or keyword | Yes | GPIO number used as the OneWire data pin. |
None None.
onewire.reset() -> bool
onewire.reset() -> boolReset the OneWire bus and detect device presence.
bool True when at least one device presence pulse is detected.
onewire.search() -> list
onewire.search() -> listDiscover OneWire devices on the bus.
Each returned ROM is a 16-character lowercase hex string in OneWire bus byte order. Pass the returned string directly to ``select(rom)``.
list List of discovered ROM hex strings.
onewire.select(rom: str) -> None
onewire.select(rom: str) -> NoneSelect one specific OneWire device by ROM ID.
Call this after ``reset()`` and before a device-specific command. The ROM value can be a string returned by ``search()``, 8 bytes, or 8 integer byte values.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
rom |
str |
positional or keyword | Yes | ROM ID to select. |
None None.
onewire.skip() -> None
onewire.skip() -> NoneSend the Skip ROM command.
Use this after ``reset()`` when there is only one device on the bus, or when broadcasting a command supported by all connected devices.
None None.
onewire.write_byte(value: int) -> None
onewire.write_byte(value: int) -> NoneWrite one byte to the OneWire bus.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
value |
int |
positional or keyword | Yes | Byte value from 0 to 255. |
None None.
onewire.read_byte() -> int
onewire.read_byte() -> intRead one byte from the OneWire bus.
int Integer byte value from 0 to 255.
onewire.write_bytes(data: list) -> None
onewire.write_bytes(data: list) -> NoneWrite multiple bytes to the OneWire bus.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
list |
positional or keyword | Yes | Bytes, string, or list of integer byte values. |
None None.
onewire.read_bytes(count: int) -> bytes
onewire.read_bytes(count: int) -> bytesRead multiple bytes from the OneWire bus.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
count |
int |
positional or keyword | Yes | Number of bytes to read. |
bytes Bytes-like native value.
onewire.crc8(data: list) -> int
onewire.crc8(data: list) -> intCalculate the Dallas/Maxim OneWire CRC-8.
Use this to validate ROM IDs and device scratchpad responses.
| Name | Type | Pass as | Required | Description |
|---|---|---|---|---|
data |
list |
positional or keyword | Yes | Bytes, ROM hex string, string, or list of integer byte values. |
int Integer CRC-8 value from 0 to 255.