I2C OLED Temperature Display

Introduction

This experiment demonstrates how to use an I2C OLED display with a temperature sensor (like the DHT11 or DHT22) to display temperature readings.

I2C OLED Display with Temperature Sensor

Components Needed

Circuit Setup

  1. Connect the I2C OLED display to the SDA and SCL pins on the Arduino.
  2. Connect the DHT sensor's data pin to a digital input pin on the Arduino (e.g., D2).
  3. Connect the power and ground pins of both the OLED and DHT sensor to the 5V and GND pins on the Arduino.
Circuit Setup for OLED Temperature Display

Code for I2C OLED Temperature Display

Upload the following code to your Arduino:


#include 
#include 
#include 
#include 

#define DHTPIN 2
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
Adafruit_SSD1306 display(128, 64, &Wire, -1);

void setup() {
  Serial.begin(9600);
  dht.begin();
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
}

void loop() {
  float temperature = dht.readTemperature();
  if (isnan(temperature)) {
    display.setCursor(0, 0);
    display.print("Failed to read sensor");
  } else {
    display.setCursor(0, 0);
    display.print("Temp: ");
    display.print(temperature);
    display.print(" C");
  }
  display.display();
  delay(2000);
}
            

Explanation

This code reads temperature data from the DHT sensor and displays it on the I2C OLED screen. The display updates every 2 seconds with the current temperature in Celsius.

Troubleshooting