A practical Arduino Uno project • DHT22 sensor • 0.96-inch SSD1306 OLED • Updates every two seconds
A practical weekend project. Read temperature and humidity with a DHT22 sensor and show the values live on a small OLED display, using an Arduino Uno. No cloud service, no phone app — just a sensor, a screen, and a few lines of code. A good starter project for learning how to work with sensors and I2C displays.
If you have never combined a sensor and a display in the same sketch before, this is a comfortable place to start. Each part does one clear job, the wiring is short, and you can see the result on the screen as soon as the sketch is uploaded.
What you will build
You will build a small monitor that reads temperature and humidity every two seconds and shows them on a 0.96-inch OLED display, while also printing the values to the Serial Monitor.
That second output matters while you are building. If the display stays blank, the Serial Monitor still tells you whether the sensor is being read. If the serial output looks right but the screen does not, you know to check the display wiring and its I2C address instead of starting again from the beginning.
Components
You need only the parts below. Nothing else is required for the version in this article.
- Arduino Uno (or any Arduino-compatible board)
- DHT22 temperature and humidity sensor
- 0.96" OLED display (SSD1306, I2C)
- Breadboard and jumper wires
- 10k resistor (pull-up for the DHT22 data line)
- USB cable for power and programming
Wiring
Connect everything with the power off, then check each wire once before you plug in the USB cable. Pay particular attention to the pull-up resistor on the DHT22 data line — it is easy to leave out, and the readings can fail without it.
- DHT22 data pin — Arduino digital pin 2, with a 10k pull-up resistor to 5V
- DHT22 VCC — 5V
- DHT22 GND — GND
- OLED SDA — Arduino A4
- OLED SCL — Arduino A5
- OLED VCC — 3.3V (or 5V if the module supports it)
- OLED GND — GND
Libraries
Install these three libraries before you compile the sketch. In the Arduino IDE, open the Library Manager and install each one by name:
- DHT sensor library
- Adafruit GFX Library
- Adafruit SSD1306
Installing them first avoids the most common compile errors for this project, which are simply missing-library errors.
The full sketch
Copy the sketch below into the Arduino IDE, select your board and port, and upload it. The sketch is complete as shown — it needs no extra files and no changes to the pin numbers if you have followed the wiring above.
#include <DHT.h> #include <Wire.h> #include <Adafruit_GFX.h> #include <Adafruit_SSD1306.h> #define DHTPIN 2 #define DHTTYPE DHT22 #define SCREEN_WIDTH 128 #define SCREEN_HEIGHT 64 DHT dht(DHTPIN, DHTTYPE); Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); void setup() { Serial.begin(9600); dht.begin(); if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) { Serial.println("OLED not found"); for (;;); } display.clearDisplay(); display.setTextSize(1); display.setTextColor(SSD1306_WHITE); } void loop() { float h = dht.readHumidity(); float t = dht.readTemperature(); if (isnan(h) || isnan(t)) { Serial.println("Failed to read from DHT sensor"); delay(2000); return; } display.clearDisplay(); display.setCursor(0, 0); display.print("Temperature: "); display.print(t); display.println(" C"); display.print("Humidity: "); display.print(h); display.println(" %"); display.display(); Serial.print("Temp: "); Serial.print(t); Serial.print(" C, Humidity: "); Serial.print(h); Serial.println(" %"); delay(2000); } How it works
In setup, the sketch starts the serial port and the sensor, then initialises the OLED; if the display is not found, the sketch stops and reports it. In the loop, the sketch reads humidity and temperature, skips the update if either reading is not a number, otherwise clears the display, prints the two values, refreshes the screen, and mirrors the same values to the Serial Monitor. The readings update every two seconds.
That check for a failed reading is worth keeping. Instead of printing nonsense values or freezing, the sketch prints a short message to the Serial Monitor, waits, and tries again on the next cycle.
Troubleshooting
If something does not work on the first upload, work through these three checks in order:
- The OLED I2C address is usually 0x3C; if nothing shows on the screen, try 0x3D.
- If the readings fail, check the wiring and the 10k pull-up resistor on the DHT22 data line.
- Make sure all three libraries are installed before compiling.
Most first-build problems with this project come back to one of those three: the display address, the data-line wiring, or a missing library.
What is next
Once the basic monitor is running, there are two natural next steps to try: add a buzzer for a high-temperature alarm, or send the readings over Wi-Fi so they can be checked from a phone.
Either extension builds directly on the same sketch. The sensor reading and the display code stay as they are, and you add one new output at a time — which is a good habit for small embedded projects in general.