In this example we take a look at the KY-021 Mini Reed Switch Module and connect this to a Raspberry Pi.
Lets speak about the reed switch – from wikipedia
Reed Switch Information
The reed switch contains a pair (or more) of magnetizable, flexible, metal reeds whose end portions are separated by a small gap when the switch is open. The reeds are hermetically sealed in opposite ends of a tubular glass envelope.
A magnetic field (from an electromagnet or a permanent magnet) will cause the reeds to attract each other, thus completing an electrical circuit. The spring force of the reeds causes them to separate, and open the circuit, when the magnetic field ceases. Another configuration contains a non-ferromagnetic normally-closed contact that opens when the ferromagnetic normally-open contact closes.
A thin layer of non-ferromagnetic material is applied to the reed switch contact area to serve as an electrical contact switching (wear) surface and, for normally-open contacts, as a magnetic spacer whose thickness is important in controlling the magnetic field level at which the contact opens (the drop-out).
Reed switch contacts are typically Rh, Ru, Ir, or W. There are also versions of reed switches with mercury “wetted” contacts. Such switches must be mounted in a particular orientation. Otherwise drops of mercury may bridge the contacts even when not activated.
Since the contacts of the reed switch are sealed away from the atmosphere, they are protected against atmospheric corrosion. The hermetic sealing of a reed switch make them suitable for use in explosive atmospheres where tiny sparks from conventional switches would constitute a hazard.
One important quality of the switch is its sensitivity, the amount of magnetic field necessary to actuate it. Sensitivity is measured in units of Ampere-turns (AT), corresponding to the current in a test coil multiplied by the number of turns in the test coil. Typical pull-in sensitivities for commercial devices are in the 10 to 60 AT range.
The lower the AT, the more sensitive the reed switch. Also, smaller reed switches, which have smaller parts, are more sensitive to magnetic fields, so the smaller the reed switch’s glass envelope is, the more sensitive it is.
In production, a metal reed is inserted in each end of a glass tube and the end of the tube heated so that it seals around a shank portion on the reed. Green-colored Infrared-absorbing glass is frequently used, so an infrared heat source can concentrate the heat in the small sealing zone of the glass tube.
The thermal coefficient of expansion of the glass material and metal parts must be similar to prevent breaking the glass-to-metal seal. The glass used must have a high electrical resistance and must not contain volatile components such as lead oxide and fluorides which can contaminate the contacts during the sealing operation.
The leads of the switch must be handled carefully to prevent breaking the glass envelope. The glass envelope can be damaged if the reed switch is subjected to mechanical stress.
|Raspberry Pi 4||Aliexpress product link|
|37 sensor kit||AliExpress Product link|
|connecting wire||Aliexpress product link|
We use pin 40 (GPIO 21) in this example, you can use another GPIO pin.
|# Needed modules will be imported and configured|
|import RPi.GPIO as GPIO|
|# The input pin which is connected with the sensor|
|GPIO_PIN = 21|
|GPIO.setup(GPIO_PIN, GPIO.IN, pull_up_down = GPIO.PUD_DOWN)|
|print "KY-021 Sensor Test [press ctrl+c to end the test]"|
|print("Sensor is blocked")|
|# signal detection (raising edge).|
|GPIO.add_event_detect(GPIO_PIN, GPIO.RISING, callback=outputFunction, bouncetime=100)|
|# Main program loop|
|# Scavenging work after the end of the program|