SparkFun Humidity Sensor Breakout - SHTC3 (Qwiic) Hookup Guide

Pages
Contributors: El Duderino
Favorited Favorite 0

SHTC3 Arduino Library

Note: This library assumes you are using the latest version of the Arduino IDE on your desktop. If this is your first time using Arduino, please review our tutorial on installing the Arduino IDE. If you have not previously installed an Arduino library, please check out our installation guide.

The SparkFun SHTC3 Arduino library can be downloaded with the Arduino library manager by searching 'SparkFun SHTC3' or you can grab the zip here from the GitHub repository:

Once the library is installed, we can move on to working with the included example sketches and take some sensor readings.

Example 1: Basic Readings

Open the first example by heading to File > Examples > SparkFun SHTC3 Humidity and Temperature Sensor Library > Example1_BasicReadings. Select your board (in our case, the Arduino/Genuino Uno) and the COM port that it enumerated on. Then hit upload. Open the Arduino Serial Monitor at 115200 baud to start viewing the humidity and temperature!

Try holding your hand over the sensor or breathe lightly on to the SHTC3 and watch the values change.

Example 2: Verify Checksums

This example is very similar to the Basic Readings but also enables and returns checksum data calculated by the sensor to ensure the data is valid. Open it by following the instructions for Example 1 but instead select Example2_VerifyChecksums. The checksum pass indicators are passIDcrc, passRHcrc and passTcrc for the sensor ID, relative humidity and temperature, respectively.

Using checksums is not absolutely necessary but can be helpful for troubleshooting any I2C communication errors. Once the code is uploaded, it will print out over serial all checksum data and whether or not it has passed or failed so you can identify if something such as the device ID is not returned properly.

Example 3: Changing Options

The third example builds on the previous two and demonstrates how to configure the SHTC3 using the setMode function as well as selecting your Wire port and clock speed. Again, open the example by navigating to the SHTC library and selecting Example2_ChangingOptions.

Below are the available modes available on the SHTC3:

language:c
SHTC3_CMD_CSE_RHF_NPM = 0x5C24,   // Clock stretching, RH first, Normal power mode
SHTC3_CMD_CSE_RHF_LPM = 0x44DE,   // Clock stretching, RH first, Low power mode
SHTC3_CMD_CSE_TF_NPM = 0x7CA2,    // Clock stretching, T first, Normal power mode
SHTC3_CMD_CSE_TF_LPM = 0x6458,    // Clock stretching, T first, Low power mode

SHTC3_CMD_CSD_RHF_NPM = 0x58E0,   // Polling, RH first, Normal power mode
SHTC3_CMD_CSD_RHF_LPM = 0x401A,   // Polling, RH first, Low power mode
SHTC3_CMD_CSD_TF_NPM = 0x7866,    // Polling, T first, Normal power mode
SHTC3_CMD_CSD_TF_LPM = 0x609C   // Polling, T first, Low power mode

By default, the code sets the SHTC3 up to use clock stretching, report temperature data first and run on low power mode. To change to another mode, refer to the settings above and change this line:

language:c
mySHTC3.setMode(SHTC3_CMD_CSE_TF_LPM) == SHTC3_Status_Nominal

Once you have selected which mode you want the SHTC3 to operate in, upload it and open a serial monitor to 115200 baud. You should see the checksum data print and then whether or not the setMode function was successful. Note that the code defaults to print out "Choosing low-power measurements with T first: " so if you use a different mode, you may want to alter that print statement to avoid confusion.

If all of that is successful, the code will start printing out humidity and temperature data.

Example 4: Using Callback

The fourth example in the library demonstrates how to use the callback feature on the SHTC3. You may be wondering, what is a callback? It is a way that a library can provide a place for the user to do something. The library was written with a function that is called (nearly) every time a function is exited. How is that useful? You can overwrite the function with your own definition that allows you to do things like debug or watch the program execute.

Make a function with this signature:

language:c
void SHTC3_exitOp_Callback(SHTC3_Status_TypeDef status, bool inProcess, char * file, uint16_t line)

Then write in it some code that might help you. The example helps us watch the code execute by displaying the line number, file, and status every time that a function finishes.

Including these functions is particularly helpful for debugging but can be taxing on your processor and slow down your code.