ESP32 In MicroPython: One Wire Bus DS1820 |
Written by Harry Fairhead & Mike James | ||||
Monday, 13 January 2025 | ||||
Page 2 of 3
The OneWire DriverThe 1-Wire bus is general in the sense that you can have multiple slave devices connected to it at the same time. This means that there has to be a way to find out what devices are connected and to select which device you want to talk to. The OneWire class provides a driver that works with any set of 1-Wire devices you care to work with. You can create an instance of the OneWire class to work with the protocol on any GPIO line: ow = onewire.OneWire(pin) Before you can work with any devices you have to send a reset signal. This causes any connected device to hold the line low and this is used to indicate that there are active devices on the bus: presence = ow.reset(required=False) The returned value is True if there are any active devices. If you set required to True a OneWireError exception is thrown if there are no devices connected to the bus. To discover what devices are connected you can use the scan method: roms = onewire.scan() This scans for devices on the bus and returns a list of 64-bit ROM codes. The ROM codes uniquely define both the device and its type: For example, all DS18B20 Temperature Sensors have a serial number that starts with 28-. Variations on the device have different family codes: W1_THERM_DS18S20 0x10 W1_THERM_DS1822 0x22 W1_THERM_DS18B20 0x28 W1_THERM_DS1825 0x3B W1_THERM_DS28EA00 0x42 Once you have a ROM code you can use it to select the device the master is interacting with: ow.select_rom(rom) After this all data transfer occurs between the master and the selected device. All of the other devices ignore what is going on until a reset is sent. There are some low-level communication functions. For example: ow.writebit(value) value = ow.readbit() sends and receives a single bit to the currently selected device. You usually don’t need to make use of these functions, but there are devices and commands that require that you work with a single bit to signal. Usually you want to send and receive single bytes: ow.writebyte(value) value = ow.readbyte() or a set of bytes in a byte array buffer: ow.write(buf) ow.readinto(buf)
Finally we have: ow.crc8(data) which will compute the CRC checksum of the data. These methods are completely general and will work with any 1-Wire bus device, but we will use the DS18B20 for our example as it is by far the most commonly encountered 1-Wire device. In fact, it is so popular that there is a driver that specifically targets it and makes it very easy to use. We will first look at how to use this driver and then at how to use the OneWire class to use the same device and to access additional features. The DS18B20 HardwareThe most popular 1-Wire device is the DS18B20. It is available in a number of formats, but the most common makes it look just like a standard BJT (Bipolar Junction Transistor) which can sometimes be a problem when you are trying to find one. You can also get them made up into waterproof sensors complete with cable.
No matter how packaged, they will work at 3.3V or 5V. The basic specification of the DS18B20 is:
It can also be powered from the data line, allowing the bus to operate with only two wires, data and ground. However, this parasitic power mode is difficult to make work reliably and is best avoided in an initial design. There are also the original DS1820 and the DS18S20, which too are best avoided in new applications.
Ground needs to be connected to the system ground, VDD to 3.3V and DQ to the pull-up resistor of an open collector bus. While you can have multiple devices on the same bus, for simplicity it is better to start off with a single device until you know that everything is working.
|
||||
Last Updated ( Monday, 13 January 2025 ) |