report #4 raspberry pi 5 — adding a pushbutton switch

Raspberry Pi 5 GPIO connected to LEDs and a push button switch

If it’s not obvious by now I’m sticking with Ubuntu 23.10 on my RPi 5 SBCs, so I won’t be including it in the post title anymore. In this report I add additional functionality as well as clean up the code. The simple circuit now has a push button that connects pin GPIO3 to ground when it’s pressed. This will trigger a button closure event that is monitored and used by the gpiozero Button instance in the Python script.

#!/usr/bin/env pythonfrom gpiozero import LED, Buttonfrom time import sleepfrom datetime import datetimefrom signal import signal, SIGINT, pausefrom sys import exitimport asyncioleds = [LED(22), LED(23), LED(24), LED(25)]button = Button(3)def sigint_handler(signal_received, frame):for led in leds:led.off()print()exit(0)signal(SIGINT, sigint_handler)def flash_leds():while button.is_pressed:for led in leds:led.on()sleep(.04)led.off()sleep(1)for led in reversed(leds):led.on()sleep(.05)led.off()sleep(1)def print_when_pressed():print(f"{datetime.now()}: Button pressed")flash_leds()def print_when_released():print(f"{datetime.now()}: Button released")for led in leds:led.off()button.when_pressed = print_when_pressedbutton.when_released = print_when_releasedpause()

The Python code used to flash the four LEDs in the circuit was cleaned up a bit by creating an array of the LEDs, and then the flash_leds function tightened up using for loop iterators as well as Python’s built-in reverse function, so that the lights flash in one direction, pause, then flash in the reverse direction. Finally, instead of a blind test for true, we check to see if the button is pressed and then flash if it is.

To make the code a bit more interesting the button was given a pair of callback functions to work with, one to print out when the button is pressed, and one when it’s released. This is a demonstration of the capability, nothing more. I put in in because I’ve seen the question asked about how to use this functionality on various fora. If you read the documentation (see the link below) it’s very simple, but none-the-less, here’s what I did after reading the fine manual online. For a lot of tasks, gpiozero Just Works. The fact that it works under Ubuntu 23.10 on the Raspberry Pi is the primary reason I’m sticking with Ubuntu on the Raspberry Pi.

Links

Latest gpiozero documentationhttps://gpiozero.readthedocs.io/en/latest/index.html

report #3 on using ubuntu 23.10 with a raspberry pi 5 — blinkin’ lights

a rant about raspberry pi os

 

The Raspberry Pi OS desktop

I’m still trying to love Raspberry Pi OS and its LXDE-based window manager, and it’s just not happening. I’ve tweaked some things, even going so far as to pull a screen wallpaper from Linux Mint Uma (mikeu_red_waves.jpg); that seems to make the overall desktop experience a bit better for me.

I tried lxappearance to change the desktop. That application opened but I couldn’t change anything.

I then switched from Firefox as my default browser to Chromium because in spite of what was claimed, Firefox is still a hot mess and slower on Raspberry Pi OS than Chromium. I found Chromium a better browser experience than Firefox; Chromium is now my default browser.

That desktop screen capture you see above was taken with grim, a command line tool that is absolutely primitive to use and the only screen capture tool I found that worked. No other screen capture application will work because of the shift from X to Wayland. Basically it’s an all or nothing type of tool. If you want a specific section of the desktop, then you edit the full screen capture with another primitive graphics editing tool to whittle away the extraneous parts. For this post the full desktop was sufficient and required no further post processing.

After flipping back to Raspberry Pi OS and regretting the decision almost immediately, my intent is now to flop back to Ubuntu 23.10. For anything needing SPI support I’m going to move it over to one of my numerous microcontroller boards, such as one of the ESP32-S3 development boards. I can either code something in C++, or drop MicroPython on it. There’s a certain irony in doing a write/port to MicroPython as it won’t work with regular Python 3 under Ubuntu 23.10. But everything else works, and I might even cobble something together to use Bluetooth to communicate from the Raspberry Pi 5 to the ESP32-S3 hosting the MAX7219/1088AS displays. That sounds a lot more productive and a lot less frustrating than trying to beat Raspberry Pi OS into shape.

Yep. That’s my plan.