Introduction: Spy-tech : Intruder Detection & Logging System
You are the chief scientist of the research and development division of the Meow Secret Service. One fine day the top agent in your agency comes seeking help & he is looking desperate.
"I need something fast. I think someone has been entering my safe house while I am out on a mission. Is there a way to discreetly detect if anyone has broken into my premises through the main door? In other words, I need a silent tripwire system."
Opening your drawer, you found your trustee Micro:bit & a piece of magnet.
In the 1962 film Dr. No, James Bond placed a hair across his door, which would fall off as soon as somebody opened it. I am pretty sure my readers are not that old. In more recent memory, the main character in Death Note set a strip of paper on the opening of the door and a mechanical pencil lead on the hinge to determine if someone has snuck into his room. We are going to create a rather similar system but with a modern twist. Thanks to the versatility of the Micro:bit with its wide range of onboard sensors & connectivity options, many projects could be created without much additional components.
- 1 x Micro:bit board (works for both v1 and v2)
- 1 x Micro-B USB Cable
- 1 x AA or AAA battery holder
- 2 x AA or AAA batteries
- 1 x Magnet (or any fridge magnet)
- 3 x Blu-Tacks
- 1 x Ruler
Step 1: Let's Get Started !
To create the gizmo for our Meow Secret agent, we will be using the onboard electronics compass (or built-in magnetometer as we will understand shortly). The normal use case for Micro:bit's on-board compass is to detect the earth's magnetic field a.k.a A Compass (duh).
This magnetic field around the globe (https://en.wikipedia.org/wiki/Earth%27s_magnetic_field) is created by strong charges from earth's north & the south poles.
If you have never experienced making a digital compass with your Micro:bit, do check out the following tutorial (https://www.instructables.com/MicroBit-Compass-DIY/) . In this above project/example, the Micro:bit’s compass senses the earth’s magnetic field & display which the direction your Micro:bit is currently facing.
Step 2: Measuring Magnetic Fields
Knowing that the on-board compass is so sensitive that it can even sense the slight magnetic field around us (You don't see metal objects flying around you. Right? Well, unless you have a sudden visit by Magneto from the X-men comic series). We will know that Micro:bit is more than eligible to fulfill the task of measuring the strength of a nearby magnet!
Step 2.1: Open the following MakeCode link with your Chrome (or any WebUSB compatible web browser) for the code: https://makecode.microbit.org/_giRPKb570Yy1
The code does two main operations.
(A) It takes the combined magnetic field detected in all three axis
(B) It sends the reading through the USB (UART) back to your computer.
Step 2.2: Download the code to your attached Micro:bit
Step 2.3: Once the program is loaded & running on our Micro:bit, click on the "Show Console Device"
** To avoid any possible damage to your Micro:bit or it's on-board compass, please do not touch your Micro:bit with the magnet **
Step 2.4: Gradually move your piece of magnet closer & closer to your Micro:bit & note the changes in the value (& graph).
[You can refer to the image above]
Step 2.5: Take note of the magnetic force value when there is no magnet
Step 2.6: Take note of the magnetic force value when there is magnet near our Micro:bit.
(If you change the direction of the magnet, the value will change too.)
Step 2.6 (Bonus): You can jot down the correlation between the distance of Micro:bit & the piece of magnet on a piece of paper.
Step 3: A Simple Door Opening Detection / Trip Wire System
We will now build a simple tripwire system using our Micro:bit with the piece of magnet. The logic of the tripwire is rather simple, our Micro:bit will continuously read the magnetic field around itself. And we will increase the count (DoorOpened) by one whenever the magnet is pulled away, passing the threshold set in our simple MakeCode program. (100 is a sweet number that will work in most cases, but you are free to adjust it to increase the sensitivity). It is really important for our Micro:bit to only add the count when it registers a change in magnetic field strength, passing the threshold (Magnet close-by to pulled further apart). We create a variable to be used as a door opened flag (DoorOpenFlag).
The following flow-diagram will better illustrate how the program works.
Step 4: Putting the Codes Together
Step 4.1: Create the codes as illustrated in the flow chart. Or, open the following MakeCode link with your browser directly: https://makecode.microbit.org/_iXd3Xtbtt5aw
Step 4.2: Place a piece of magnet close to your Micro:bit
Step 4.3: Download the code to your attached Micro:bit (You may need to adjust the threshold of 100 based on the result in Step 2.5)
Step 4.4: Once the program is loaded & running on our Micro:bit, move the magnet away & back to close proximity to the Micro:bit a number of times.
Step 4.5: Press Button A on your Micro:bit to display the number of times "Door" has been opened since our Micro:bit starts running the program.
Step 5: Installing Your Sensors to the Door
Step 5.1: Unplug your Micro:bit from the USB cable & attach the battery holder to your Micro:bit
Step 5.2: Attach the Micro:bit & the battery holder to the Door-frame using Blu-Tack, with another piece of Blu-Tack, attach the magnet on the door maintaining close proximity between the magnet & the Micro:bit on the door frame. [As illustrated]
Step 5.3: Leave the gizmo running for a few days, and you can press on the button A whenever you need to see the sum of Door opening activity "seen" by our Micro:bit. You can either press the reset button on the bottom side of the Micro:bit (which might be rather hard to reach since it is attached to the door) or press Button B to reset the count.
Step 6: Retrieving the Count Wirelessly & Discreetly Like a Super Spy
Sometimes it might be rather hard to press the button on your Micro:bit to retrieve the count, possibly because the gizmos is set up in the enemy territory during your espionage mission, the gizmo is installed so discreetly within your door hidden from view (& touch) or simply because we attached it too high up, beyond your reach.
We are going to utilize the Bluetooth connectivity capability of Micro:bit to remotely beam the information to our smartphone. On our smartphone, we will require a decent android phone or iPhone with Bluetooth BLE capability.
Step 7: Adding the Codes to Communicate Wirelessly
Step 7.1: We are going to add Bluetooth capability to our code from Step 4.1. Add the Bluetooth extension within our MakeCode project by clicking on the Gear & choosing "Extension"
Step 7.2: Search for "Bluetooth" and choose the first option that presented itself. (This will effectively replace the Radio extension, just answer "Yes".)
Step 7.3: Add in two Bluetooth low energy (BLE) services offered for our Micro:bit in the on start block
Step 8: Retrieving the Information Using Our Phone
Step 8.1: Download the smartphone app for your Micro:bit & follow the pairing guide as documented in the official user guide (Go through the section under "MakeCode apps" only & disregard the portion under "Swift Playgrounds")
Step 8.2: Once the Micro:bit is successfully paired with your smartphone + app, choose "Monitor and Control" from the main menu.
Step 8.3: Hit "Start" at the bottom right corner of the screen.
Step 8.4: Using the virtual on-screen Micro:bit, you will be able to do everything you were doing with your Micro:bit wirelessly.
Step 9: Result
Step 10: Adding an Enclosure to Your Solution
Instead of having a bare Micro:bit board sticking out like a sore thumb on your door frame (You do not want your spy tech to be noticed, do you?), why don’t we add a 3D printed enclosure to your Micro:bit. Furthermore, you can choose a 3D filament to match the color of your door frame. You can check out this beautiful enclosure created by Chaw here: https://www.thingiverse.com/thing:4704248.
As for the magnet, you can simply choose a fancy one from any local souvenir stores =)
Step 11: Virtual Classroom
This instructable is created for teachers to have fun with the students by building a simple Micro:bit project with a magnet.
Photos are the throwbacks of our class conducted by our local teachers here via Zoom.