We do not take any responsibility for possible errors in the guide or errors that you might do wiring it up. Incorrect wiring can result in damaged sensor or damaged Raspberry Pi.
Pieces we use:
How this sensor works:
This sensor is basically a mechanical switch with metal balls inside. When the housing gets tilted the balls will close the circuit as shown on the picture above.
(Note that the signal on the brake-out board we use is reversed from what you might think from how the mechanical switch works, so the board will return Digital LOW when the switch is ON and Digital HIGH when the switch is Off.
Variations:
The sensor it self can come in different housings, some of whic hcan easily be confused with a capacitor.
The bakeout boards exist with one or two sensors on them, when there are two then you get two digital output channels.
Wiring:
If you have two channel sensor board and wish to use both channels then wired up the 2nd digital output to other GPIO pin.
When measuring up this sensor then it shows the digital output will have same voltage as the input. Meaning you have to connect 3.3 V to the input when using with the Raspberry Pi.
The potentiometer on the board allows you to adjust the sensitivity of the DO output.
The board has PWR_LED and a DO_LED.
The sensor will have the PWR_LED showing that it is turned on.
The DO_LED lights up when the circuit it closed. (Sensor is standing upwards).
The sensor puts out digital HIGH when circuit is not closed, and digital LOW when circuit is closed.
Spec on this sensor is:
If you need more information about the GPIO headers on Raspberry Pi or other boards then click bellow.
Raspberry Pi Pin guideOrange Pi One Pin guideOrange Pi Pin guide for most H3 modelsOrange Pi Zero 3 Pin guideOrange Pi 5 Plus Pin guideRock 64 Pin guideRaspberry Pi pin layout
The Xojo Code
Private mLineRequest As Gpiod.LineRequest
And then Opening event for the Window:
Sub Opening() Handles Opening
try
using Gpiod
var chip as Chip = Chip.Open("/dev/gpiochip0")
var lineRequest as LineRequest = RequestInputLine(chip, 5, "tilt-sensor")
lineRequest.RequestReadEdgeEventsAsync(5, true, AddressOf LineMonitorCallback)
catch e as GpiodException
MessageBox(e.Message)
end try
End Sub
Helper function to set up the line
Public Function RequestInputLine(chipHandle as Gpiod.Chip, lineOffset as UInt32, consumer as String) As Gpiod.LineRequest
using Gpiod
// Exceptions will be caught by the caller method
var reqCfg as RequestConfig = nil
var setting as new LineSetting()
setting.Direction = Gpiod.Direction.INPUT
setting.EdgeDetection = Gpiod.LineEdge.FALLING
setting.Bias = Gpiod.LineBias.PULL_DOWN
var lineCfg as new LineConfig()
lineCfg.AddLineSetting(array(lineOffset),setting)
if consumer <> "" then
reqCfg = new RequestConfig()
reqCfg.Consumer = consumer
end if
return chipHandle.RequestLines(reqCfg, lineCfg)
End Function
Callback for our events:
Public Sub LineMonitorCallback(edgeEvent as Gpiod.EdgeEvent)
lstEvents.AddRow(if(edgeEvent.EventType = Gpiod.EdgeEventType.RISING_EDGE, "Rising", "Falling"))
lstEvents.CellTextAt(lstEvents.LastRowIndex, 1) = edgeEvent.LineOffset.ToString()
lstEvents.CellTextAt(lstEvents.LastRowIndex, 2) = edgeEvent.LineSequenceNumber.ToString
lstEvents.CellTextAt(lstEvents.LastRowIndex, 3) = edgeEvent.TimestampNs.ToString()
End Sub
You can find this example code at Example code for GPIO guides.