Solutions to Exercise 4: The I2C Bus and the SHT30 Temperature and Humidity Sensor

Exercise 2:

This is an extremely relevant exercise because it shows typical work that must be done during an IoT project. Often code from other authors can be found on the Internet (e.g. github) but often you find problems with this. Maybe it is

  • outdated and must be adapted to a newer system version
  • there are still bugs in the code
  • it was written for hardware slightly different from the one you want to use
Still, starting from something that has worked before or worked on a different hardware is often (not always!) easier than starting from scratch. And reading code written be experienced programmers may help you to improve your own coding skills.

A quick try

After having uploaded mysht30.py I simply copy the first example to a Python script and modify the import statement:

sht30Meas.png

and this is what I see when running it:

crcError.png

Debugging the STH30 driver

Well, this does not look good! So, let's have a look if the SHT30 is found on the I2Cbus. This is the code found in the sht30 README.md:

sht30Check.png

and here is the result:

sht30Connected.png

This looks good but ... When I take the SHT30 off the triple base, I still get the same result which is much less good! Looking into the mysht30.py code I see that by default pin 5 is used for SCL and pin 4 for SDA, which is not correct for our board! Now we have 2 options:

  • modify the defaults
  • add the scl and sda parameters when creating the sht30 object.
Since We will use the driver only with the ESP32 WeMos D1 mini card, I decided for the first option. I also add the bus number (1) to make MicroPython use the ESP32 hardware interface to the I2C bus. SHT30 class initialization now looks like this:

sht30Init.png

With this change I see the Is connected: True if the SHT30 is connected and Is connected: False if I remove it.

Let's try the measurement script again:

sht30BusError.png

Different, but not much better!

Let's try to send a reset command with a little script, not using the sht30 driver:

sht30CmdTest.png

with this result:

sht30NotSupported.png

This tells me that start() and stop() are not supported by the I2C driver on the ESP32. Looks like the I2C start and stop conditions have been integrated into the writeto() and readfrom() methods! Let's just remove those two calls:

sht30Reset.png

As you can see, the reset now works without errors. And since we are at it, let's try to make a measurement without passing through the SHT30 class:

sht30CmdOk.png

with this result:

sht30MeasOk.png

Now that we know how to read and write the SHT30 using the ESP32 I2C driver, we can make the necessary corrections in the SHT30 class.

-- Uli Raich - 2020-05-18

Comments

Topic attachments
I Attachment History Action Size Date Who Comment
PNGpng crcError.png r1 manage 24.7 K 2020-05-18 - 10:48 UliRaich  
PNGpng i2cWrongPins.png r1 manage 21.6 K 2020-05-18 - 11:22 UliRaich  
PNGpng sht30BusError.png r1 manage 17.2 K 2020-05-18 - 12:38 UliRaich  
PNGpng sht30Check.png r1 manage 7.3 K 2020-05-18 - 11:22 UliRaich  
PNGpng sht30CmdOk.png r1 manage 71.2 K 2020-05-18 - 13:19 UliRaich  
PNGpng sht30CmdTest.png r2 r1 manage 25.9 K 2020-05-18 - 12:55 UliRaich  
PNGpng sht30Connected.png r1 manage 12.3 K 2020-05-18 - 11:22 UliRaich  
PNGpng sht30Init.png r1 manage 26.4 K 2020-05-18 - 12:33 UliRaich  
PNGpng sht30Meas.png r1 manage 9.8 K 2020-05-18 - 13:20 UliRaich  
PNGpng sht30MeasOk.png r1 manage 6.6 K 2020-05-18 - 13:22 UliRaich  
PNGpng sht30NotSupported.png r1 manage 11.9 K 2020-05-18 - 12:53 UliRaich  
PNGpng sht30Reset.png r1 manage 3.6 K 2020-05-18 - 13:02 UliRaich  
Edit | Attach | Watch | Print version | History: r4 < r3 < r2 < r1 | Backlinks | Raw View | Raw edit | More topic actions...
Topic revision: r1 - 2020-05-18 - UliRaich
 
  • Edit
  • Attach
This site is powered by the TWiki collaboration platform Powered by PerlCopyright © 2008-2024 by the contributing authors. All material on this collaboration platform is the property of the contributing authors.
Ideas, requests, problems regarding TWiki? Send feedback