Sub topic 14
When to Choose an I2C Sensor Instead
Assume you know basic Arduino sketches, can wire a breadboard, and have already read a DHT11 or DHT22 with the Adafruit DHT library. This chapter compares the DHT family with I2C temperature and humidity sensors. The goal is to choose the right interface and sensor for a project, not to replace a working DHT sensor without reason.
What I2C Changes
I2C, or Inter-Integrated Circuit, is a two-wire digital communication bus. The Arduino is the controller. Sensors are devices on the bus.
The two signal wires are:
- SDA carries data.
- SCL carries the clock signal.
Both lines need pull-up resistors. Many breakout boards already include them. Unlike a DHT sensor, an I2C sensor does not use a custom single-wire timing protocol. The Arduino sends a device address and commands, then reads registers containing the measurements.
A DHT sensor usually has one device per data connection. An I2C bus can connect several compatible devices, provided each has a unique address and the total wiring remains reliable.
Compare the Trade-Offs
| Decision factor | DHT11 or DHT22 | I2C temperature and humidity sensor |
|---|---|---|
| Wiring | Power, ground, data, and sometimes a pull-up resistor | Power, ground, SDA, SCL, and pull-ups |
| Data rate | Slow | Usually faster |
| Sampling | Often limited to about 1 Hz or 0.5 Hz | Commonly supports more frequent measurements |
| Accuracy | DHT22 is better than DHT11, but modest | Many modern parts offer better accuracy |
| Multiple devices | Usually separate data pins | Several devices can share the bus |
| Software | Simple dedicated library | Library and I2C address management |
| Extra measurements | Temperature and humidity | Some parts also measure pressure or air quality |
Choose a DHT sensor when cost, simplicity, and a single sensor matter more than precision. It works well for a basic room display, classroom project, or slow environmental logger.
Choose an I2C sensor when you need better accuracy, faster sampling, several sensors, or additional measurements. Common choices include the AHT20 for temperature and humidity, and the BME280 for temperature, humidity, and pressure.
Wiring and Address Problems
Connect an I2C breakout board according to its voltage requirements. A typical connection is:
- VIN or 3V3 to the correct supply voltage
- GND to Arduino ground
- SDA to the board's SDA pin
- SCL to the board's SCL pin
The SDA and SCL pins depend on the Arduino model. On an Uno, SDA is A4 and SCL is A5. Some boards provide separate pins labeled SDA and SCL.
I2C devices have addresses such as 0x38 or 0x76. Two devices with the same address can conflict. An address scanner can help find a connected device, but an address that does not appear can also indicate incorrect power, reversed wiring, missing pull-ups, or an unsupported voltage level.
Use short wires and avoid routing SDA and SCL beside noisy motor or relay wiring. Long wires and excessive bus capacitance can cause intermittent readings.
Example: Reading an AHT20
Install the Adafruit AHTX0 library through the Arduino IDE Library Manager. This sketch reads the sensor every two seconds:
#include <Adafruit_AHTX0.h>
Adafruit_AHTX0 aht;
void setup() {
Serial.begin(9600);
if (!aht.begin()) {
Serial.println("Could not find AHT20 sensor");
while (true) {
delay(100);
}
}
}
void loop() {
sensors_event_t humidity;
sensors_event_t temperature;
aht.getEvent(&humidity, &temperature);
Serial.print("Temperature: ");
Serial.print(temperature.temperature);
Serial.println(" C");
Serial.print("Humidity: ");
Serial.print(humidity.relative_humidity);
Serial.println(" %");
delay(2000);
}The library hides the register-level protocol, just as the DHT library hides the DHT timing protocol. You still need to check the library documentation for supported boards, initialization behavior, and measurement limits.
Make the Choice
Use a DHT sensor for a simple, low-rate project with one sensor and relaxed accuracy requirements. Use I2C when the project needs more frequent readings, improved accuracy, several devices on shared wiring, or pressure and other environmental data.
Also consider replacement cost and software support. A familiar DHT sensor may be the better engineering choice when its limitations do not affect the result.
Exercise
You need to measure temperature and humidity every five seconds in one room, and the project has no pressure requirement. Which interface is a reasonable starting choice, and when might you still choose I2C?
Show answer
A DHT22 is a reasonable starting choice because its sampling limit is sufficient and its wiring is simple. Choose I2C instead if you need better accuracy, want to add other I2C devices, or prefer a newer sensor such as the AHT20.