Movatterモバイル変換


[0]ホーム

URL:


Motion detector

Detect motion without polling
Edit this page

Overview

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.

Learn more

Example

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.

Buying

The periph authors do not endorse any specific seller. These are only providedfor your convenience.


← MF-RC522 |SSD1306 →


[8]ページ先頭

©2009-2025 Movatter.jp