The DS18B20 driver builds on the methods provided by the OneWire driver. It makes it easy to read the temperature by supplying all of the commands to select the device, make the DS18B20 take a temperature measurement and retrieve the result. You can, in fact do all of these things with just the OneWire driver as will be explained later, but the DS18B20 driver is easier to use.
To use the ds18x20 driver you have to first create a OneWire driver object which sets the pin and finds out what devices are connected to it. Next you have to create a ds18X20 object which works with any DS18B20 or DS18S20 devices connected to the bus.
You need to find out what devices are connected to the bus and you can use the OneWire object’s scan method to do this. The ds18X20 object has its own version of the scan method which only returns devices in the DS family.
You can initiate a temperature reading on all of the connected DS18X20 devices using the convert_temp() method. This causes the temperature to be stored in the device’s scratchpad memory. To get the temperature from a specific device you have to use:
t = ds.read_temp(rom)
where rom is the ROM code of the device you want to read.
So to read a temperature from a single device you can use:
from machine import Pin
import onewire,ds18x20
ow = onewire.OneWire(Pin(2))
presence=ow.reset()
if presence:
print("Device present")
else:
print("No device")
ds = ds18x20.DS18X20(ow)
roms = ds.scan()
ds.convert_temp()
t = ds.read_temp(roms[0])
print(t)
You can see how this program works. After setting up the 1-Wire bus the DS18X20 object is created. The scan of the bus returns a list of DS18X devices and then we initiate a conversion and read the temperature of the first, and often only, device. Notice that you only take a new temperature reading when you use the convert_temp method.
There are some additional methods that provide access to more advanced functions of the DS18B20 and these are discussed at the end of the chapter.
As already said, if you just want to read the temperature the driver is all you need. However, if you want to know how the 1-Wire bus works and how to use other 1-Wire devices, read on.
In Chapter but not in this extract
The 1-Wire Protocol
Match or Skip ROM
Getting the Temperature
A Temperature Class
Other Commands
Summary
The DHT22 is a low-cost temperature and humidity sensor. It uses a custom single wire bus which is not compatible with the well known 1-Wire bus. Its asynchronous protocol is easy to implement directly in MicroPython.
The 1-Wire bus is a proprietary, but widely-used, bus. It is simple and very capable. As its name suggests it makes use of a single data wire and usually a power supply and ground.
MicroPython provides a OneWire driver which works with all 1-Wire devices.
Implementing the 1-Wire protocol is mostly a matter of getting the timing right.
There are three types of interaction: presence pulse, which simply asks any connected devices to reply and make themselves known, read and write.
The 1-Wire protocol is easier to implement than you might think because each bit is sent as a “slot” and while timing is critical within the slot, how fast slots are sent isn’t and the master is in control of when this happens.
The DS18B20 temperature sensor is one of the most commonly encountered 1‑Wire bus devices. It is small, low-cost and you can use multiple devices on a single bus.
MicroPython provides a simple DS18X20 driver, but it is also easy to implement your own driver.
After a convert command is sent to the device, it can take 750ms before a reading is ready. To test for data ready you have to poll on a single bit. Reading a zero means data not ready and reading a one means data ready. When the data is ready, you can read the scratchpad memory where the data is stored.
The DS18B20 has other commands that can be used to set temperature alarms etc, but these are rarely used.
Programming the ESP32in MicroPython Second Edition