Using the Wire library to communicate with SHTC3 temp/RH sensor via I2C

I am trying to use the Wire library to communicate to an Adafruit Sensirion SHTC3 breakout.

I am using a TeensyLC as my MCU. I have attached the SHTC3 datasheet and a picture of my schematic.

I have read the SHTC3 data sheet, looked through the Wire library, and perused many resources on how to use Wire to communicate to I2C devices. The purpose of my current code is to see if I am able to read bits of data from the sensor. I cannot get my code to work and I am looking for further direction.

As a note, I scanned for an I2C using an example from the Wire library and it returned that the SHTC3 was connected at the correct address (0x70). Code is below:

#include <Wire.h>

byte addressWrite = B11100000;
byte addressRead = B11100001;
byte wakeupMSB = B00110101;
byte wakeupLSB = B00010111;
byte measurementMSB = B01111000;
byte measurementLSB = B01100110;

int var;

void setup() {
  Wire.begin();
  Serial.begin(9600);
}


void loop() {
  Wire.beginTransmission(addressWrite); //Could use 112 or 0x70

  Wire.write(wakeupMSB);  //writes wakeup command
  Wire.write(wakeupLSB);

  delay(.24);  //recommended delay for wakeup from table 5 in datasheet

  Wire.write(addressWrite);   //Sends I2c write command

  Wire.write(measurementMSB);  //Send measurement command; Temp first
  Wire.write(measurementLSB);
  
  delay(12.1); //recommended delay for measurement from table 5 in datasheet

  Wire.requestFrom(addressRead, 6); //Sends I2c read command

  var = Wire.available(); //Return number of bits available to read
  
  Wire.endTransmission();

  Serial.println(var);
}

Teensy-SHTC3.PNG

Teensy-SHTC3.PNG

Sensirion_Humidity_Sensors_SHTC3_Datasheet.pdf (639 KB)

Nice first post!!! Well structured, complete, code tags, real schematic (not a Fritzing cartoon). Karma++

Anyway, have you checked out the Adafruit Tutorial for Your Device, Complete with Arduino Library? Even if you don’t end up using it in the end, you’ll learn a lot reading their code.

The delay() function takes an 'unsigned long int' which represents milliseconds. delay(0.24) will translate into delay(0). If you need to pause for microseconds, use delayMicrosonds() but keep it to whole numbers.

blh64:
use delayMicrosonds()

delayMicroseconds()

gfvalvo:
Nice first post!!! Well structured, complete, code tags, real schematic (not a Fritzing cartoon). Karma++

Anyway, have you checked out the Adafruit Tutorial for Your Device, Complete with Arduino Library? Even if you don't end up using it in the end, you'll learn a lot reading their code.

Thanks!

I have checked out the Adafruit tutorial. I used Adafruits SHTC3 library and was able to get temp and RH measurements on the TeensyLC.

I've been pushing myself to better understand how electronics work and communicate, thus my attempt at solely using the Wire library and a datasheet to communicate via I2C.

"use delayMicrosonds() but keep it to whole numbers.",
No lower than 4, and will probably get consistent results using multiples of 4.
I've used 0 in 1.8.4 for checking timing, didn't seem to cause any problems.

CrossRoads:
"use delayMicrosonds() but keep it to whole numbers.",
No lower than 4, and will probably get consistent results using multiples of 4.
I've used 0 in 1.8.4 for checking timing, didn't seem to cause any problems.

I made the adjustment and used delayMicroseconds(24);

This had no effect on the output of the code and it still returned a value of 0 for var.

CrossRoads:
No lower than 4, and will probably get consistent results using multiples of 4.

Not necessarily true on a Teensy LC (48 MHz ARM Cortex M0).

If you look at the source code for the wire library, you'll see that all the write() function does is buffer the data. It doesn't actually send anything on the I2C bus. That doesn't happen until you call endTransmission(). So, the delays you have interspersed are just delaying the writes to the buffer, not the bus. Also, you must endTransmission() to send the data before you request to receive data from the I2C slave.

Study the examples that come with the wire library.