As an Amazon Associate, Akermin earns from qualifying purchases. Product links on this page are affiliate links.
For a DHT22 or DHT11, wire VCC to 5 V (or 3.3 V), GND to GND, and the data pin to a digital pin such as D2 with a 10 kilohm pull-up resistor to VCC, then read it with a DHT library no more than once every two seconds. For an I2C sensor such as a BME280 or SHT31, wire VCC and GND, then SDA to A4 and SCL to A5 on an Uno (or the board’s labelled SDA and SCL pins), and make sure the library uses the module’s address. If you get NaN, the problem is almost always wiring, a missing pull-up, the wrong sensor type in the code, or reading too fast. Once it works, check the reading against a salt test, which holds about 75 percent relative humidity.
Which sensor you have changes the wiring
Hobby humidity sensors fall into two groups. The DHT family (DHT11, DHT22, also sold as AM2302) uses a single data wire with its own timing protocol. The I2C sensors (BME280, SHT31, SHT4x, AHT20 and similar) share a two-wire bus and carry a fixed address. All of them measure relative humidity with a capacitive or resistive polymer film, and all of them report temperature too, because relative humidity means nothing without it.
| Sensor | Interface | Typical humidity accuracy | Notes |
|---|---|---|---|
| DHT11 | Single wire | Roughly plus or minus 5 percent RH, limited range | Blue case; fine for teaching, poor for monitoring |
| DHT22 / AM2302 | Single wire | Roughly plus or minus 2 to 5 percent RH | White case; one reading every two seconds at most |
| BME280 | I2C or SPI | Roughly plus or minus 3 percent RH | Also gives air pressure; not the same as the BMP280, which has no humidity |
| SHT31 / SHT4x | I2C | Roughly plus or minus 2 percent RH or better | Good for long-term logging |
A common trap is a cheap module sold as BME280 that is actually a BMP280. It will answer on I2C and report temperature and pressure, but humidity will be zero or missing. The chip ID read by the library tells you which one you have.
Wiring step by step
DHT22 or DHT11. A bare sensor has four pins; a module on a small board usually has three, with the pull-up already fitted.
- Connect VCC to the Arduino’s 5 V pin. Most DHT sensors run from 3.3 to 5.5 V; on a 3.3 V board such as an ESP32 use 3.3 V.
- Connect GND to GND.
- Connect the data pin to a digital pin, for example D2.
- On a bare sensor, fit a 10 kilohm resistor between the data pin and VCC. Without it, readings fail or come back as NaN. Leave the fourth pin of a bare DHT unconnected.
- Keep the wire under a couple of metres. Longer runs need a lower pull-up value and still get unreliable.
BME280, SHT31 and other I2C modules.
- Check the module’s supply. Many breakouts include a regulator and level shifting and accept 5 V on VIN; bare 3.3 V boards can be damaged by 5 V on VCC or the data lines.
- Connect VCC (or VIN) and GND.
- On an Uno or Nano, connect SDA to A4 and SCL to A5. On a Mega they are pins 20 and 21. On newer boards use the pins marked SDA and SCL.
- Run an I2C scanner sketch to find the address. A BME280 is usually 0x76 or 0x77; an SHT31 is usually 0x44 or 0x45. Pass that address to the library if it is not the default.
What the code needs, and why readings come back NaN
Install a library for your sensor through the Arduino Library Manager. For DHT sensors the widely used one is the DHT sensor library together with its unified sensor dependency. In the sketch you declare the pin and the sensor type (DHT11 or DHT22), call begin in setup, then in the loop call readHumidity and readTemperature, check whether either result is NaN, and print them. Wait at least two seconds between reads for a DHT22, one second for a DHT11; reading faster returns stale data or failures. For I2C sensors the library typically gives a begin call that takes the address and returns false if the sensor is not found, which is your first diagnostic.
| Symptom | Likely cause | Fix |
|---|---|---|
| NaN or failed to read | Loose jumper, wrong pin number, no pull-up, wrong DHT type in code | Reseat wires, match the pin, add 10 kilohm, set DHT11 or DHT22 correctly |
| Sensor not found on I2C | SDA and SCL swapped, wrong address, 3.3 V module unpowered | Swap the wires, run a scanner, check VCC |
| Humidity always 0 | BMP280 instead of BME280 | Read the chip ID; buy a genuine humidity sensor |
| Humidity stuck at 99 or 100 percent | Condensation on the film, or a failed sensor | Dry it in a warm room for several hours, then retest |
| Humidity a few points low, temperature high | Sensor heated by the board, regulator or its own frequent reads | Move it away from the board on wires, read less often |
The last row matters more than most tutorials admit. Relative humidity falls by roughly 3 to 5 percentage points for every degree Celsius of warming at room conditions, so a sensor sitting a degree warm on top of a voltage regulator reads noticeably dry. Mount it on short leads, outside any enclosure, or in a vented housing with the electronics below it.
Checking the numbers are right
A working sketch does not mean accurate readings. The simplest home reference is the saturated salt test: put a few spoons of table salt in a bottle cap, add just enough water to make a damp paste, and seal it with the sensor in a zip bag or airtight container. After several hours at a steady room temperature, the air above saturated sodium chloride sits at about 75 percent RH. Our guide on how to calibrate a hygrometer covers the method in detail.
If the sensor reads 71 percent in the salt test, add 4 points in your code as an offset. A single-point offset is good near that humidity; for a full range check, a second reference such as a magnesium chloride salt at about 33 percent RH lets you correct the slope as well. You can also put the Arduino next to a known-good hygrometer such as the JEDEW digital hygrometer in a still room for an hour and compare. Expect two cheap sensors to disagree by a few percent; that is normal.
Once you trust temperature and RH, you can compute dew point in the sketch, which tells you whether surfaces will condense. The arithmetic is in our article on how to calculate dew point from temperature and humidity.
When to give up on a sensor
Polymer humidity films age, faster in dusty, high-humidity or chemically dirty air. Replace a sensor that stays above 95 percent after drying out, drifts more than about 5 points from a salt reference after you corrected it, or reports temperatures that make no sense. DHT11 sensors in particular are not worth trusting for anything that matters, such as mould prevention, a reptile enclosure or a server cupboard; spend a little more on a DHT22 or an SHT series part.
For mains-powered projects, keep the Arduino and sensor on a low-voltage supply from a certified adapter, and never switch mains loads such as humidifiers or fans directly from a hobby relay board in a damp location unless the enclosure and wiring are suitable for it.
Get one good reading, then test it
Wire the sensor, run the example sketch from its library until you see sensible numbers, then put it in a sealed salt test overnight. Whatever offset you find from 75 percent goes into your code, and you will have a logger you can actually believe.
Frequently Asked Questions
Can I connect two humidity sensors to one Arduino?
Yes. Two DHT sensors each need their own data pin. Two I2C sensors can share SDA and SCL if their addresses differ; many modules let you change the address with a solder jumper or address pin.
Does a DHT22 need a resistor?
A bare four-pin DHT22 needs a pull-up of around 10 kilohm between data and VCC. Most three-pin modules on a small circuit board already include one, so check the board for a small resistor before adding another.
Why does my humidity reading jump around?
Breath, drafts and the sensor's own warming all move humidity readings quickly. Average several readings, keep the sensor away from the board and your hands, and read at the slowest rate your project allows.
Can I use 5 V with an ESP32 or other 3.3 V board?
Power the sensor from 3.3 V on those boards. The data line of a sensor powered at 5 V can push 5 V into a 3.3 V input pin, which can damage it unless you use a level shifter.
Is a BME280 better than a DHT22?
For most projects, yes. It is faster, uses I2C, includes pressure, and tends to be steadier, though fakes are common. A genuine SHT31 or SHT4x is usually the better choice if humidity accuracy is the main goal.