Page 1 of 3 The one-wire bus is easy and cheap - what more could you want? This extract is from the newly published Programming the ESP32 in MicroPython, 2nd Edition and shows you how the DS18B20 temperature sensor works with the ESP32, ESP32 S3 and Arduino Nano ESP32.
Programming the ESP32in MicroPython Second Edition
By Harry Fairhead & Mike James
Buy from Amazon.
Contents
Preface
- The ESP32 – Before We Begin
- Getting Started
- Getting Started With The GPIO **
- Simple Output
- Some Electronics
- Simple Input
- Advanced Input – Interrupts**
- Pulse Width Modulation
Extract: PWM And The Duty Cycle**
- Controlling Motors And Servos
- Getting Started With The SPI Bus
- Using Analog Sensors
Extract: Analog Input **
- Using The I2C Bus
Extract: I2C, HTU21D And Slow Reading **
- One-Wire Protocols
Extract: One Wire Bus DS1820 ***NEW!!!
- The Serial Port
- Using WiFi
Extract: WiFi **
- Sockets
Extract: Client Sockets** Extract: SSL Client Sockets**
- Asyncio And Servers
Extract: Asyncio **
- Direct To The Hardware
Extract: Using Hardware Registers **
** Not updated from first edition as yet
<ASIN:187196282X>
One-Wire Protocols
In this chapter we make use of all the ideas introduced in earlier chapters to create a raw interface with the low-cost DHT11/22 temperature and humidity sensor and the 1-Wire bus device the DS18B20. Both devices have MicroPython drivers and so are easy to use but they make good examples of how to use “bit banging” to create a direct interface with a device.
In book but not in this extract
- The DHT22
- The Electronics
- The DHT Driver
- The Protocol
- Reading the Data
- Extracting the Data
- A DHT22 Object
The 1‑Wire Bus And The DS1820
Unlike the DHT22 which uses a bus that is unique to itself, the 1-Wire bus is more general and there are a range of devices that work with it. It is a proprietary bus, but it has a lot in common with the I2C and SPI buses. That is, it defines a general bus protocol that can be used with a range of different devices. There are many useful devices you can connect to it, including the iButton security devices, memory, data loggers, fuel gauges and more. However, probably the most popular of all 1‑Wire devices is the DS18B20 temperature sensor - it is small, very cheap and very easy to use. This chapter shows you how to work with it, but first let's deal with the general techniques needed to work with the 1‑Wire bus.
The Hardware
One-wire devices are very simple and only use a single wire to transmit data:
The 1‑Wire device can pull the bus low using its Tx line and can read the line using its Rx line. The reason for the pull-up resistor is that both the bus master and the slave can pull the bus low and it will stay low until they both release the bus.
The device can even be powered from the bus line by drawing sufficient current through the pull-up resistor - so called parasitic mode. Low power devices work well in parasitic mode, but some devices have such a heavy current draw that the master has to provide a way to connect them to the power line - so called strong pull-up. In practice, parasitic mode can be difficult to make work reliably for high-power devices.
In normal-powered mode there are just three connections – V, usually 3.3V for the ESP32, Ground and Data. The pull-up resistor varies according to the device, but anything from 2.2K to 4.7kΩ works. The longer the bus, the lower the pull-up resistor has to be to reduce “ringing”. There can be multiple devices on the bus and each one has a unique 64-bit lasered ROM code, which can be used as an address to select the active devices.
|