Shtc3 - Measuring temperature and humidity (examples)
This page contains some simple examples with function documentation on how to take measurements using the SHTC3 temperature and humidity sensor.
Initialization
To use the SHTC3 sensor, first, include the required library, create the sensor object and initialize the sensor in the setup() function. You can use the return of begin() to check if everything is connected correctly:
// Include the library
#include "SHTC3-SOLDERED.h"
// Create an SHTC3 object
SHTC3 shtcSensor;
// Setup function, runs once
void setup()
{
Serial.begin(115200);
// Initialize sensor
if(!shtcSensor.begin())
{
// 'begin' returned false, there is an error
Serial.println("Can't init SHTC3!");
Serial.println("Check connection!");
while(true)
;
}
}
// ...
shtcSensor.begin()
Initializes the SHTC3 sensor, setting up communication over I2C and verifying its presence.
Returns value: Returns true if initialization is successful, false otherwise.
Sampling new measurements
Before reading temperature or humidity, call sample() to request a new measurement from the sensor.
shtcSensor.sample(); // Request new temperature and humidity readings
shtcSensor.sample()
Requests a new temperature and humidity measurement from the SHTC3 sensor. It wakes up the sensor, performs the measurement, checks CRC validation, and puts the sensor back to sleep.
Returns value: Returns true if the sampling operation is successful and the CRC check passes, false otherwise.
Function parameters:
| Type | Name | Description |
|---|---|---|
uint16_t | readcmd | Optional, default is SHTC3_READ_LP. Command specifying the type of measurement to be performed. See table below for details. |
uint8_t | pause | Optional, time delay in milliseconds between issuing the command and reading data from the sensor. |
The available commands to send are as follows:
| Command Macro | Value | Description |
|---|---|---|
SHTC3_READ | 0x7CA2 | Initiates a temperature-first measurement with clock stretching enabled. |
SHTC3_READ_LP | 0x6458 | Default, similar to SHTC3_READ, but after measuring, puts the SHTC3 in low power mode. Clock stretching remains enabled. |
sample()!Measuring temperature
To measure the temperature, use the readTempC() function. The sensor samples temperature in degrees Celsius.
void loop()
{
shtcSensor.sample(); // Request new measurements
Serial.print("Temp: ");
Serial.println(shtcSensor.readTempC(), 2); // Read and print temperature
delay(5000); // Wait 5 seconds before next reading
}
shtcSensor.readTempC()
Reads the current temperature from the SHTC3 sensor in degrees Celsius.
Returns value: Returns a float value representing the temperature in degrees Celsius.
Measuring humidity
To measure humidity, use the readHumidity() function. The sensor provides humidity readings as a percentage.
void loop()
{
shtcSensor.sample(); // Request new measurements
Serial.print("Hum: ");
Serial.println(shtcSensor.readHumidity(), 2); // Read and print humidity
delay(5000); // Wait 5 seconds before next reading
}
Below is the detailed function explanation:
shtcSensor.readHumidity()
Reads the current relative humidity from the SHTC3 sensor as a percentage.
Returns value: Returns a float value representing the relative humidity in percentage (%).
Full example
Try all of the above mentioned functions in this full example which prints out the measured temperature and humidity over Serial at 115200 baud:
#include "SHTC3-SOLDERED.h"
SHTC3 shtcSensor;
void setup()
{
shtcSensor.begin(); //Initialize sensor
Serial.begin(115200); //Start serial communication with PC using 115200 baudrate
}
void loop()
{
shtcSensor.sample(); //Initialize sensor
// For temperature use function readTempC
Serial.print("Temp: ");
Serial.println(shtcSensor.readTempC(), 2); //Get temperature and print
// For geting humidity use function readHumidity
Serial.print("Hum: ");
Serial.println(shtcSensor.readHumidity(), 2); //Get humidity and print
delay(5000);
}
TempAndHumidity.ino
Example file for using SHTC3 sensor with easyC/Qwiic/I2C