Getting Started with MicroPython and the SparkFun Inventor's Kit for micro:bit
Experiment 5: Reading an SPDT Switch
Introduction
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.
Parts Needed
You will need the following parts:
- 1x Breadboard
- 1x micro:bit
- 1x micro:bit Breakout with Headers
- 1x micro-B USB Cable
- 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:
Suggested Reading
Before continuing with this tutorial, we recommend you be somewhat familiar with the concepts in these tutorials:
Button and Switch Basics
May 7, 2013
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).
Hardware Hookup
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
Run Your Script
Let's read a SPDT switch and control some LEDs. Type (or copy) the program into your Mu editor, or download all the programs from this GitHub Repository and open the Ex5_readSPDT.py program. Save it, then click the Flash icon to program your micro:bit.
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
pin0.read_digital()
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.
Troubleshooting
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.
Underwhelmed
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.