Bird
0
0
Raspberry Piprogramming~7 mins

Displaying sensor readings on OLED in Raspberry Pi

Choose your learning style9 modes available
Introduction

We use OLED screens to show sensor data so we can see real-time information easily without a computer screen.

You want to show temperature or humidity readings from a sensor on a small screen.
You need a portable display for sensor data in a project like a weather station.
You want to check sensor values quickly without connecting to a computer.
You are building a gadget that shows live data like air quality or light levels.
You want to learn how to connect sensors and displays on a Raspberry Pi.
Syntax
Raspberry Pi
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C
import adafruit_dht

i2c = busio.I2C(board.SCL, board.SDA)
oled = SSD1306_I2C(128, 32, i2c)
dhtDevice = adafruit_dht.DHT22(board.D4)

try:
    temperature = dhtDevice.temperature
    humidity = dhtDevice.humidity
    oled.fill(0)
    oled.text(f"Temp: {temperature:.1f} C", 0, 0, 1)
    oled.text(f"Humidity: {humidity:.1f}%", 0, 16, 1)
    oled.show()
except RuntimeError as error:
    print(error.args[0])

This example uses the Adafruit DHT22 sensor and SSD1306 OLED display libraries.

Make sure your sensor and OLED are connected to the correct Raspberry Pi pins.

Examples
Shows a simple text message on the OLED screen.
Raspberry Pi
oled.text("Hello, OLED!", 0, 0, 1)
oled.show()
Reads temperature and humidity from the sensor and prints to the console.
Raspberry Pi
temperature = dhtDevice.temperature
humidity = dhtDevice.humidity
print(f"Temp: {temperature} C, Humidity: {humidity} %")
Clears the OLED screen and displays formatted temperature and humidity readings.
Raspberry Pi
oled.fill(0)
oled.text(f"Temp: {temperature:.1f} C", 0, 0, 1)
oled.text(f"Humidity: {humidity:.1f}%", 0, 16, 1)
oled.show()
Sample Program

This program reads temperature and humidity from a DHT22 sensor every 2 seconds and shows the values on a 128x32 OLED screen. If the sensor has an error, it prints the error message to the console.

Raspberry Pi
import board
import busio
from adafruit_ssd1306 import SSD1306_I2C
import adafruit_dht
import time

i2c = busio.I2C(board.SCL, board.SDA)
oled = SSD1306_I2C(128, 32, i2c)
dhtDevice = adafruit_dht.DHT22(board.D4)

while True:
    try:
        temperature = dhtDevice.temperature
        humidity = dhtDevice.humidity
        oled.fill(0)  # Clear the screen
        oled.text(f"Temp: {temperature:.1f} C", 0, 0, 1)
        oled.text(f"Humidity: {humidity:.1f}%", 0, 16, 1)
        oled.show()
    except RuntimeError as error:
        print(f"Sensor error: {error.args[0]}")
    time.sleep(2)
OutputSuccess
Important Notes

Use the correct pins for your sensor and OLED connections on the Raspberry Pi.

OLED screens usually need to be cleared before drawing new text to avoid overlapping.

Sensor readings can sometimes fail; handle errors to avoid crashes.

Summary

OLED screens let you see sensor data directly on your Raspberry Pi project.

Use libraries to read sensors and control the OLED easily.

Update the display regularly to show fresh sensor readings.