Sub topic 14
When to Choose an I2C Sensor Instead
This chapter assumes you can already wire and read a DHT22, handle NaN results, and respect its two-second sampling interval. You should also know how setup() and loop() work and how to use Serial. No earlier I2C experience is needed. The chapter explains the parts of I2C that matter for this decision.
Why the question comes up
A DHT sensor is cheap and needs only one data pin. It also has limits you have probably already met:
- The accuracy is modest. A DHT22 is typically rated around ±0.5 °C and ±2 to 5 % relative humidity. A DHT11 is much coarser.
- Reads are slow. You must wait about two seconds between reads, and a DHT11 allows only about one read per second.
- Reads depend on timing. The single-wire protocol uses pulses measured in microseconds. A busy sketch or a long cable can cause
NaNresults. - Each sensor needs its own data pin.
An I2C sensor addresses these limits. It does not make every project better. This chapter helps you decide when the change is worth it.
What I2C is
I2C (Inter-Integrated Circuit) is a two-wire serial bus. One wire, SDA, carries data. The other, SCL, carries a clock signal. The Arduino acts as the controller and starts every transfer. Each sensor on the bus is a peripheral with a fixed 7-bit address, such as 0x44 or 0x76.
Because every device has an address, several sensors can share the same two wires. The bus lines have pull-up resistors, and most breakout boards include them. Other bus devices, such as an OLED display, can share the same SDA and SCL lines.
| Board | SDA pin | SCL pin |
|---|---|---|
| Arduino Uno / Nano | A4 | A5 |
| Arduino Mega | 20 | 21 |
| Arduino Leonardo | 2 | 3 |
| ESP32 (default Wire pins) | 21 | 22 |
Newer Uno R3 boards also have dedicated SDA and SCL header pins. They connect to the same signals as A4 and A5. Check your board's pinout if you use a different model.
Comparing DHT and I2C sensors
| Property | DHT22 | SHT31 | BME280 |
|---|---|---|---|
| Interface | Single wire, custom timing | I2C | I2C (also SPI) |
| Typical temperature accuracy | ±0.5 °C | ±0.3 °C | ±0.5 °C to ±1 °C |
| Typical humidity accuracy | ±2 to 5 % | ±2 % | ±3 % |
| Also measures | Nothing | Nothing | Pressure |
| Minimum read interval | About 2 s | Well under 1 s | Well under 1 s |
| Pins needed | 1 data pin | SDA and SCL (shared) | SDA and SCL (shared) |
| Typical supply voltage | 3.3 to 5 V | 2.4 to 5.5 V | 1.7 to 3.6 V on the chip |
| Relative cost | Low | Moderate | Low to moderate |
These figures vary by manufacturer, batch, and breakout board. Treat them as typical values and read the datasheet for the exact part you buy.
The BME280 chip runs at 3.3 V. Most breakout boards include a regulator and level shifting so they work on a 5 V Arduino. Confirm this for your board before you connect it to 5 V.
Decision criteria
Choose based on the requirement that matters most for your project.
- You need better accuracy or less drift. The SHT3x and similar parts are specified more tightly than the DHT22. They also tend to be more stable over time.
- You need faster updates. I2C sensors can be read many times per second. Check the datasheet, because the chip's internal measurement time still applies.
- You need more than one sensor. Two DHT sensors need two pins. Two I2C sensors with different addresses need only SDA and SCL.
- Your sketch is timing-sensitive. The DHT protocol is sensitive to interrupts and long delays. I2C is clocked by the controller, so a late read does not corrupt the data.
- You need pressure or air-quality data. The BME280 adds pressure. The BME680 adds a gas sensor.
- You need a low-power design. Many I2C sensors have sleep modes and single-shot measurement. This matters for battery projects.
- You want the cheapest and simplest build. A DHT22 or DHT11 is hard to beat for a first project or a low-stakes indoor reading.
Stay with a DHT sensor when a reading every few seconds is enough, ±2 % humidity is acceptable, and a spare digital pin is available.
Wiring an I2C sensor
An SHT31 breakout has four connections you need:
- VIN to the Arduino 5 V (or 3.3 V, depending on the board).
- GND to ground.
- SDA to A4 on an Uno.
- SCL to A5 on an Uno.
There is no data pin with a separate pull-up resistor like the one you added for the DHT. Most breakout boards already include I2C pull-ups. If you connect several boards, the combined pull-ups act in parallel. Two or three boards are usually fine. A long chain may need some pull-ups removed.
Finding the address
If a sketch cannot find the sensor, the usual cause is a wrong address or a wiring fault. An I2C scanner sketch tells you which addresses respond.
#include <Wire.h>
void setup() {
Serial.begin(9600);
Wire.begin();
Serial.println(F("Scanning..."));
byte found = 0;
for (byte address = 1; address < 127; address++) {
Wire.beginTransmission(address);
if (Wire.endTransmission() == 0) {
Serial.print(F("Device at 0x"));
if (address < 16) Serial.print('0');
Serial.println(address, HEX);
found++;
}
}
if (found == 0) {
Serial.println(F("No I2C devices found."));
}
}
void loop() {
}Common results are 0x44 or 0x45 for an SHT31 and 0x76 or 0x77 for a BME280. A solder jumper or an address pin on the breakout board selects between the two addresses. This is how you place two identical sensors on one bus.
Reading an SHT31
The Adafruit SHT31 library installs from the Library Manager. Search for "Adafruit SHT31 Library". The Adafruit BusIO dependency is installed with it.
#include <Wire.h>
#include <Adafruit_SHT31.h>
Adafruit_SHT31 sht31 = Adafruit_SHT31();
void setup() {
Serial.begin(9600);
if (!sht31.begin(0x44)) {
Serial.println(F("SHT31 not found. Check wiring and address."));
while (true) {
delay(10);
}
}
}
void loop() {
float temperatureC = sht31.readTemperature();
float humidity = sht31.readHumidity();
if (isnan(temperatureC) || isnan(humidity)) {
Serial.println(F("Failed to read from SHT31!"));
} else {
Serial.print(temperatureC);
Serial.print(',');
Serial.println(humidity);
}
delay(1000);
}Compare this with your DHT sketch. The structure is the same: a library object, a start-up call, two read calls, and an isnan check. The main differences are these:
begin()returns a result that tells you whether the sensor answered. With a DHT, a missing sensor shows up only asNaNlater. Here you can stop with a clear message at start-up.- You pass the I2C address to
begin(). - There is no two-second wait imposed by the protocol. The one-second
delayhere is a choice, not a requirement.
The earlier tools still apply. The heat index, dew point, and calibration offset code from earlier chapters works on any temperature and humidity pair. The non-blocking millis pattern also carries over, and you can use a shorter interval.
Reading a BME280
A BME280 adds pressure. The Adafruit BME280 library follows a similar pattern.
#include <Wire.h>
#include <Adafruit_BME280.h>
Adafruit_BME280 bme;
void setup() {
Serial.begin(9600);
if (!bme.begin(0x76)) {
Serial.println(F("BME280 not found. Check wiring and address."));
while (true) {
delay(10);
}
}
}
void loop() {
float temperatureC = bme.readTemperature();
float humidity = bme.readHumidity();
float pressureHpa = bme.readPressure() / 100.0;
Serial.print(temperatureC);
Serial.print(',');
Serial.print(humidity);
Serial.print(',');
Serial.println(pressureHpa);
delay(1000);
}readPressure() returns pascals, so dividing by 100 gives hectopascals. Some cheap boards sold as BME280 are actually BMP280 chips, which have no humidity sensor. If readHumidity() returns a constant or NaN, check the chip marking and the chip ID the library reports.
Common problems
- The scanner finds nothing. Check that SDA and SCL are not swapped. Check power and ground. Confirm that the board has pull-ups.
- The sensor is found at a different address. Use the address the scanner reports, not the one in the example.
- The bus works for a while, then locks up. Long wires, a loose breadboard connection, or electrical noise can hang the bus. Keep wires short, and keep I2C wiring away from motors and relays.
- Two sensors conflict. They share an address. Change one with the address jumper, or use an I2C multiplexer such as the TCA9548A.
- Readings are warm. A sensor placed near a regulator, display, or the Arduino itself reads high. This is a placement problem, and a new sensor does not fix it. Mount the sensor away from heat sources.
Costs of switching
An I2C sensor is not free of trade-offs.
- Pin and library changes. You rewrite the declaration and start-up code, and you add a second library.
- Price and availability. Parts like the SHT31 cost more than a DHT22, and clone boards vary in quality.
- Cable length. I2C is meant for short runs, typically under about one metre without extra care. A DHT cable can run longer at the cost of reliability.
- Voltage matching. You must check that the board matches your logic level.
- Complexity. A bus with several devices needs an address plan.
For a first project or a single indoor reading every few seconds, a DHT22 is still a reasonable choice. Move to I2C when a requirement from the decision list applies to you.
Exercise
You are building a greenhouse monitor. It needs temperature and humidity readings from four locations. It must also log air pressure, and it should update about once per second. Would you choose DHT22 sensors or I2C sensors? Give two reasons, and explain how you would connect four sensors if they all use the same default address.
Show answer
Choose I2C sensors, for example BME280 boards. First, DHT22 sensors need a two-second interval between reads, which conflicts with the one-second update target. Second, a DHT22 does not measure pressure, while a BME280 does, and four I2C sensors can share SDA and SCL instead of using four data pins. If all four have the same address, a BME280 has only two selectable addresses (0x76 and 0x77), so use an I2C multiplexer such as the TCA9548A. Connect each sensor to its own multiplexer channel and select the channel before each read.
Exercise
Your I2C scanner sketch prints "No I2C devices found." on an Uno with an SHT31 breakout. List three things you would check, in order.
Show answer
First, check power and ground, and confirm the breakout board has power. Second, check that SDA goes to A4 and SCL goes to A5 and that the two are not swapped, and check for loose breadboard connections. Third, confirm that the board is a genuine I2C part with its pull-ups present, and try a shorter wire or a different breadboard row. If the scanner still finds nothing, test the sensor on another board to rule out a faulty breakout.