I want to build a small robot that can control individual LEDs (WS2811) via a Raspberry Pi and also implement an audio output. I control the LEDs via the rpi_ws281x library and the audio output via a subprocess.call function. My problem now is that the LEDs have to be controlled via a sudo command, otherwise the error message (Can't open /dev/mem: Operation not permitted
Traceback (most recent call last):
File "/home/pi/Desktop/robot.py", line 97, in <module>
strip.begin()
File "/home/pi/.local/lib/python3.11/site-packages/rpi_ws281x/rpi_ws281x.py", line 143, in begin
raise RuntimeError('ws2811_init failed with code {0} ({1})'.format(resp, str_resp))
RuntimeError: ws2811_init failed with code -5 (mmap() failed)
Memory access error) occurs.
However, the audio output via subprocess.call only works without executing the sudo command. Since the two functions were implemented in a file, I cannot execute them with sudo and also not without sudo. So I have now tried to add GPIO output 12, which I use to control the LEDs, to groups in order to obtain more rights for the LEDs only. Unfortunately, nothing changed when I added my user "pi" to the "Gpio" and "kmem" groups. I have read in other forums that many advise switching to a GPIO PIn with SPI, but unfortunately I have not understood why.
This things I tried:
"Gpio" und "kmem" added to group user pi. Executing this command:
sudo usermod -aG gpio www-data
sudo chown root:gpio /dev/gpiomem
sudo chmod g+rw /dev/gpiomem
Adding one line in file: /etc/udev/rules.d/99-custom-permissions.rules:
KERNEL=="mem", GROUP="kmem", MODE="0660"
Adding to sudoers file: %pi ALL=(ALL) ALL
Can anyone help me please?