Skip to content

Instantly share code, notes, and snippets.

@Nguimjeu
Last active May 30, 2024 13:54
Show Gist options
  • Save Nguimjeu/6e86eff080beb3ed1589c7bd3d3b8e7f to your computer and use it in GitHub Desktop.
Save Nguimjeu/6e86eff080beb3ed1589c7bd3d3b8e7f to your computer and use it in GitHub Desktop.
OLED Display on Raspberry Pi + Ubunutu Server 21.10

Raspberry Pi + Ubuntu Server 21.10 + OLED Display

  • Raspberry Pi 4 (8GB)
  • Ubuntu Server 21.10 (64-bit server OS for arm64 architectures)
  • Mini-tower case with SSD (NVMe M.2)
  • 128×64 I2C OLED display
  1. Make sure your system is up to date
sudo apt update && sudo apt upgrade -y

Reboot your PI

sudo reboot
  1. Install drivers and libraries
sudo apt install -y python3-pip python3-dev python3-pil python3-setuptools python3-rpi.gpio i2c-tools
  1. Add your current user account to the group i2c
sudo usermod -aG i2c ubuntu
  1. Connect the OLED Display to the Raspberry Pi

  2. Check if the display was recognized

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:~$
  1. Install SSD1306 python libary
sudo pip3 install adafruit-circuitpython-ssd1306
  1. Install monitor script

Clone the script from 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

sudo reboot

You should be able to run the script as following:

python3 stats.py
  1. Run The Script On Start-up

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 &

reboot and you should now have a working OLED stats display that starts up automatically each time your Pi boots up.

All credits to: https://www.the-diy-life.com/add-an-oled-stats-display-to-raspberry-pi-os-bullseye/

@finnie2006
Copy link

Worked like a charm! thanks

@jwilki1
Copy link

jwilki1 commented Sep 28, 2023

This works like a charm on Ubuntu Server 22.04.3 LTS as well. Please forgive my ignorance, but is there a way to convert to F or add F and cycle between the two temperature standards?

@Jedrusia
Copy link

Jedrusia commented May 30, 2024

Unfortunately, as it turned out after starting the project, I have OLED SH1106, which will not work properly instead of SSD1306. Unfortunately, I cannot modify the project myself to adapt SH1106. Would anyone be able to help me get SH1106 up and running (step by step) with RPI4?
SSD1306 = OLED 0.91 and 0.96"
SH1106 = OLED 1.3"
SSD1309 = OLED 1.54"
SSD1309 = OLED 2.42"
None of the descriptions I found work properly and I cannot turn on this display.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment