Skip to content

Instantly share code, notes, and snippets.

@Nguimjeu
Last active September 28, 2023 18:43
  • Star 6 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
Star You must be signed in to star a gist
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/

@thatmikewilliams
Copy link

thatmikewilliams commented Jul 6, 2022

Brilliant instructions!!

Couple of observations from my experience installing this on u20.04:

Check if the display was recognized
i2cdetect -y 1

On my pi this needed
sudo i2cdetect -y 1

On running python3 stats.py I got the following error repeating but my oled came alive:

/bin/sh: 1: vcgencmd: not found
/bin/sh: 1: vcgencmd: not found
/bin/sh: 1: vcgencmd: not found
/bin/sh: 1: vcgencmd: not found

I ran this to solve that one:
sudo apt-get install libraspberrypi-bin -y

Then I got this:

VCHI initialization failed
VCHI initialization failed
VCHI initialization failed

The solution to this for me was adding the video group to the ubuntu user:
sudo usermod -aG video ubuntu

However, weirdly, logging out and logging back in wasn't enough - you need to reboot at this point.

Not sure if this is my browser or character encoding issue but copy pasting this crontab –e gave me a confusing error because I know this is correct:

$ crontab –e
–e: No such file or directory

If you type crontab -e it works fine.

Thanks for this @Nguimjeu

@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?

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