Main Site Documentation

SI7021 Temperature & Humidity sensor I2C Exception

Hi folks,
does somebody successfully used a Silicon Labs SI7021 Temp & Hum sensor with TinyCLR? Yesterday a struggled connecting to that device with TinyCLR V1.0.0 installed in my G30 based board. I got I2cClockStretchTimeoutException. Just before you ask, I successfully connected to other kind of device such a Real Time Clock (DS1307). So I know my I2C port is working well. Furthermore, I’ve checked my connections to the SI7021 many times and I am very confident it is correct. The Silicon Labs manual talks about a “Clock stretch during measurement”. Apparently it looks related to the exception I got.

Does I2C on TinyCLR support the clock stretching?

Might be a bug. Is there a breakout I can buy somewhere to try? I need your code s well.

Hi Gus, in fact I am using old GHI Gadgeteer stuff. My board is a Fez Lemur flashed with TinyCLR, the temperature module is the TempHumid Si70 Gadgeteer and I use the Gadgeteer extender to connect the module to the board. You probably have some of them hidden in a closet. You can refer to the Silicon Labs documentation here. The code I use is the Following:

Imports GHIElectronics.TinyCLR.Devices.I2c
Imports GHIElectronics.TinyCLR.Pins

Module Module1
    Public Sub Main()
        Dim i2c_temp_settings = New I2cConnectionSettings(&H40) With {
                           .BusSpeed = I2cBusSpeed.FastMode, .AddressFormat = I2cAddressFormat.SevenBit}
        Dim i2c_controller = I2cController.FromName(G30.I2cBus.I2c1)
        Dim TEMP_device = i2c_controller.GetDevice(i2c_temp_settings)
        Dim HUM_Buffer As Byte() = New Byte(1) {}
        Dim TEMP_Buffer As Byte() = New Byte(1) {}

        Try
            While True
                TEMP_device.WriteRead(New Byte() {&HE5}, TEMP_Buffer)
                Thread.Sleep(3000)
            End While
        Catch ex As Exception

        End Try
    End Sub
End Module

@Greg_Norris do you have one of these models still? Maybe you can help us and give it a try.

Any development on this topic? If mister Norris has Nothing in hand you can give a try with this one : Si7021

We tried this code on FEZ using the same sensor and it worked.

using System;
using System.Diagnostics;
using System.Collections;
using System.Text;
using System.Threading;
using GHIElectronics.TinyCLR.Devices.I2c;
using GHIElectronics.TinyCLR.Pins;

namespace TinyCLRApplication_LemurTempTest {
    class Program {
        static void Main() {

            var i2c_temp_settings = new I2cConnectionSettings(0x40); 
            i2c_temp_settings.BusSpeed = I2cBusSpeed.FastMode;
            i2c_temp_settings.AddressFormat = I2cAddressFormat.SevenBit;

            var controller = I2cController.FromName(FEZ.I2cBus.I2c1);
            var device = controller.GetDevice(i2c_temp_settings);

            byte[] TEMP_Buffer = new byte[1];

            try {
                while (true) {
                    device.WriteRead(new byte[] { 0xE5 }, TEMP_Buffer);
                    Thread.Sleep(3000);
                    Debug.WriteLine(TEMP_Buffer[0].ToString());
                }
            }
            catch (Exception e) {
                Debug.WriteLine(e.ToString());
            }                                           
        }
    }
}

Ok, I can’t explain what happen. Could you try the same logic in VB? I do not have a FEZ but I will test it with C# from my side on my G30. It could be a VB compilation error?

1 Like

I ran this code in VB, and everything still worked. We added a Debug.WriteLine to see the values coming off the sensor and. we used the FEZ.

Imports GHIElectronics.TinyCLR.Devices.I2c
Imports GHIElectronics.TinyCLR.Pins

Module Module1
    Public Sub Main()
        Dim i2c_temp_settings = New I2cConnectionSettings(&H40) With {
                           .BusSpeed = I2cBusSpeed.FastMode, .AddressFormat = I2cAddressFormat.SevenBit}
        Dim i2c_controller = I2cController.FromName(FEZ.I2cBus.I2c1)
        Dim TEMP_device = i2c_controller.GetDevice(i2c_temp_settings)
        Dim HUM_Buffer As Byte() = New Byte(1) {}
        Dim TEMP_Buffer As Byte() = New Byte(1) {}

        Try
            While True
                TEMP_device.WriteRead(New Byte() {&HE5}, TEMP_Buffer)
                Thread.Sleep(3000)
                Debug.WriteLine(TEMP_Buffer(1).ToString())
            End While
        Catch ex As Exception

        End Try
    End Sub
End Module

Thank you for your time Greg. I can’t explain why it is working today. My sole explications is that I used the bad pin on my breakout. :roll_eyes: GHI dedication to his Community is extraordinary.