We use OLED displays to show information like text or numbers in a small, clear way. Displaying text on an OLED helps us see messages directly on the device.
0
0
Displaying text on OLED in Raspberry Pi
Introduction
You want to show sensor readings like temperature or humidity on a small screen.
You need to display status messages or alerts on a device without a full monitor.
You want to create a simple user interface on a Raspberry Pi project.
You want to show time or date on a compact display.
You want to debug or check values without connecting to a computer.
Syntax
Raspberry Pi
from luma.core.interface.serial import i2c from luma.oled.device import sh1106 from PIL import ImageDraw, ImageFont serial = i2c(port=1, address=0x3C) device = sh1106(serial) with canvas(device) as draw: draw.text((x, y), "Your text here", fill="white", font=font)
Use the canvas context to draw on the OLED safely.
Coordinates (x, y) start at the top-left corner of the screen.
Examples
This example shows how to write 'Hello OLED' at position (10, 10) on the screen.
Raspberry Pi
from luma.core.interface.serial import i2c from luma.oled.device import sh1106 from luma.core.render import canvas serial = i2c(port=1, address=0x3C) device = sh1106(serial) with canvas(device) as draw: draw.text((10, 10), "Hello OLED", fill="white")
This example uses a default font to display temperature at the top-left corner.
Raspberry Pi
from luma.core.interface.serial import i2c from luma.oled.device import sh1106 from luma.core.render import canvas from PIL import ImageFont serial = i2c(port=1, address=0x3C) device = sh1106(serial) font = ImageFont.load_default() with canvas(device) as draw: draw.text((0, 0), "Temp: 25C", fill="white", font=font)
Sample Program
This program connects to the OLED screen and shows two lines of text at different positions.
Raspberry Pi
from luma.core.interface.serial import i2c from luma.oled.device import sh1106 from luma.core.render import canvas from PIL import ImageFont # Setup I2C and OLED device serial = i2c(port=1, address=0x3C) device = sh1106(serial) # Load default font font = ImageFont.load_default() # Display text on OLED with canvas(device) as draw: draw.text((5, 5), "Hello, Raspberry Pi!", fill="white", font=font) draw.text((5, 20), "OLED Display Test", fill="white", font=font)
OutputSuccess
Important Notes
Make sure your OLED is connected correctly to the Raspberry Pi's I2C pins.
Use ImageFont.load_default() for simple text or load custom fonts for style.
Always use the canvas context to update the display to avoid flickering.
Summary
OLED displays show text by drawing on a small screen using coordinates.
Use the canvas context to safely draw text on the OLED.
Connect the OLED via I2C and use libraries like luma.oled to control it easily.
