Reed switch

In this example we are going to connect the Raspberry Pi or alternate board to Reed switch.
Reed switches are a special kind of electrical switch, actuated (turned on and off or change over) by magnetism.
I will be using KY-021 brake-out board. Though it is such a simple board that you can as well use just Reed switch. Only components on the board are the Reed switch and a 10kΩ resistor.

A Reed switch is sort of like hall sensor where the difference is that Reed switch consumes no power at all while idling and waiting for magnet to turn things on while hall sensor consumes power all the time. But hall sensor is not mechanical and can therefor measure much faster making it good for all sorts of time sensitive measures.

Reed switch on KY-021 brake-out board

DISCLAIMER

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:

When there is no magnetic field near the sensor then the output will put out HIGH, while if there is magnetic field near the sensor then the output will output LOW. (You can test with magnet).

Wiring:

Warning

This sensor is just a switch so what you put in comes out. We have to connect this to the 3.3V on the Raspberry Pi and not the 5.5V, else the output can damage the Raspberry Pi pin.


Spec on this brake-out board sensor is:

If you are not familiar with working with a breadboard or on how to place a cobbler on the breadboard then click here bellow

Breadboard


Note the picture is showing a vibration sensor since I did not have Fritzing part for the SW520D, but their wiring is 100% the same.
Raspberry Pi pin layout

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 guide

Raspberry 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, "reed-switch")
    
    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.DebouncePeriod = 10000
  
  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

Getting the code

You can find this example code at Example code for GPIO guides.


Reed switch

In this example we are going to connect the Raspberry Pi or alternate board to Reed switch.
Reed switches are a special kind of electrical switch, actuated (turned on and off or change over) by magnetism.
I will be using KY-021 brake-out board. Though it is such a simple board that you can as well use just Reed switch. Only components on the board are the Reed switch and a 10kΩ resistor.

A Reed switch is sort of like hall sensor where the difference is that Reed switch consumes no power at all while idling and waiting for magnet to turn things on while hall sensor consumes power all the time. But hall sensor is not mechanical and can therefor measure much faster making it good for all sorts of time sensitive measures.

Reed switch on KY-021 brake-out board

DISCLAIMER

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:

When there is no magnetic field near the sensor then the output will put out HIGH, while if there is magnetic field near the sensor then the output will output LOW. (You can test with magnet).

Wiring:

Warning

This sensor is just a switch so what you put in comes out. We have to connect this to the 3.3V on the Raspberry Pi and not the 5.5V, else the output can damage the Raspberry Pi pin.


Spec on this brake-out board sensor is:

If you are not familiar with working with a breadboard or on how to place a cobbler on the breadboard then click here bellow

Breadboard


Note the picture is showing a vibration sensor since I did not have Fritzing part for the SW520D, but their wiring is 100% the same.
Raspberry Pi pin layout

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 guide

Raspberry 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, "reed-switch")
    
    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.DebouncePeriod = 10000
  
  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

Getting the code

You can find this example code at Example code for GPIO guides.