GPIO pins can be leveraged to detect motion via a PIR(Passive/Pyroelectric InfraRed sensor), similar to readingbutton presses.
Thegpio.PinIn.WaitForEdge()function permits a edge detection without a busy loop.
packagemainimport("fmt""log""periph.io/x/conn/v3/gpio""periph.io/x/conn/v3/gpio/gpioreg""periph.io/x/host/v3")funcmain(){// Load all the drivers:if_,err:=host.Init();err!=nil{log.Fatal(err)}// Lookup a pin by its number:p,err:=gpioreg.ByName("16")iferr!=nil{log.Fatal(err)}fmt.Printf("%s: %s\n",p,p.Function())// Set it as input.iferr=p.In(gpio.PullNoChange,gpio.RisingEdge);err!=nil{log.Fatal(err)}// Wait for edges as detected by the hardware.for{p.WaitForEdge(-1)ifp.Read()==gpio.High{fmt.Printf("You moved!\n")}}}
This example uses basically no CPU: theWaitForEdge() leverages the edgedetection provided by the kernel, unlike other Go hardware libraries.
The periph authors do not endorse any specific seller. These are only providedfor your convenience.