ESP32 In MicroPython: I2C, HTU21D and Slow Reading
Written by Harry Fairhead & Mike James   
Monday, 10 July 2023
Article Index
ESP32 In MicroPython: I2C, HTU21D and Slow Reading
A First Program
Polling
Reading Humidity

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

By Harry Fairhead & Mike James

esppython360

Buy from Amazon.

Contents

       Preface

  1. The ESP32 – Before We Begin
  2. Getting Started
  3. Getting Started With The GPIO 
  4. Simple Output
  5. Some Electronics
  6. Simple Input
  7. Advanced Input – Interrupts
  8. Pulse Width Modulation
       Extract:
    PWM And The Duty Cycle
  9. Controlling Motors And Servos
  10. Getting Started With The SPI Bus
  11. Using Analog Sensors
       Extract:
    Analog Input
  12. Using The I2C Bus
       Extract
    : I2C, HTU21D And Slow Reading 
  13. One-Wire Protocols
  14. The Serial Port
  15. Using WiFi
     Extract:
    WiFi 
  16. Sockets
     Extract:
    Client Sockets
     Extract:
    SSL Client Sockets
  17. Asyncio And Servers
     Extract:
    Asyncio ***NEW!
  18. Direct To The Hardware
     Extract:
    Using Hardware Registers 

<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.

HTU21

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/-

 



Last Updated ( Tuesday, 11 July 2023 )