The final installment of November's micro:bit peripherals in Python series is the easy-to-implement magnetic switch. These are the same little sensors you might find on doors and windows in a home security system. Each sensor is two parts. One is a magnet and the other is a switch that activates when a magnet is near. In our fourth installment of the peripherals in Python series we worked through switches and buttons. The implementation is no different here. After we finish the magnetic switch we will dive into another feature of the micro:bit that allows you to create the same effect with just a magnet and no external sensor.
Check out the demo video at: https://youtu.be/ks6JoeQ71Zg
HOW DOES A MAGNETIC SWITCH WORK?
Nearly all door and window sensors work effectively the same way. They use a component called a “reed switch” to determine if the door is open or closed. A reed switch is a device that activates (closes) when a magnetic field gets close. Inside the device, there are two contacts. One of the contacts gets pulled toward the other when a magnet is near. This closes the connection and sets off your home alarm. The two images below show this effect:
That is how a window or door sensor works. Now let’s connect it to a micro:bit!
SETTING UP THE DEVICE:
The magnetic switch has two parts. The magnet and the reed switch. The reed switch has two connection points. In this example we’ll be using the input pin in PULL_DOWN mode, so one side of the switch gets hooked to the micro:bit’s 3V pin and the other gets connected to the input pin. In the switches and buttons tutorial, we showed how you could alternately use the GND pin and a PULL_UP on the input pin. The magnetic switch works the same way.
CODE TO TRIGGER THE ALARM:
The following code will be just a clear screen when the magnet is close (when the window is closed) and will display an “A” for alarm when the magnet is removed (when the window is open).
USING THE MICRO:BIT’S COMPASS:
From the code above you see how simple it is to incorporate a magnetic switch into your next project! But what if you don’t have a magnetic switch… just a magnet? You’re in luck! The micro:bit has a built-in sensor to detect magnetic fields. You can write code to replace the function of the separate reed switch by detecting the strength of the magnetic field, and using that to trigger the “switch” action. We will need to work with the micro:bit’s compass library to utilize this feature.
To setup your micro:bit just disconnect all external pins leaving only the USB connection. If you’ve been through JumpStart Python, this code will be very familiar to you. It’s part of the Alarm System project. In that project you build a wireless alarm system, with multiple sensors communicating with a central Annunciator device.
The micro:bit can sense Earth’s magnetic field. It really can act as a compass! But that’s for a different project… Even a small magnet will create a much more powerful field in the vicinity of the micro:bit when you bring it close. When your code starts up, the first step is to get a baseline for the magnetic field without the magnet nearby:
baseline = compass.get_field_strength()
That reading is an integer value in units of nano-Tesla. A Tesla is a unit of measurement that represents the amount of magnetic induction (flux). We will now compare all future readings of the magnetic field to that baseline.
The next step is to enter a loop and determine whether the field strength has increased more than 10,000 nano-Tesla. 10,000 is a rough estimate to detect a magnet within a few inches. You can refine the strength of your sensor by adjusting the number to something other than 10,000. Here is the full snippet of code for our window alarm:
Now, go forth and experiment with the magnetic field around you! Be sure to change the 10,000 value and see how sensitive your sensor can be.