In this post, we will describe how we can set up the OLED display SSD1306 on Raspberry Pi.
Related Readings
- Getting started with Raspberry Pi I2C and Ubuntu Server
- Using an I2C OLED Display Module with the Raspberry Pi
Configuration
Hardware: The OLED display module used in this post is this one.
Raspberry Pi Model: 3B+
OS: Ubuntu 20.04
Python version: 3.8
Instruction
The setup consists of the following steps:
- Wire the cable and connect the display module.
- Enable I2C on Raspberry Pi and verify Raspberry Pi can detect I2C line.
- Download the SSD1306 driver and install it.
- Write a code to display some text on the OLED display.
The wiring is shown in the figure below:

To enable I2C on Raspberry Pi, we can execute the following commands:
sudo apt update sudo apt upgrade -y sudo apt install -y i2c-tools python3-pip
We need to verify that Raspberry Pi can detect the I2C line by using the following command:
sudo i2cdetect -y 1
The command should output something like the following:
0 1 2 3 4 5 6 7 8 9 a b c d e f 00: -- -- -- -- -- -- -- -- -- -- -- -- -- 10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 30: -- -- -- -- -- -- -- -- -- -- -- -- 3c -- -- -- 40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 70: -- -- -- -- -- -- -- --
Now we can install the SSD1306 drive:
git clone https://github.com/adafruit/Adafruit_Python_SSD1306.git cd Adafruit_Python_SSD1306 sudo python3 setup.py install
At this point, the basic setup is pretty much done. In order to display texts and run the script provided in this post, we need to install the rpi.gpio
and PIL
packages:
sudo apt-get update sudo apt-get install rpi.gpio sudo python3 -m pip install --upgrade Pillow
The script below will display the user "ubuntu" and the ip address of the Raspberry Pi on the display module:
import Adafruit_SSD1306
import subprocess
from PIL import Image, ImageDraw, ImageFont
import RPi.GPIO as GPIO
display = Adafruit_SSD1306.SSD1306_128_64(rst=None)
display.begin() # initialize graphics library for selected display module
display.clear() # clear display buffer
display.display() # write display buffer to physical display
displayWidth = display.width # get width of display
displayHeight = display.height # get height of display
image = Image.new('1', (displayWidth, displayHeight)) # create graphics library image buffer
draw = ImageDraw.Draw(image) # create drawing object
font = ImageFont.load_default() # load and set default font
#draw.text((0,0), "Hello,\nRaspberry Pi!", font=font, fill=255) # print text to image buffer
output = subprocess.check_output(['ifconfig']).decode('utf-8')
isWlan = False
for line in output.split('\n'):
if line.startswith('wlan') and not isWlan:
isWlan = True
elif isWlan and 'inet' in line and 'netmask' in line:
ipaddressLine = line
startIndex = ipaddressLine.find('inet')
endIndex = ipaddressLine.find('netmask ')
ipAddress = ipaddressLine[startIndex : endIndex].replace('inet', '').strip()
message = "User: ubuntu\nIP Address:\n{}".format(ipAddress)
draw.text((0,0), message, font=font, fill=255)
display.image(image) # set display buffer with image buffer
display.display() # write display buffer to physical display
GPIO.cleanup() # release all GPIO resources
----- END -----
©2019 - 2024 all rights reserved