Go on Raspberry Pi GPIO

golang raspberry pi gpio
Symptom First action Planning use
The program only works with sudo Run it as root for the first test. For normal use, add a gpio group, add your user to it and set up udev rules so /dev/gpiomem is usable. Root is fine on the bench. Plan the group and udev setup before the program runs unattended.
The wrong pin blinks, or nothing does Use BCM numbers in code. BCM 10 is physical pin 19 on the header. Write the BCM number beside each header pin you plan to use before you write any code.
The LED is lit when the code writes LOW Check which LED leg goes to the GPIO pin. With the negative terminal on the pin, LOW lets current flow. Choose active-high or active-low wiring first, then keep the code consistent with it.
The code runs but the LED stays dark Check the LED polarity and that a resistor sits in series. One tutorial uses a 220 Ohm resistor per LED. Start from that resistor value and change it only if you have a reason.

The first row follows the go-rpio package documentation. The wiring rows follow a Raspberry Pi LED bar graph tutorial.

Yes, you can drive golang raspberry pi gpio pins with the go-rpio package, which toggles a pin to blink an LED. It needs root, or a gpio group with udev rules, because it memory-maps the GPIO registers.

The walkthrough below covers library choice, wiring, the program, running it and fixing the usual faults. If you are new to the shell commands used along the way, our linux command line tutorials cover the basics.

Choosing a Go GPIO library

Go does not need a wrapper around Python or shell tools to reach the pins. Community packages talk to the hardware directly, and the two that matter here take different approaches.

  • go-rpio: the package documentation says it works by memory-mapping the bcm2835 gpio range. That is why it needs root unless /dev/gpiomem is used. Its example opens the library, sets a pin to output and calls Toggle in a loop with a sleep of one second.
  • go-rpigpio: the project page describes it as an extremely simple Go package. You open a pin with rpi.OpenPin(2, rpi.OUT), check the error, then write HIGH or LOW.

This walkthrough uses go-rpio. Its toggle-and-sleep example is already a blink program, so there is little to adapt. Choose go-rpigpio if you prefer an explicit open-then-write style.

Newer boards and Raspberry Pi OS

Check your board before committing to a library. A March 2024 forum thread notes that the Raspberry Pi 5 has a new RP1 chip handling the GPIO interfaces. One user there found that the RPi.GPIO package on Bookworm failed with a peripheral base address error, and rpi-lgpio was suggested instead. Both are Python packages, not Go.

The lesson carries over to Go. A library written around the bcm2835 address range was designed for earlier boards. On a Pi 5, get the blink working before you build anything larger on it. On an older board, go-rpio’s documented behaviour applies as described.

Wiring the LED and choosing a pin

The circuit is one GPIO pin, one resistor and one LED. The resistor limits current through the LED, so never connect an LED straight to a pin. The tutorial linked above uses a 220 Ohm resistor for each LED. That is a sensible starting value for a single indicator LED.

Pick the pin by its BCM number

go-rpio uses BCM numbers, not positions along the header. The documentation’s own example makes the difference clear. Pin 10 in code is exposed on the header as physical pin 19. If you wire to the header position you counted and then type that number into the code, you will drive a different pin from the one you wired.

Choose active-high or active-low

The usual arrangement runs from the pin through the resistor and the LED to ground, so writing HIGH lights the LED. The LED bar graph tutorial does the opposite. It connects each GPIO pin to the LED’s negative terminal, so the pins must be set LOW for current to flow. Both work. Pick one before you write the code, because the blink loop looks identical and only the starting state differs.

The same tutorial advises a 40-pin cable with a T-type adapter between the Pi and the breadboard. It says this makes wiring easier and helps prevent damage to the GPIO pins. It is worth having if you plan to move jumper wires often.

Each red LED on this trainer board is paired with its own resistor, the same LED-plus-resistor pairing the blink circuit needs. This is a different board, not a Raspberry Pi build.

Writing the blink program

Create a folder, run go mod init in it and fetch the package with go get github.com/stianeikeland/go-rpio. You can edit the file in any terminal editor. Our nano vs vim comparison helps you choose one if you are editing over SSH.

The screen shows PHP and HTML-style code lines; it is not Go code and does not show an SSH session.

Put this in main.go. It follows the documented pattern: open the library, make the pin an output, then toggle with a one-second sleep.

package main
import (
“log”
“time”
“github.com/stianeikeland/go-rpio”
)
func main() {
if err := rpio.Open(); err != nil {
log.Fatal(err)
}
defer rpio.Close()
pin := rpio.Pin(10)
pin.Output()
for {
pin.Toggle()
time.Sleep(time.Second)
}
}

A laptop screen displays lines of colored source code; no Raspberry Pi or Go code is visible.

The pin number is the BCM number discussed above, so change it to match your wiring. If rpio.Open returns an error, the cause is almost always permissions, covered below.

Source code on a screen with line numbers at the left edge. A blink program is short enough to review in one screen before you copy it to the Pi.

Building and running on the Pi

You can run go build on the Pi itself, or build on a laptop and copy the binary across. The laptop route is faster on small boards, but the binary must target the Pi’s processor. Our guide to how to cross compile go for arm explains the settings. Either way, start the program from an SSH session on the Pi, since the LED is the output you are watching.

Run it with sudo for the first test. If the LED blinks once per second, the wiring, pin number and library all work. Stop it with Ctrl+C.

Fixing permission errors

The symptom is an error from rpio.Open, or a program that works only under sudo. The cause is that go-rpio memory-maps the GPIO range, so, in the package’s words, it requires root or administrative rights. An older Go GPIO library write-up from 2013 describes the same /dev/mem mapping and the same need for root. That behaviour is not new.

To run without root, the go-rpio documentation says you must use /dev/gpiomem, add a gpio group, add your user to that group and set up udev rules. Do this once, log out and back in so the group membership applies, then retest without sudo.

  • Bench testing: sudo is acceptable while you prove the circuit.
  • Anything left running: use the group and udev route so the program does not hold more privilege than it needs.
  • Older boards: the older Pi tutorial linked earlier notes that the board supports user-level GPIO access, but its example uses a different library. Do not assume go-rpio behaves the same way without the group setup.

Fixing wiring and logic faults

If the program runs without errors but the LED does not behave, work from the code outward.

  • Nothing lights: confirm the LED is the right way round and that the resistor is in series. Then confirm the BCM number in code matches the physical pin you wired, using the header mapping above.
  • Lit constantly, dark during the loop: your LED is probably wired active-low. The blink is still happening, only inverted, so there is nothing to fix in the loop.
  • A different LED or pin flickers: you have used the header position instead of the BCM number. Recheck the pin mapping in the documentation.

If the pin toggles correctly in code and the circuit checks out, swap in a second LED and resistor before suspecting the library. A dead LED is far more common than a library fault.

Frequently asked questions

Question Answer
Can I make the LED blink faster or slower? Yes. The example sleeps for time.Second between toggles. Change that duration in the loop to alter the blink rate.
Do I need a header adapter? No, but the LED bar graph tutorial recommends a 40-pin cable with a T-type adapter to make breadboard wiring easier and to help prevent damage to the GPIO pins.
Do old Go GPIO tutorials still apply? Their root requirement does. Libraries that memory-map /dev/mem or the GPIO range need root, as a 2013 write-up and the current go-rpio documentation both say. Check the library’s own page for the newest setup instructions.
Is go-rpigpio easier than go-rpio? Its project page calls it extremely simple. You open a pin with rpi.OpenPin and write HIGH or LOW, but go-rpio’s documented example is already a blink loop.