From a7a9598bc8fc19b5317e55b3fd670acb0a17e08f Mon Sep 17 00:00:00 2001 From: androiddrew Date: Sun, 5 Apr 2026 18:46:38 -0400 Subject: [PATCH] Working LED example --- gpio/LED/README.md | 5 +++++ gpio/LED/main.go | 2 +- gpio/README.md | 38 ++++++++++++++++++++++++++++++++++++++ gpio/udev/99-gpio.rules | 7 +++++++ 4 files changed, 51 insertions(+), 1 deletion(-) create mode 100644 gpio/LED/README.md create mode 100644 gpio/udev/99-gpio.rules diff --git a/gpio/LED/README.md b/gpio/LED/README.md new file mode 100644 index 0000000..93b7791 --- /dev/null +++ b/gpio/LED/README.md @@ -0,0 +1,5 @@ +# LED GPIO + +- The long end of an LED is the Anode (+) and will need a 220ohm-ish resistor in line with the GPIO pin since it will power it via toggling a 3.3v voltage. +- Hook up the Cathode (-), the shorter end, to ground. +- GPIO17 is the 11th pin on the raspberry pi header. diff --git a/gpio/LED/main.go b/gpio/LED/main.go index a9ccfa9..c84675a 100644 --- a/gpio/LED/main.go +++ b/gpio/LED/main.go @@ -11,7 +11,7 @@ import ( func main() { host.Init() - pin := gpioreg.ByName("GPIO11") + pin := gpioreg.ByName("GPIO17") pin.Out(gpio.Low) for { diff --git a/gpio/README.md b/gpio/README.md index 6fed1c5..02aae4f 100644 --- a/gpio/README.md +++ b/gpio/README.md @@ -2,6 +2,44 @@ Ok, we are going to try the golang periph library with this. +## Permissions + +The user running this binary must have `gpio` permissions on the Raspberry Pi. + + +Check if you can read the device without being root + +```bash +head -c 1 /dev/gpiomem > /dev/null && echo "Success" || echo "Access Denied" +``` + +If you get access denied then we can create a gpio user group with udev runs that grant permissions to the /dev/gpiomem device. + +```bash +sudo groupadd gpio +sudo usermod -aG gpio $USER +``` + +Create a udev rules file +```bash +sudo vim /etc/udev/rules.d/99-gpio.rules +``` + +Include the following: +```txt +# Access to the memory-mapped GPIO +SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660" + +# Access to the gpiochip character devices (periph.io uses these) +SUBSYSTEM=="gpio", KERNEL=="gpiochip*", GROUP="gpio", MODE="0660" +``` + + +```bash +sudo udevadm control --reload-rules && sudo udevadm trigger +``` + + ## Periph Library The Periph library is broken up into a number of sub libraries that are imported as needed. This allows the developers to decouple board implementations from drivers and devices. diff --git a/gpio/udev/99-gpio.rules b/gpio/udev/99-gpio.rules new file mode 100644 index 0000000..1646b2d --- /dev/null +++ b/gpio/udev/99-gpio.rules @@ -0,0 +1,7 @@ +# Access to the memory-mapped GPIO +SUBSYSTEM=="bcm2835-gpiomem", KERNEL=="gpiomem", GROUP="gpio", MODE="0660" + +# Access to the gpiochip character devices (periph.io uses these) +SUBSYSTEM=="gpio", KERNEL=="gpiochip*", GROUP="gpio", MODE="0660" + +