Go GPIO library for Raspberry Pi

Gopher

Been playing a bit with Go-lang lately, it seems like a fun little language. Very minimalistic and clean, yet quite powerful - you can do so many things with the simple constructs they provide (especially the goroutines, channels and type-system). Sadly, it looks like it's pretty slow in benchmarks compared to the other natural alternatives - but it's still early.

Anyway, in an attempt to mix Gophers and Pi, I've made a small native GPIO library for Go on the Raspberry Pi (or the bcm2835 chipset in general). Nothing advanced, but it provides the usual suspects: PinMode (Output, Input), Write, Read, PullUp, PullDown, PullOff, etc. It works by memory-mapping the GPIO addresses in /dev/mem, so it will require root.

To use, and blink a little LED ("hello world" of the electronics/microcontroller realm) just do something like:

import (
	"fmt"
	"github.com/stianeikeland/go-rpio"
	"os"
	"time"
)

func main() {
	if err := rpio.Open(); err != nil {
		fmt.Println(err)
		os.Exit(1)
	}
	defer rpio.Close()

	pin = rpio.Pin(10)
	pin.Output()

	for x := 0; x < 20; x++ {
		pin.Toggle()
		time.Sleep(time.Second)
	}
}

Available over at GitHub. Would be awesome to add PWM, I2C, SPI, etc.. who knows, maybe one day..

Stian Eikeland bio photo

Stian Eikeland

  • 🧑‍💻 Developer
  • ☔ Bergen, Norway

13 comments

  1. Tamás Gulácsi

    Thanks for this nice, clean, usable library!
    Compiling native Go (without WiredPi) is especially delighting!

  2. Loran Briggs

    thanks for the library, but I cant seem to get this to work. I ran "go get ..." wrote the same program you have here. When I go to run nothing happens. I have been able to get other go programs running on the rpi, but no gpio ones. Any tips? thanks.

    1. Loran Briggs

      I also tried running it as sudo as you mentioned.

  3. Hishah

    Excellent sample LED sample works for me, but cannot figure out how to read my DHT11 Temp sensor

  4. BloomT1990

    Does this support Raspberry PI zero?

    1. Stian Eikeland

      Theoretically yes, but I don't have any PI zeros to test.

      1. Curiositry

        I’ve used it on several Pi Zeros: it works practice as well as in theory.

  5. John Wick

    Will it run on banana pi?

    1. Stian Eikeland

      I don't own any banana pis so not sure.

      Gut feeling says probably no, banana pi uses an Allwinner chip, while RPi uses a Broadcom. I expect them to use different memory adresses for GPIO.

      1. John Wick

        Well I tried running it on banana pi, its not working. It worked easily with my Raspberry Pi 3. But I need it on my Banana Pi, is there any alternate library for Banana Pi.

  6. Daniel Machnik

    Hey,

    examples/event/event.go
    sometimes recognizes two clicks, although I hit only once

    i'm on Raspberry PI zero w

    1. Stian Eikeland

      You got a physical push button hooked up to a pin, and sometimes get two clicks in software when pushing the button?

      Buttons sometimes "bounce" just as the metal parts inside the button make contact, and you usually have to work around this either in software or in hardware.

      To solve it in hardware you can add a small capacitor and resistor.

      https://hackaday.com/2015/1...

      1. Daniel Machnik

        Thanks for the fast answer.
        Yes.
        Sounds good. I try to fix my hardware soon.