Simple circuits: Reading in an “on/off” magnetic sensor

The m5stick comes with a lot of “sensors” – the simplest of which is the ‘A’ and ‘B’ buttons. These buttons “sense” when you press them by connecting the circuit between two “pins” on the microchip inside the orange casing. There are more complex sensors embedded in there as well – a temperature sensor, an acceleration sensor (accelerometer), gyroscope, etc. etc., and when you tap into the world of sensors you can measure *anything* with your system.

Your shadkits come with a couple of simple but powerful sensors. Exhibit A:

A magnetic “reed” switch – senses the presence of a magnet, by having the two “reeds” touch each other, completing the circuit across the pins.
How to Connect Reed Switch with Arduino Reed Switch Working
From electronicshub.org

Note: The reed switch you have is in a glass package. Please be careful, and do not bend the two metal leads too close to the glass. If you must bend them, use pliers and bend them far away from the glass, or simply connect them by plugging the ends into socket wires, as follows:

Next – how do we get the m5stick to know what the reed switch is doing? Load this simple program onto your devices – you can download reed.m5f or just code the following:

This is “initializing” pin 26, which you can physically find labelled G26 in the top row of black sockets on the m5stick:

Initializing means setting the pin up to do what we want. In this case, the init statement is:

  • labelling it as “pin0” – in more complex programs we can use pin0 or pin1 or pin2 etc, which is just a lot easier to remember than “G26”
  • Setting it to be an INput pin – it will take signals IN and read off their values. All “sensors” would connect to “input” pins.
  • The pin is also being set as “Pull Up”, which means that if there is nothing connected, then the default value will be pulled “up” to it’s highest level.

If the pin is normally going to pulled up HIGH (value of 1), then the only way to make a reading is to force it down LOW, by connecting it to a low voltage. In circuit terms, this low voltage is called “ground” or GND. So if pin G26 is connected to GND, it will read 0, but if it is DISCONNECTED from GND, it will read 1. Go ahead and try it by physically connecting G26 to GND with a wire!

You should see the label on the screen reading 0 when it is connected, and 1 when disconnected. Now replace that wire with the magnetically activatable reed switch:

Bring a magnet close to the reed switch, and see if the sensor reading changes.

Although this seems simple, this is the basis for most home alarm systems, and bicycle speed measurement systems. Opening a window moves a magnet away from the reed switch, the pin goes HIGH, and the alarm goes off. On a bicycle, a magnet mounted to the wheel passes by to activate the reed switch, and the stick “counts” the number of switch activations per second, and uses that to calculate how fast the wheel is turning.

Challenge questions!

  • Make the stick LED turn on whenever a magnet is close by
  • Sound a buzzer alarm and change the screen colour when a magnet is removed (as in a burglar alarm)
  • Mount the magnet to a box and protect your stuff!
  • With wifi-enabled m5sticks, you can even have the stick send you an email or text message when your box is opened. This wizardry requires an external service – the m5stick sends an MQTT message to the external service, which then performs the action. There are several such services available online.