Introduction
This experiment demonstrates how to use an I2C OLED display with a temperature sensor (like the DHT11 or DHT22) to display temperature readings.
Components Needed
- OLED Display (I2C)
- Temperature Sensor (e.g., DHT11 or DHT22)
- Arduino (e.g., Uno, Nano)
- Jumper wires
Circuit Setup
- Connect the I2C OLED display to the SDA and SCL pins on the Arduino.
- Connect the DHT sensor's data pin to a digital input pin on the Arduino (e.g., D2).
- Connect the power and ground pins of both the OLED and DHT sensor to the 5V and GND pins on the Arduino.
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
- If the display is blank, check the wiring and ensure the I2C address is correct in the code.
- If the temperature reading is inaccurate, check the sensor's calibration and wiring.