源码
/*
A blinker example using go-rpio library.
Requires administrator rights to run
Toggles a LED on physical pin 19 (mcu pin 10)
Connect a LED with resistor from pin 19 to ground.
*/
package main
import (
"fmt"
"github.com/stianeikeland/go-rpio/v4"
"os"
"time"
)
var (
// Use mcu pin 10, corresponds to physical pin 19 on the pi
pin = rpio.Pin(21)
)
func main() {
// Open and map memory to access gpio, check for errors
if err := rpio.Open(); err != nil {
fmt.Println(err)
os.Exit(1)
}
// Unmap gpio memory when done
defer rpio.Close()
// Set pin to output mode
pin.Output()
// Toggle pin 20 times
for x := 0; x < 20; x++ {
pin.Toggle()
time.Sleep(time.Second / 5)
}
}
树莓派操作
415 mkdir my
416 mv gpio.go my
417 cd my/
418 go mod init hello
419 go mod tidy
420 go build gpio.go
421 ./gpio
效果:GND-GND LED+连接GPIO21 也就是最边上的一对儿
一闪一闪