RuntimeError: Not running on a RPi!
2 min readNov 27, 2023
When working with GPIO pins on a Raspberry Pi using Python, you might encounter an error: RuntimeError: Not running on a RPi!
. This issue often stems from a lack of proper read-write permissions for accessing the GPIO memory, specifically /dev/gpiomem
. Here's a straightforward solution to grant the necessary permissions:
Steps to Grant Access:
- Change Group Ownership:
- The
/dev/gpiomem
file needs to have its group ownership changed to your user's group. This can be done using the command:
sudo chown root:$USER /dev/gpiomem
- This command changes the group owner of
/dev/gpiomem
to your user's primary group.
2. Allow Group Read-Write Access:
- Next, you need to modify the permissions to allow read-write access for the group:
sudo chmod g+rw /dev/gpiomem
- This command changes the group owner of
/dev/gpiomem
to your user's primary group.
- Allow Group Read-Write Access:
- Next, you need to modify the permissions to allow read-write access for the group:
sudo chmod g+rw /dev/gpiomem
- This command grants read-write permissions to the group owner of
/dev/gpiomem
.
Persistence Issue:
- While the above steps are effective, they might not persist after a reboot, meaning you could face the same error again and have to repeat the process. This lack of persistence can be a hassle for regular Raspberry Pi users.
Seeking a Permanent Solution:
- Persistent Permission Setting with udev Rule:
- To make the permission change persistent across reboots, a
udev
rule was created. - A file named
99-gpiomem.rules
was created in/etc/udev/rules.d/
with the following content:
sudo nano /etc/udev/rules.d/99-com.rules
//add this rule in the editor and save and exit
KERNEL=="gpiomem", OWNER="root", GROUP="dialout"
- This rule sets the ownership to
root
and the group todialout
for thegpiomem
device, ensuring the correct permissions are applied at boot.
2. Testing the udev Rule:
- The new udev rule was tested with the following command:
sudo udevadm trigger /dev/gpiomem
3. Adding User to the dialout
Group:
- To provide the user with the necessary permissions, the user was added to the
dialout
group using:
sudo adduser $USER dialout
- Replace
$USER
with your actual username if necessary.
4. Reboot or Log Out and In:
- A reboot or log out/in is required for these changes to take effect.