ESP32 SHTC3 Temperature and Humidity Sensor
The SHTC3 sensor offers precise temperature and humidity measurements in a compact and energy-efficient package. It is designed for low-power IoT applications and features Sensirion's CMOSens® technology for high accuracy and long-term stability.
Jump to Code Examples
Quick Links
SHTC3 Price
About SHTC3 Temperature and Humidity Sensor
The SHTC3, developed by Sensirion, is a digital humidity and temperature sensor optimized for battery-driven applications. It combines high accuracy with low power consumption and a compact design, making it ideal for IoT devices, consumer electronics, and portable applications.SHTC3 Sensor Technical Specifications
Below you can see the SHTC3 Temperature and Humidity Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C
- Operating Voltage: 1.62V to 3.6V
- Temperature Range: -40°C to 125°C
- Humidity Range: 0% to 100% RH
- Temperature Accuracy: ±0.2°C
- Humidity Accuracy: ±2% RH
- Interface: I2C
- Dimensions: 2mm x 2mm x 0.75mm
SHTC3 Sensor Pinout
Below you can see the pinout for the SHTC3 Temperature and Humidity Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The SHTC3 pinout is as follows:
- VDD: Power supply voltage (1.62V to 3.6V).
- GND: Ground.
- SDA: Serial Data Line for I2C communication.
- SCL: Serial Clock Line for I2C communication.
SHTC3 Wiring with ESP32
Below you can see the wiring for the SHTC3 Temperature and Humidity Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
- Connect VDD to the 3.3V pin on the ESP32.
- Connect GND to the ground (GND) of the ESP32.
- Connect SDA to the ESP32's GPIO21 (default I2C data pin).
- Connect SCL to the ESP32's GPIO22 (default I2C clock pin).
- Place pull-up resistors (10kΩ) between SDA and VDD, and SCL and VDD, to ensure reliable communication.
Code Examples
Below you can find code examples of SHTC3 Temperature and Humidity Sensor with ESP32 in several frameworks:
ESP32 SHTC3 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the SHTC3 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHTC3.h"
Adafruit_SHTC3 shtc3;
void setup() {
Serial.begin(115200);
if (!shtc3.begin()) {
Serial.println("Couldn't find SHTC3 sensor!");
while (1) delay(10);
}
Serial.println("SHTC3 initialized");
}
void loop() {
sensors_event_t humidity, temp;
if (!shtc3.getEvent(&humidity, &temp)) {
Serial.println("Failed to read sensor data");
return;
}
Serial.print("Temperature: "); Serial.print(temp.temperature); Serial.println(" °C");
Serial.print("Humidity: "); Serial.print(humidity.relative_humidity); Serial.println(" %");
delay(2000);
}
This Arduino sketch demonstrates how to use the SHTC3 sensor with the Adafruit SHTC3 library. It initializes the sensor, configures it for data reading, and retrieves temperature and humidity values every 2 seconds. The readings are printed to the Serial Monitor.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 SHTC3 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the SHTC3 Temperature and Humidity Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "driver/i2c.h"
#define I2C_MASTER_SCL_IO 22 /*!< GPIO number for I2C master clock */
#define I2C_MASTER_SDA_IO 21 /*!< GPIO number for I2C master data */
#define I2C_MASTER_NUM I2C_NUM_0 /*!< I2C master port */
#define I2C_MASTER_FREQ_HZ 100000 /*!< I2C master clock frequency */
#define SHTC3_SENSOR_ADDR 0x70 /*!< SHTC3 I2C address */
static esp_err_t i2c_master_init(void) {
i2c_config_t conf = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ,
};
ESP_ERROR_CHECK(i2c_param_config(I2C_MASTER_NUM, &conf));
return i2c_driver_install(I2C_MASTER_NUM, conf.mode, 0, 0, 0);
}
void read_shtc3_sensor() {
uint8_t data[6];
uint8_t cmd[] = {0x7C, 0xA2}; // Measurement command
i2c_master_write_to_device(I2C_MASTER_NUM, SHTC3_SENSOR_ADDR, cmd, 2, pdMS_TO_TICKS(1000));
vTaskDelay(pdMS_TO_TICKS(20));
i2c_master_read_from_device(I2C_MASTER_NUM, SHTC3_SENSOR_ADDR, data, 6, pdMS_TO_TICKS(1000));
uint16_t temp_raw = (data[0] << 8) | data[1];
uint16_t hum_raw = (data[3] << 8) | data[4];
float temperature = -45 + 175 * ((float)temp_raw / 65535.0);
float humidity = 100 * ((float)hum_raw / 65535.0);
printf("Temperature: %.2f °C, Humidity: %.2f %%\n", temperature, humidity);
}
void app_main() {
ESP_ERROR_CHECK(i2c_master_init());
while (1) {
read_shtc3_sensor();
vTaskDelay(pdMS_TO_TICKS(2000));
}
}
This ESP-IDF code initializes the I2C master, sends a command to the SHTC3 sensor to perform measurements, reads the raw temperature and humidity data, and converts it into human-readable values. The results are printed every 2 seconds.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 SHTC3 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the SHTC3 Temperature and Humidity Sensor
sensor:
- platform: shtc3
temperature:
name: "Room Temperature"
humidity:
name: "Room Humidity"
update_interval: 60s
This ESPHome configuration defines the SHTC3 sensor. Two entities are created: one for temperature and one for humidity, with user-friendly names such as 'Room Temperature' and 'Room Humidity.' The sensor reads and updates data every 60 seconds.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 SHTC3 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
lib_deps =
adafruit/Adafruit SHTC3 Library @ ^1.0.0
monitor_speed = 115200
ESP32 SHTC3 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the SHTC3 Temperature and Humidity Sensor:
#include <Wire.h>
#include "Adafruit_SHTC3.h"
Adafruit_SHTC3 shtc3;
void setup() {
Serial.begin(115200);
if (!shtc3.begin()) {
Serial.println("Couldn't find SHTC3 sensor!");
while (1) delay(10);
}
Serial.println("SHTC3 initialized.");
}
void loop() {
sensors_event_t temp, humidity;
if (!shtc3.getEvent(&humidity, &temp)) {
Serial.println("Failed to read sensor data");
return;
}
Serial.print("Temperature: ");
Serial.print(temp.temperature);
Serial.println(" °C");
Serial.print("Humidity: ");
Serial.print(humidity.relative_humidity);
Serial.println(" %");
delay(2000);
}
This PlatformIO code uses the Adafruit SHTC3 library to initialize and interface with the SHTC3 sensor. It retrieves temperature and humidity values and prints them every 2 seconds to the Serial Monitor.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 SHTC3 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the SHTC3 Temperature and Humidity Sensor with your ESP32.
from machine import I2C, Pin
from time import sleep
# Initialize I2C interface
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# SHTC3 I2C address
SHTC3_ADDR = 0x70
# Command for measurement
MEASURE_CMD = b'\x7C\xA2'
while True:
try:
i2c.writeto(SHTC3_ADDR, MEASURE_CMD)
sleep(0.02) # Wait for measurement to complete
data = i2c.readfrom(SHTC3_ADDR, 6)
raw_temp = (data[0] << 8) | data[1]
raw_hum = (data[3] << 8) | data[4]
temperature = -45 + 175 * (raw_temp / 65535.0)
humidity = 100 * (raw_hum / 65535.0)
print("Temperature: {:.2f} °C".format(temperature))
print("Humidity: {:.2f} %".format(humidity))
except Exception as e:
print("Error reading SHTC3: ", e)
sleep(2)
This MicroPython script interfaces with the SHTC3 sensor using I2C. It sends a measurement command, reads the raw data, processes it, and prints temperature and humidity values to the console every 2 seconds.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of SHTC3 Temperature and Humidity Sensor, its pinout, connection with ESP32 and SHTC3 Temperature and Humidity Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.