Sub topic 3
Wiring the Sensor to the Arduino
This chapter covers the physical connection between a DHT11 or DHT22 and an Arduino. It assumes you can place components on a breadboard, read a basic schematic, and upload a sketch that uses pinMode and digitalRead. The DHT library comes in a later chapter. Here the goal is a circuit you can trust, so that later read failures point to software and not to a loose wire.
What you are connecting
A DHT sensor has three jobs on the wire side: receive power, share ground, and send data. That gives three electrical connections, and the sensor needs only one data pin on the Arduino.
The bare sensor comes in a 4-pin package. Many breakout boards use a 3-pin version of the same sensor with a pull-up resistor already soldered on.
| Pin (bare sensor, grille facing you, left to right) | Name | Connects to |
|---|---|---|
| 1 | VCC | 5V or 3.3V on the Arduino |
| 2 | DATA | A digital pin, plus a pull-up resistor to VCC |
| 3 | NC | Nothing (not connected) |
| 4 | GND | GND on the Arduino |
Breakout boards usually label their three pins +, OUT (or S), and -. The order varies between manufacturers, so read the silkscreen on your board and do not copy the order from a photo.
Power and voltage
Both sensors accept a range of supply voltages. The DHT11 runs from about 3 V to 5.5 V and the DHT22 from about 3.3 V to 6 V. Check your own datasheet, since clones differ. Two rules follow from this:
- On a 5 V board such as the Uno or Nano, power the sensor from 5V.
- On a 3.3 V board such as an ESP32 or a Due, power the sensor from 3.3V.
Match the sensor's supply to the logic level of the board. The data line swings between 0 V and the supply voltage. If you power a sensor from 5 V and connect its data line to a 3.3 V-only pin, the pin sees 5 V, which can damage it. If you power it from 3.3 V and use a 5 V board, the sensor's HIGH level is lower than 5 V. That usually still registers as HIGH on an Uno, but the margin is small.
The pull-up resistor
The sensor's data pin is an open-drain output. It can pull the line down to ground, but it cannot drive the line up. Something else must pull the line high when nobody is talking. That is the job of the pull-up resistor, connected between DATA and VCC.
Without it, the idle line floats. Floating inputs pick up noise, and the Arduino sees random HIGH and LOW values instead of a clean signal. The usual symptom is a sketch that prints failed reads on every attempt.
Choose the value like this:
- Use 10 kΩ for a short wire (under about 30 cm). A 4.7 kΩ resistor also works and is common.
- Use a lower value such as 4.7 kΩ for longer cables. The lower resistance charges the wire's capacitance faster, so edges stay sharp.
- Skip the resistor only if your breakout board has one built in. Look for a small surface-mount resistor near the pins, or a product description that says "with pull-up resistor."
You may be tempted to use the Arduino's internal pull-up with INPUT_PULLUP. It is typically 20 kΩ to 50 kΩ, which is too weak for reliable reads on all but the shortest wires. Use an external resistor.
The circuit
The following diagram shows the connections for a bare 4-pin sensor on an Uno, using digital pin 2 for data.
Build it in this order:
- With the Arduino unplugged, place the sensor on the breadboard so each pin sits in its own column.
- Run a jumper from the Arduino 5V pin to the power rail, and from GND to the ground rail.
- Connect sensor pin 1 to the power rail and sensor pin 4 to the ground rail.
- Connect sensor pin 2 to Arduino digital pin 2.
- Place the resistor between the sensor's pin 2 column and the power rail. Both ends must be in the correct columns. A resistor that bridges two pins of the sensor itself is a common mistake.
- Leave pin 3 empty.
- Check every connection against the diagram, then plug in the USB cable.
For a 3-pin breakout, skip step 5. Connect + to 5V, - to GND, and OUT to pin 2.
Choosing the data pin
Any digital pin works, and the examples in this tutorial use pin 2. Keep these points in mind:
- Avoid pins 0 and 1 on an Uno. They carry the serial connection to your computer, so a sensor there interferes with uploads and the Serial Monitor.
- On an ESP32, avoid pins that are strapping pins, used at boot, and input-only pins are fine for reading but check your board's pinout. Pick a pin the board documentation lists as a general-purpose I/O.
- Whatever pin you choose, you must use the same number in the sketch. A mismatch between wiring and
#define DHTPINis a frequent cause of failed reads.
An optional decoupling capacitor
The sensor draws a short burst of current during each read. On a long or noisy supply this can produce a voltage dip. A 100 nF ceramic capacitor between VCC and GND, placed close to the sensor, smooths the supply. It is not required for a short breadboard setup, but it costs almost nothing and helps with long cables or shared power rails.
Verifying the wiring without a library
You can test the electrical side before installing anything. The sketch below reads the data pin as a plain input. With the pull-up resistor in place and the sensor idle, the line should read HIGH constantly.
const int DATA_PIN = 2;
void setup() {
Serial.begin(9600);
pinMode(DATA_PIN, INPUT); // plain INPUT: tests the external pull-up
}
void loop() {
int level = digitalRead(DATA_PIN);
Serial.println(level == HIGH ? F("Line is HIGH (pull-up OK)")
: F("Line is LOW (check wiring)"));
delay(1000);
}Open the Serial Monitor at 9600 baud and interpret the output:
| Reading | Likely cause |
|---|---|
| Always HIGH | Wiring is probably fine. |
| Always LOW | DATA shorted to GND, resistor missing, or pin 1 and pin 4 swapped. |
| Changing randomly | Pull-up resistor missing or not connected to the data column. |
This test checks only the idle line, not the sensor's response. A sensor that is dead or powered backwards can still show HIGH. It rules out the most common wiring faults, which is its purpose.
Common wiring faults
- Misread pin order. The grille side faces you when you count pins from the left. Viewing the sensor from behind reverses the order.
- No pull-up on a bare sensor. This produces random or failed reads.
- Loose breadboard contacts. Worn breadboards and thin leads from the sensor can make intermittent contact. Press each pin in, or move to another row.
- Long, unshielded wires. Beyond a meter or so the data edges degrade. Use a lower pull-up value, add the capacitor, and keep the data wire away from motor or relay wiring.
- Sharing a data pin. Each DHT sensor needs its own data pin. Do not wire two sensors to one pin.
- Wrong supply for the board. See the voltage section above.
Fix electrical faults first. A sketch cannot compensate for a floating data line, and most apparent library bugs in the first hour turn out to be wiring.
Exercise
Your Uno reads the data pin in the test sketch and prints "Line is LOW" every time. The sensor is a bare 4-pin DHT22 with a 10 kΩ resistor between its data pin and 5V. Name two wiring faults that could cause this and say how you would check each.
Show answer
First, the data line may be shorted to ground, for example by a jumper in the wrong breadboard row or by pin 1 and pin 4 being swapped so the supply is reversed. Check by unplugging the USB cable and inspecting that the data column has no connection to the ground rail, then compare the pin order with the sensor's markings. Second, the resistor may be connected to the wrong rail or not connected to the data column, so the line is not pulled up. Check by measuring or tracing that one resistor end is in the same column as sensor pin 2 and the other end is on the 5V rail.
Exercise
Why is the Arduino's INPUT_PULLUP mode a poor replacement for the external resistor, and which pins should you avoid for the data line on an Uno?
Show answer
The internal pull-up is roughly 20 kΩ to 50 kΩ, which is too weak to pull the line up quickly enough for reliable reads over anything but a very short wire. On an Uno, avoid pins 0 and 1, which are used for serial communication with the computer.