2

I have a fairly substantial hardware control application that I'm currently running on a RPI 3b. I'm using nearly every pin on the GPIO, including using PWM output, I2C and multiple 1-wire buses. The application uses pigpio (python bindings) to control the GPIO/PWM/I2C and w1thermsensor to do the 1-wire temperature readings

I'd like to update to RPI 5, but as I understand it, the major hardware changes involved mean that pigpio will no longer work and there doesn't seem to be much sign of that getting fixed any time soon. I've checked out various other libraries briefly like WiringPi, gpiod and gpiozero but they all seem to have limitations/problems that make the choice non-obvious:

  • WiringPi -> although there's a new fork that supports rpi5, I can't find any new python bindings for it. I'm really not interested in having to write my own at this point; I'm not a C programmer. But maybe I've just missed where I can get the python bindings (or can I just use the old ones?)
  • gpiod -> doesn't seem to have any DMA support, maybe not important. Doesn't seem to be any documentation for python bindings. Not enormously excited about the idea of having to guess at the python API without any docs
  • gpiozero -> seems too opinionated. I already have all my IO code written based on direct access to the GPIO using pigpio, converting it all to use a "device-based" library is going to be a bit of PITA. Can't see any I2C support.

Although I'm using the hardware PWM outputs, both these and the I2C bus are operating at relatively slow speeds so I can probably get away without hardware support in a pinch, providing that I can still use the same pins on the GPIO header (I have a whole load of downstream breakout boards etc that I really don't want to have to rebuilt and test for a new pin config.

I'm hoping that w1thermsensor will just continue to work as before.

I'm guessing I'm going to have to accept that this upgrade isn't going to be painless. Can anyone suggest what the least painful way of converting my existing python code for pi 5 would be?

2
  • 2
    I suspect you are in for a lot of pain. Have a look at abyz.me.uk/lg In particular abyz.me.uk/lg/py_lgpio.html Commented Oct 5, 2024 at 17:27
  • I'm not a Python programmer, but AFAIK there simply is no direct way to upgrade older Python apps to an RPi5. I think you are stuck with the following choices: 1) WiringPi offers a C API; its gpio module supports shell programming (and Python), 2) "Kernel code" is available for I2C (i2ctools) and PWM (through sysfs), and 3) pinctrl for general GPIO control. Commented Oct 6, 2024 at 5:00

1 Answer 1

2

I would suggest you forget any attempt to upgrade your code. There are so many differences this would be a nightmare. I suggest you write new code building up each functional block one by one.

If you want to use gpiod the version included in Raspberry Pi OS is 3 years old and has been replaced by V2.
This can be installed in a virtual environment using pip and is more usable. Having said that the documentation leaves a lot to be desired (it assumes a good understanding of the gpiochip interface), but it does have internal documentation which you can access via the normal python methods.

NOTE gpiod ONLY supports GPIO functionality it was never intended to support other functions. To use I²C you need to use the kernel functionality.

NONE of the existing libraries support hardware PWM, only software PWM.

Note gpiozero uses lgpio as its underlying library. lgpio is reasonably documented and behaves more like the older libraries and includes software timed PWM.

There is kernel support for PWM using the sysfs interface, but poorly documented. I have not been successful in getting it to work on Pi5, but Seamus has posted some code in another answer.

I have pretty well given up on PWM on Pi5 and use a Pico, which supports 8 hardware PWM blocks, each block has two signal outputs, for a total of up to 16 PWM outputs.

5
  • I think you're under a misapprehension about the size of the rest of my code. The GPIO/PWM/i2c interface is part of a large application with a lot of logic that controls half my house. "Not upgrading" is not an option. Fortunately, I put the GPIO interface code behind a facade interface, so swapping out the library used to talk to the hardware shouldn't be too difficult. I already have a dummy implementation of that interface so I can write E2E tests for the application to run on another machine without the GPIO hardware. Software PWM might be ok, but I still need something for i2C :-/ Commented Oct 7, 2024 at 8:39
  • I did not underestimate the difficulty but as you have posted absolutely no code you can only expect generic comments. Unfortunately pigpio while a exceptional piece of code would be almost impossible to emulate (at least without writing your own kernel driver). There is a shim for RPi.GPIO and I wrote one for my python library but gpiod only supports GPIO I/O. I²C is simple and there are plenty of libraries and it is easy using ioctl Commented Oct 7, 2024 at 9:22
  • Realistically WHY do you think you need to migrate to a different platform? Pi3+ is still available and will be for an extended period. Commented Oct 7, 2024 at 9:27
  • 1
    Because I'm an idiot and I didn't realise how different the RPI5 was before I bought it. I checked that the pinout was the same and assumed it was just an upgrade like 3->4 that would be more or less compatible. I wanted some redundancy (the SD card in my Pi3 ate itself recently and this thing runs my heating) and the Pi 3 is a little slow when running HomeAssistant + my hypercorn app that runs the backend so I figured on just using a more powerful platform. One solution here might be to move HA to the 5 and buy a 4 as my hot spare instead. Commented Oct 7, 2024 at 9:56
  • I've quoted you here Commented Oct 15 at 11:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.