Setup OLED Display SSD1306 on Raspberry Pi

Subscribe Send me a message home page tags


#raspberry-pi  #OLED display  #I2C 

In this post, we will describe how we can set up the OLED display SSD1306 on Raspberry Pi.

Related Readings

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:

The wiring is shown in the figure below:

ssd1306-setup.jpg

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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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 -----