report #2 on using ubuntu 23.10 with a raspberry pi 5 — gpio usage

This report concerns the configuration of Ubuntu 23.10 on a Raspberry Pi 5 to properly interact with the Pi’s built-in GPIO hardware. But first…

Caveats

  1. I’ve been using the Raspberry Pi 5 since it showed up at my home on 13 November. The first two operating systems I tried were Raspberry Pi OS (October major update) and Ubuntu 23.10 for Raspberry Pi. A lot has changed and improved over the last four weeks with both operating systems.
  2. Many little annoyances have been corrected with both operating systems. For example the December 2023 release of Raspberry Pi OS now supports dark mode and is based on Debian 12. Ubuntu 23.10 has updated its kernel and native Raspberry Pi software so that the active cooling fan is now properly managed and not turned on fully all the time. Both operating systems allow the Raspberry Pi 5’s active cooling to run silently.
  3. The latest Ubuntu 23.10 with patches still doesn’t properly enable GPIO, requiring some changes to the operating system once it’s installed and updated.
  4. There is a tutorial on Tom’s Hardware released in October showing how to use the GPIO with a Python library, which I will neither name nor link to. That’s because that tutorial does not work at all. I have no idea why not. I will show GPIO Zero that works quite well.

How to Achieve Successful GPIO Usage

The first major step is to add a rules file to the filesystem location /etc/udev/rules.d. The file name is 99-com.rules. The listing follows.

SUBSYSTEM=="input", GROUP="input", MODE="0660"SUBSYSTEM=="i2c-dev", GROUP="i2c", MODE="0660"SUBSYSTEM=="spidev", GROUP="spi", MODE="0660"SUBSYSTEM=="*gpiomem*", GROUP="gpio", MODE="0660"SUBSYSTEM=="rpivid-*", GROUP="video", MODE="0660"KERNEL=="vcsm-cma", GROUP="video", MODE="0660"SUBSYSTEM=="dma_heap", GROUP="video", MODE="0660"SUBSYSTEM=="gpio", GROUP="gpio", MODE="0660"SUBSYSTEM=="gpio", KERNEL=="gpiochip*", ACTION=="add", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys/class/gpio && chmod -R g=u /sys/class/gpio'"SUBSYSTEM=="gpio", ACTION=="add", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys%p && chmod -R g=u /sys%p'"# PWM export results in a "change" action on the pwmchip device (not "add" of a new device), so match actions other than "remove".SUBSYSTEM=="pwm", ACTION!="remove", PROGRAM="/bin/sh -c 'chgrp -R gpio /sys%p && chmod -R g=u /sys%p'"KERNEL=="ttyAMA[0-9]*|ttyS[0-9]*", PROGRAM="/bin/sh -c '\ALIASES=/proc/device-tree/aliases; \TTYNODE=$$(readlink /sys/class/tty/%k/device/of_node | sed 's/base/:/' | cut -d: -f2); \if [ -e $$ALIASES/bluetooth ] && [ $$TTYNODE/bluetooth = $$(strings $$ALIASES/bluetooth) ]; then \echo 1; \elif [ -e $$ALIASES/console ]; then \if [ $$TTYNODE = $$(strings $$ALIASES/console) ]; then \echo 0;\else \exit 1; \fi \elif [ $$TTYNODE = $$(strings $$ALIASES/serial0) ]; then \echo 0; \elif [ $$TTYNODE = $$(strings $$ALIASES/serial1) ]; then \echo 1; \else \exit 1; \fi \'", SYMLINK+="serial%c"ACTION=="add", SUBSYSTEM=="vtconsole", KERNEL=="vtcon1", RUN+="/bin/sh -c '\if echo RPi-Sense FB | cmp -s /sys/class/graphics/fb0/name; then \echo 0 > /sys$devpath/bind; \fi; \'"

This file is installed when Raspberry Pi OS is installed. I copied this file to an external thumb drive, and then when I installed Ubuntu 23.10 for Raspberry Pi I copied it back into the same location in the file system. There is currently no package that installs this file on Ubuntu 23.10. Rather than go through what I went through, copy the file verbatim and past it into a text file named as shown, then copy it the location /etc/udev/rules.d. Reboot.

After reboot the GPIO devices will have the group ID of gpio. You will need to create that same group (sudo groupadd gpio) and add that group to the users who will need to access all the GPIO devices (sudo usermod -aG gpio USERID) where USERID is a real user ID. In my case the user on my Raspberry Pi 5 is pi itself, so I executed sudo usermod -aG gpio pi. Once done, log out and then back in again. From this point you will be able to access GPIO resources without root (sudo) access.

The final piece of the puzzle to working with GPIO devices under Ubuntu 23.10 is the installation of gpiozero. I used Python pip to install it. I can’t recall if pip is preinstalled with Python 3 or not. If not pip is easily installed with sudo apt install python3-pip. One note of warning: if you install gpiozero via pip you will run into the EXTERNALLY-MANAGED error. I’ve solved this already, and the solution is in the links at the bottom.

Many may ask why go to this much trouble if the latest version of Raspberry Pi OS has everything essentially built in. The answer is that Ubuntu 23.10 is far more polished than Raspberry Pi OS. I’m used to Ubuntu and Linux Mint (my primary daily driver), an independent derivative of Ubuntu. Ubuntu 23.10 for Raspberry Pi is almost indistinguishable from the x86-64 release, and my skills and tools transfer seamlessly between the variants. The Raspberry Pi 5 has raised the bar with regards to performance and capabilities.  I’m far happier with the quality and capabilities of Ubuntu 23.10 on a Raspberry Pi 5 than Raspberry Pi OS. I only see the quality gap widening between the two distributions going forward.

Links

externally-managed-environment error on debian 12 — FIXED

report #1 on using ubuntu 23.10 with a raspberry pi 5

It has been a while since I posted anything on the blog, and a great while when it was something technically meaty. This is a report on one aspect of using Ubuntu 23.10 on a Raspberry Pi 5/8GB SBC. I will be writing about developing with Microsoft’s C# using Microsoft released tooling within Microsoft’s Visual Studio Code and plugins. I want to acknowledge right up front how we’ve come a tremendous distance with regards to using the Raspberry Pi. Using Visual Studio Code as but one example, I’ve gone from struggling to build VSC on a Raspberry Pi under Raspbian to simply installing VSC from a Microsoft maintained (yes, Microsoft) repo. And that’s just the tip of the iceberg, so to speak.

Visual Studio Code debugging a simple C# program using .net 8 tooling

Let me note up front that I have my Raspberry Pi 5 plugged into an LG 27GL850-B monitor with a native resolution of 2560×1440 (purchased last Christmas during an incredible sale from Amazon), no overscan or special screen manipulation needed. That gives me a rather wide screen, which in turn allows me to open up an editor such as VSC that allows me to set up a vertical three-panel layout (see above) with plenty of space in all three panels.

After following Microsoft’s directions for installing .NET 8 on Ubuntu I was able to follow their basic tutorials on how to compile and debug C# on Linux. Let me again emphasize that this is Linux on a Raspberry Pi 5, which is AArch64/ARM, not x86-64. And it works. I find all of this amazing considering how I started with the original Raspberry Pi 2 ten years ago and struggled to get Python running.

In my not-so-humble opinion the best distribution to run on the Raspberry Pi 5, bar none, is Ubuntu 23.10 for the Raspberry Pi. Everything works with one notable exception: manipulating the GPIO and through that, physical computing. But I have found an easy solution for that (which I will document later), and it all seems to work without a hitch as well as it would under Raspberry Pi OS for the Raspberry Pi.

As they say, more to come.

Links