
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..
13 comments
Thanks for this nice, clean, usable library!
Compiling native Go (without WiredPi) is especially delighting!
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.
I also tried running it as sudo as you mentioned.
Excellent sample LED sample works for me, but cannot figure out how to read my DHT11 Temp sensor
Does this support Raspberry PI zero?
Theoretically yes, but I don't have any PI zeros to test.
I’ve used it on several Pi Zeros: it works practice as well as in theory.
Will it run on banana pi?
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.
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.
Hey,
examples/event/event.go
sometimes recognizes two clicks, although I hit only once
i'm on Raspberry PI zero w
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...
Thanks for the fast answer.
Yes.
Sounds good. I try to fix my hardware soon.