The Sensing Principle: Capacitive Polymer and Bandgap Tech
Capacitive humidity sensing relies on a specialized polymer layer deposited directly onto a silicon substrate that absorbs ambient water vapor. As moisture increases, the dielectric constant of the polymer shifts, altering its overall capacitance. An internal RC oscillator measures this shift, converting the physical absorption into a precise frequency count that the onboard ASIC translates into a digital word.
Temperature is measured simultaneously on the same die using a bandgap reference circuit, which exploits the predictable voltage drop across a silicon PN junction as it heats up. By combining these two physical measurements in a single package, the sensor's internal logic compensates for the fact that relative humidity is entirely dependent on ambient temperature, outputting a highly accurate, temperature-compensated digital signal.
Decision Tree: Picking the Right Sensor for the Job
When sourcing sensors devices for environmental monitoring, hobbyists often default to whatever breakout board is cheapest on Amazon. This leads to failed projects when condensation shorts out a cheap sensor or when I2C address conflicts brick a multi-sensor bus. Use this decision path to select the exact part number you need:
| Project Requirement | Recommended Sensor | Exact Part Number |
|---|---|---|
| Need barometric pressure for altitude or weather station tracking? | Bosch BME280 | BME280 (Adafruit 2652) |
| Need ultra-fast response (<5s) and survival in condensing environments? | Sensirion SHT40 | SHT40-AD1B |
| Budget constrained (<$1.50/unit) and acceptable with ±2% RH accuracy? | Aosong AHT20 | AHT20 |
Wiring the SHT40 to an ESP32 (Pinout & Power)
The SHT40 communicates via a digital I2C interface. It does not output an analog voltage; it outputs 16-bit digital words over the SDA and SCL lines. Because it is a 3.3V native device, powering it with 5V will permanently destroy the internal ASIC.
| SHT40 Pin | Function | ESP32 DevKit V1 Pin | Supply Range & Notes |
|---|---|---|---|
| 1 (VDD) | Power Supply | 3V3 | 1.08V to 3.6V (Typical 3.3V) |
| 2 (SDA) | I2C Data | GPIO 21 (Default SDA) | Requires 4.7kΩ pull-up to VDD |
| 3 (SCL) | I2C Clock | GPIO 22 (Default SCL) | Requires 4.7kΩ pull-up to VDD |
| 4 (GND) | Ground | GND | Connect to common system ground |
Common Interference Sources and Bus Capacitance
The most common failure mode when interfacing I2C sensors devices over long wires is bus capacitance. The I2C specification limits total bus capacitance to 400pF. If you run standard Dupont jumper wires longer than 30cm (12 inches), the parallel capacitance between the SDA and SCL lines will smear the digital edges, causing the ESP32 to read corrupted bytes or drop the sensor entirely.
- The Fix: Use twisted-pair wire for SDA and SCL to minimize inductive coupling, and drop the I2C clock speed from 400kHz (Fast Mode) to 100kHz (Standard Mode) in your ESP32 code.
- Pull-up Resistor Math: Standard 4.7kΩ pull-ups are fine for short breadboard runs. For runs over 50cm, calculate your pull-ups using the rise-time formula. Typically, dropping to 2.2kΩ pull-ups provides a stronger current source to charge the parasitic capacitance faster. See the TI I2C Pull-up Resistor Calculation app note for the exact RC time constant math.
Output Signal Math: Raw I2C Bytes to Physical Units
Unlike analog sensors (like the TMP36) where you read a voltage and scale it via the ESP32's ADC, the SHT40 outputs a digital 16-bit raw integer (0 to 65535) for both temperature and humidity. You must apply the manufacturer's scaling formula to convert these raw words into physical units.
Temperature Scaling:
The sensor outputs a raw 16-bit value ($S_T$). To convert this to degrees Celsius, use the following formula derived from the Sensirion SHT40 Datasheet:
T (°C) = -45 + 175 × (S_T / 65535)
Relative Humidity Scaling:
The humidity raw word ($S_{RH}$) is converted to a percentage. Note that physical relative humidity cannot exceed 100% or drop below 0%, so the final result must be clamped in your code.
RH (%) = -6 + 125 × (S_{RH} / 65535)
(Clamp result: if RH > 100, set to 100; if RH < 0, set to 0)
Calibration and Scaling Requirements
The SHT40 is factory calibrated. You do not need to perform multi-point calibration or write custom scaling arrays in your firmware. However, if the sensor is exposed to extreme chemical vapors or prolonged 100% condensation, it may require a 'soft reset' command (0x94) sent over I2C to restore the internal baseline. It also features an internal heater that can be pulsed via I2C commands (0x39.24) to burn off condensation before taking a reading.
Step-by-Step Interfacing and Verification
Follow these steps to wire, verify, and read the SHT40 on the ESP32 platform using the Arduino IDE.
- De-energize the Circuit: Ensure the ESP32 is unplugged from USB before making I2C connections to prevent accidental 5V shorts to the SDA line.
- Wire the Power: Connect SHT40 VDD to ESP32 3V3, and GND to GND.
- Wire the Data Lines: Connect SHT40 SDA to ESP32 GPIO 21, and SCL to GPIO 22.
- Install Pull-ups: If your breakout board does not have surface-mount pull-ups already populated, insert two 4.7kΩ through-hole resistors between the SDA/SCL lines and the 3V3 rail.
- Verify Bus Voltage: Power the ESP32. Use a multimeter to probe the SDA and SCL lines. Both should read between 3.2V and 3.3V relative to GND. If they read near 0V, you have a short or a missing pull-up.
- Flash the Verification Code: Use the
Sensirion I2C SHT4xlibrary via the Arduino Library Manager. Run the basic example sketch and open the Serial Monitor at 115200 baud.
Wire.begin()) are roughly 45kΩ — far too weak to drive the SHT40 reliably at 400kHz. Always explicitly disable internal pull-ups and use external 4.7kΩ or 2.2kΩ physical resistors. Refer to the Espressif ESP32 I2C API documentation for low-level driver configuration if you are writing custom C++ firmware.
Final Verdict: Stop Guessing and Standardize
When building a reliable environmental monitoring node, do not leave your component selection to chance or default to the cheapest generic module on a marketplace. For standard temperature and humidity tracking where pressure is not required, the Sensirion SHT40-AD1B is the definitive choice. It provides ±1.8% RH accuracy, survives condensation events that would corrode a BME280, and outputs clean, mathematically predictable 16-bit digital words that eliminate the ADC noise headaches of analog alternatives. Buy the SHT40, use 2.2kΩ external pull-ups, and your I2C bus will remain stable across seasons and temperature swings.






