A reed switch is a switch that is actuated by the presence of a magnetic field. When you bring a magnet close to the switch, you do the equivalent of pressing a button. Depending on the exact reed switch you are using, the switch is either opened or closed when the magnet is introduced.
A reed switch can be used just like you would a button, making it quite a simple component to work with.
Here are the basic connections you need to use the reed switch,
The resistor in the image is a 10KOhm pulldown resistor.
This was my test version, without and then with the magnet near to the reed switch.
Your basic button code is going to work nicely here,
from microbit import * while True: buttonState = pin0.read_digital() if buttonState==1: display.show(Image.YES) else: display.clear() sleep(20)
- The reed switch is quite easy to use. Its main selling point is that it is contactless. You don't need to be touching the parts of the circuit to activate the switch. This means that you can place the swtich behind plastic, cardboard or glass and still make the switch work. This provides the basic for some magic-like behaviours. You could make a die that always comes out six if the magnet is there and random if not.
- Place two reed switches in a line. Work out the exact positioning to make sure that your magnet activates them separately. You can now write a program to detect whether the switches both activated within a given time frame and if so, in what order. This allows you to detect whether a magnetic source is being swiped across the area that activates the switch and determine the direction. Set this up well and your two switches can record 5 different actions (left, right, swipe left, swipe right, both) if you have a magnet in each hand.