Page 1 of 4 Using I2C with the ESP32 is easy - except when it isn't. In this example we find out how to deal with the ESP32's inability to wait for slow devices. This extract is from Programming the ESP32 in MicroPython, part of the I Programmer Library.
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>
.In chapter but not in extract.
- I2C Hardware Basics
- The ESP32 I2C
- Writing To A Register
- Reading A Register
- Slow Read Protocols
It is assumed that you know how basic I2C works and understand slow read protocols - clock stretching and polling. What we discover is that the ESP32 isn't well equipped for slow reads.
A Real Device
Using an I2C device has two problems - the physical connection between master and slave and figuring out what the software has to do to make it work. Here we’ll work with the HTU21D/Si7021 and the information in its datasheet to make a working temperature humidity sensor using the I2C functions we’ve just met.
First the hardware. The HTU21D Humidity and Temperature sensor is one of the easiest of I2C devices to use. Its only problem is that it is only available as a surface-mount package. To overcome this you could solder some wires onto the pads or buy a general breakout board. However, it is much simpler to buy the HTU21D breakout board because this has easy connections and built-in pull-up resistors. The HTU21D has been replaced by the Si7021, which is more robust than the original and works in the same way, but the HTU21D is still available from many sources.
If you decide to work with some other I2C device you can still follow the steps given, modifying what you do to suit it. In particular, if you select a device that only works at 5V you might need a level converter.
Given that the HTU21D has pull-up resistors you don’t need to enable the onboard pull-ups provided by the ESP32. If you notice any irregularity in the signal at higher frequencies then adding some additional pull-ups might help.
You can use a prototype board to make the connections and this makes it easier to connect other instruments such as a logic analyzer. Given that the pinouts vary according to the exact make of the device, you need to compare the suggested wiring with the breakout board you are actually using.
ESP32
|
HTU21
|
SDA GPIO19
|
SDA/DA
|
SCK GPIO18
|
SCL/CL
|
3.3v
|
VCC/VIN/+
|
GND
|
GND/-
|
|