Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit
Experiment 5: Reading an SPDT Switch
In this experiment you will use your first digital input: a switch. The SPDT (Single-Pole, Double-Throw) switch is a simple way to select between two options, especially when paired with an “if” state. You will use that switch to select which of the two LEDs will blink.
You will need the following parts:
- 1x Breadboard
- 1x micro:bit
- 1x micro:bit Breakout with Headers
- 2x LEDs (1 Red, 1 Yellow)
- 2x 100Ω Resistors
- 8x Jumper Wires
- 1x SPDT Switch
Didn’t Get the SIK for micro:bit?
If you are conducting this experiment and didn’t get the Inventor’s Kit, we suggest using these parts:
Before continuing with this tutorial, we recommend you be somewhat familiar with the concepts in these tutorials:
Introducing the Single-Pole, Double-Throw (SPDT) Switch
The Single-Pole, Double-Throw (SPDT) switch has a common pin in the middle and then two other pins that, depending on the location of the switch, are either connected to the common (center) pin or not. Reading a switch is similar to a button. You need to connect the common pin to a digital General Purpose Input/Output (GPIO) pin on your micro:bit and the other pins to 3.3V and ground. It doesn’t matter which pin is which. When you move the switch, the common pin will either be HIGH (connected to 3.3V) or LOW (connected to ground).
Ready to start hooking everything up? Check out the wiring diagram and hookup table below to see how everything is connected.
|Polarized Components||Pay special attention to the component’s markings indicating how to place it on the breadboard. Polarized components can only be connected to a circuit in one direction.|
Wiring Diagram for the Experiment
Having a hard time seeing the circuit? Click on the wiring diagram for a closer look.
Run Your Script
language:python # SparkFun Electronics # Experiment 5.0 # Reading an SPDT Switch from microbit import * while True: if pin0.read_digital(): pin15.write_digital(1) sleep(1000) pin15.write_digital(0) sleep(1000) else: pin16.write_digital(1) sleep(500) pin16.write_digital(0) sleep(500)
Code to Note
Just as the
write_digital() statement turns a pin on (1) or off (0), the
read_digital() statement determines the state of a pin, which is either HIGH (1) or LOW (0). By building a circuit that connects 3.3V or ground to a pin, we can detect if a switch is thrown or a button is pressed.
What You Should See
Depending on the state of the switch, a different LED will blink. If you move the switch to connect the signal pin to 3.3V (HIGH) then the LED connected to pin P15 will blink. If you flip the switch and ground the signal pin, then the LED on pin P16 will start blinking, and LED 1 will turn off.
Light Not Turning On
The wires for the switch are right next to each other. Make sure that signal is in the center with voltage and ground on the outside pins. If you connect ground and voltage, your board will short out and shut down.
Make sure your power LED is on. If it is off, pull the signal wire and see if that changes anything. If you short circuit your micro:bit board, it will turn itself off to protect the circuitry.
No worries; these circuits are all super stripped-down to make playing with the components easy, but once you throw them together, the sky is the limit.