Run an OLED Display with Raspberry PI on Ubuntu 21.10

I recently bought a Raspberry Pi 4 and for one my projects, I wanted to make an OLED display work with the Pi. In this post, I will show you how I was able to achieve it.

This post is inspired from the guide of Michael Clements on how to add an OLED Display with Raspberry Pi OS Bullseye. It is worth taking a look.

What you need

  • Raspberry Pi 4
  • Ubuntu Server 21.10 (I use the 64-bit version)
  • An 128×64 I2C OLED display

Installation

I will assume that you’ve manage to install the 64-bit version of Ubuntu Server 21.10 on the Pi

The first thing you will need to do is to make sure that you OS is up to date

 sudo apt update && sudo apt upgrade -y

Once the system has been update, reboot you Pi.

sudo reboot

The next step will be to install the necessary drivers and libraries to

sudo apt install -y python3-pip python3-dev python3-pil python3-setuptools python3-rpi.gpio i2c-tools

Add your current user account to the group i2c

sudo usermod -aG i2c ubuntu

Connect the OLED Display to the Raspberry Pi. For this part, I will again recommend to visit this post. Once the display is connected, check if it is properly recognized by the system.

i2cdetect -y 1

The output should look like the following:

ubuntu@ubuntu:~$ i2cdetect -y 1
     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: -- -- -- -- -- -- -- --
ubuntu@ubuntu:~$

Install SSD1306 python libary.

sudo pip3 install adafruit-circuitpython-ssd1306

Now I originally wanted to have a display to show information about my system. Therefore for the sake of this post, we will reuse the script of Michael Klements.

git clone https://github.com/mklements/OLED_Stats.git

Go to the cloned directory

cd OLED_Stats

Then run the script by entering:

python3 stats.py

If you have the following error when you execute the script:

ubuntu@ubuntu:~/OLED_Stats$ python3 stats.py
Traceback (most recent call last):
  File "/home/ubuntu/OLED_Stats/stats.py", line 16, in <module>
    oled_reset = digitalio.DigitalInOut(board.D4)
  File "/usr/local/lib/python3.9/dist-packages/digitalio.py", line 145, in __init__
    self.direction = Direction.INPUT
  File "/usr/local/lib/python3.9/dist-packages/digitalio.py", line 175, in direction
    self._pin.init(mode=Pin.IN)
  File "/usr/local/lib/python3.9/dist-packages/adafruit_blinka/microcontroller/bcm283x/pin.py", line 37, in init
    GPIO.setup(self.id, GPIO.IN)
RuntimeError: Not running on a RPi!
ubuntu@ubuntu:~/OLED_Stats$

Pay attention to the last line! It means that you have a persmission problem. There are two methods to solve this issue:

Method 1. Run the script with elevated privileges:

sudo python3 stats.py

Method 2. Make sure the current user has the proper privileges.

With Ubuntu server 21.10 64-bit on RPi 4, that can be achieved by creating a gpio group, and adding an udev rule to give this group access to /dev/gpiomem (which can normally only be accessed by root).

Create a /etc/udev/rules.d/90-gpio.rules file

sudoedit /etc/udev/rules.d/90-gpio.rules

add the following content

KERNEL=="gpiomem", OWNER="root", GROUP="gpio"

Create the group gpio and assign it to an existing user, in my case “ubuntu”:

sudo groupadd -f --system gpio
sudo usermod -a -G gpio ubuntu

Reboot the machine.

sudo reboot

You should now be able to run the script as following:

python3 stats.py

Run the script on system start up

If you want to run the display command script every time you start the system, do the following:

Open up crontab by entering the following command

crontab –e

Add the following line to the end of the file to run the script

@reboot cd /home/ubuntu/OLED_Stats && python3 stats.py &

Now reboot the Pi. Yyou should now have a working OLED stats display that starts up automatically each time your Pi boots up.