How to fix Raspberry Pi GPIO "No access to /dev/mem. Try running as root!"
Problem:
You want to toggle a Raspberry Pi GPIO pin using RPi.GPIO
but you see an error message like
Traceback (most recent call last):
File "gpio.py", line 8, in <module>
GPIO.setup(pin1, GPIO.OUT)
RuntimeError: No access to /dev/mem. Try running as root!
Solution
Add your user to theĀ gpio
group:
sudo usermod -a -G gpio $USER
thenĀ log out and log back in(or reboot, if that doesn’t help).
The reason for this error is that /dev/gpiomem
is only accessible by users belonging to the gpio
group. By default, only the pi
user belongs to the gpio
group - hence, if you run a script as any users other than pi
or root
, you will see that error message.