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.
We will be using HC-SR501 sensor which is a very common type. (At bottom of the Guide we also test unknown Infrared sensor and ST-00082 Mini)

Specifications:
| Operating Voltage (VDC) | 4.5 ~ 20 V |
|---|---|
| Average Current Consumption | 0.06 mA |
| Distance Measuring Range (CM) | 300 ~ 700 |
| Output Type | (High/ Low-level Signal) 3.3V TTL output |
| Delay Time | 5 to 200s (Can be Adjusted, Default 5s +/- 3%) |
Back side of my sensor was like this:
Where I put H & L there is usually a jumper to be able to configure it as either H (Repeat trigger mode) or L (Single trigger mode). This one I have right now has no jumper and if peering eyes on the board then it seems to be permanently set to H (Repeat trigger mode).
Sensitivity adjusts the trigger range. (Turn clockwise to increase range)
Time delay adjustment is for adjusting the detection delay (0.3 seconds to 600 seconds): Turn the potentiometer clockwise to increase delay.
Pieces we use:
First thing is connecting the sensor:
Make sure that your Raspberry PI is not turned on when your wiring it up.
Wiring of the HC-SR501 - (This is older picture from last time I did this, notice this one does actually have the jumper which the one I have now does not have)
Note that when shown on the breadboard the sensor is shown from the other side, so check the picture above to make sure the wires go in right place.
(Inspect your sensor carefully for markings, I have seen sensor of this type that had the pins in different order, the sensor I used last time had no markings on it so I just had to know the pin order, if you have such case then you can use the picture a bit above to go by).
Raspberry Pi pin layoutIf 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, "watch-line-value")
lineRequest.RequestReadEdgeEventsAsync(10, 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.RISING
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.
After this I proceeded to test another Infrared sensor I had.
For this one I had no name at all for. So no specifications at all.
Warning do not connect this one directly to the GPIO pin.
Not knowing its specs then I had to measure it up with voltage meter and I found that the out pin was putting out 4.4V which is enough to damage your Raspberry Pi.
To solve this then I had to set up voltage divider.
Where 300 Ω resistor went from Ground to Point X and 200 Ω Resistor went from Out on the sensor to point X. And then point X to the GPIO pin.
This gave me voltage of just about 2.4V to the GPIO pin.
Click bellow to learn about voltage dividers.
Voltage dividersOne more sensor is ST-00082 Mini.
This one is drop in replacement for the HC-SR501 sensor.
Summarize for the different sensors:
Same code example works for all three sensors, but special care is needed when wiring the unknown sensor.
| Sensor | Input voltage | Output voltage | Needs Voltage divider |
|---|---|---|---|
| HC-SR501 | 5V | 3,3V | No |
| Unknown Mini sensor | 5V | 4,4V | Yes |
| ST-00082 Mini | 5V | 3,3V | No |