onewire — 1-Wire bus protocol
The onewire module implements the 1-Wire bus master protocol used by
devices such as the DS18x20 temperature sensor. It uses a single
machine.Pin configured as open-drain to communicate with one or
more slave devices on the shared bus.
Example:
from machine import Pin
from onewire import OneWire
ow = OneWire(Pin(15))
devices = ow.scan()
for rom in devices:
print(rom)
Classes
- class onewire.OneWire(pin: machine.Pin)
Construct a 1-Wire bus master on the given
machine.Pin. The pin is automatically configured as open-drain with a pull-up.ROM command constants:
- SEARCH_ROM: int
Search ROM command (
0xF0). Used internally byscan()to discover devices on the bus.
- MATCH_ROM: int
Match ROM command (
0x55). Used internally byselect_rom()to address a specific device by its 64-bit ROM code.
- SKIP_ROM: int
Skip ROM command (
0xCC). Addresses all devices on the bus simultaneously, skipping ROM matching.
Bus reset:
- reset(required: bool = False) bool
Issue a reset pulse on the bus. Returns
Trueif at least one slave device responded with a presence pulse, otherwiseFalse. If required isTrueand no device responds, raisesOneWireError.
Bit/byte I/O:
- readinto(buf: bytearray) None
Read
len(buf)bytes from the bus into the given pre-allocated buffer.
Device addressing:
- select_rom(rom: bytes | bytearray) None
Issue a reset followed by a MATCH ROM command to address the device whose 64-bit ROM code is in rom (an 8-byte buffer).
- scan() list[bytearray]
Search the bus and return a list of 8-byte ROM codes (one
bytearrayper detected device). Returns an empty list if no devices are present.
Cyclic redundancy check:
Exceptions
- exception onewire.OneWireError
Raised when a 1-Wire operation fails. Currently raised by
OneWire.reset()when required isTrueand no slave responds to the reset pulse.