Many of the magnetic contact switches I found are composed of two pieces. One side has a magnet in it and another side has a reed sensor in it. The side with the magnet will not have any wires coming out, but the sensor side will have two wires. Normally they are enclosed in a plastic housing that allows users to mount them with screws or press fit into a hole. The side with the wires and the reed switch is the part that will hook up to your microcontroller.
Magnetic door contact switch (from Adafruit)
Magnetic window contact switch (from Amazon)
How does a typical reed switch work?
A reed switch without the enclosure
Inside the switch’s glass casing there are two thin pieces of conductive, magnetized material that are parallel to each other. When the switch is near a magnetic field, the two magnetized contacts will react. Reed switches come in two different varieties which determine how they behave and will affect how you set up your code.
- Normally open (NO)
- By itself the switch is open
- When the switch is near a magnet, the switch closes
- Normally closed (NC)
- By itself the switch is closed
- When the switch is near a magnet, the switch opens
There is likely a point when the magnet is not close enough to fully activate the switch and not far enough to deactivate the switch. This will probably create some noise in your sensor readings so depending on how you use the switch, you may need to integrate debouncing into your code to get the reading you want.
Example Use Case Scenarios
Most of these switches that you find readily available on Adafruit or Amazon are intended for security purposes to check if doors and windows are fully closed. However, you can use them for many other applications. Essentially, these switches are useful for when you have two items/surfaces that can move freely, but you want to sense when they touch or come near each other. Here are some use cases in everyday products:
- Knowing when your laptop lid is closed or not
- Sensing when an electronic device has been placed into a docking station
- Determining whether your refrigerator door is shut
- Sensing water/fluid levels
- Measuring distance in an odometer
Here are some free ideas:
- Count the number of rotations of an object by putting a magnet on the edge of the spinning piece and putting a reed switch in a fixed position near it
- Determine the position of objects with a series of reed sensors on a surface and put a magnet on your objects
- Put magnets on people and create an interactive project that reacts to their proximity/touching to your weird motorized, light sculpture
Pros & Cons
- They are relatively cheap to buy online if you buy them in bulk (see links below to purchase)
- Great for controlling/sensing things that get close but don’t touch
- Great switch for things not attached to each other
- Reed sensors without casings or housings may be fragile and difficult to mount. They are made of glass.
- You may need to add debouncing in your code
- If you buy the ones that already come with plastic casing, it may not have the right form factor for your project
Example Data Sheet
With all sensors you will need to make sure they meet your needs for voltage and current as well as any dimensional requirements for your project. Specific key specs to look at when shopping for these switches are:
- Is it normally open or normally closed?
- At what distance does the switch change its state?
Here is an example of important specs from Adafruit’s magnetic contact switch.
- Normally open reed switch
- ABS enclosure
- Rated current: 100 mA max
- Rated voltage: 200 VDC max
- Distance: 15mm max
- Box size (each side): 29mm x 14mm x 9mm / 1.1" x 0.6" x 0.4"
- Cable Length: 305mm ± 12mm / 12" ± 0.5"
- Weight (per side): 5.4g
How to use with an Arduino Uno (Example from SparkFun)
Wiring your switch
Put one end to ground, and put the other one into a digital pin.
Example circuit schematic from SparkFun
Writing the Arduino code
Where to buy (as of February 2018)
Magnetic Contact Switch
http://a.co/4dWyKak - NC, circular enlosure, $7.59/pack of 5
http://a.co/gXbgtB9 - NO, screw mount, $14.47/pack of 10
https://www.adafruit.com/product/375 - NO, screw mount, $3.95/each