Reading an SPDT Switch
![2016-07-16 20.54.36.jpg](/proxy/?url=https://content.instructables.com/FVK/YSNW/J58FPDVQ/FVKYSNWJ58FPDVQ.jpg&filename=2016-07-16 20.54.36.jpg)
Reading values from SPDT switches, or any switches, as a matter of fact, is one of the most essential skills to grasp in the realm of micro controllers. It is one of the simplest digital inputs to read, albeit slightly more complex than a button 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. This can be applied to virtually any project requiring two inputs of opposite nature.
Tools and Supplies
![2016-07-15 12.34.27.jpg](/proxy/?url=https://content.instructables.com/FCO/VRGA/J58FPE01/FCOVRGAJ58FPE01.jpg&filename=2016-07-15 12.34.27.jpg)
- Arduino 101 or Arduino Uno
- SPDT switch
- Breadboard
- Jumper Wires
- Blue LED
- Yellow LED
- 2 pieces of 100Ω resistor
How an SPDT Switch Works?
![spdt.png](/proxy/?url=https://content.instructables.com/F5E/RW9F/J4G5357C/F5ERW9FJ4G5357C.png&filename=spdt.png)
![dpdt.jpg](/proxy/?url=https://content.instructables.com/FVL/7104/J4G5357A/FVL7104J4G5357A.jpg&filename=dpdt.jpg)
Switches are composed of two conductors that may or may not in contact with each other depending if it is a normally-open or normally-closed switch. Normally open switches make an open circuit in it's "normal" or default configuration. While, a normally closed switch is the opposite wherein a closed circuit is the default configuration of the switch.
When a switch position is changed the two conductors are connected or disconnected for normally-open and normally-closed switches, respectively.
Circuitry
![Screen Shot 2017-07-17 at 10.58.20 PM.png](/proxy/?url=https://content.instructables.com/F4R/E2N8/J58FPE6W/F4RE2N8J58FPE6W.png&filename=Screen Shot 2017-07-17 at 10.58.20 PM.png)
![2016-07-16 20.54.44.jpg](/proxy/?url=https://content.instructables.com/FY7/HIFS/J58FPDVU/FY7HIFSJ58FPDVU.jpg&filename=2016-07-16 20.54.44.jpg)
![2016-07-16 20.54.58.jpg](/proxy/?url=https://content.instructables.com/F9N/2EO1/J58FPDVZ/F9N2EO1J58FPDVZ.jpg&filename=2016-07-16 20.54.58.jpg)
The power will be supplied by the Arduino to power both the LEDs and the switch, therefore we will need to use the power rail to distribute the 3.3V power in parallel to all three electrical components.
- Connect the 3.3V power from the Arduino to the red power rail of the breadboard using a red jumper wire.
- Connect the GND pin from the Arduino to the black rail on the breadboard with a black jumper wire.
Connecting the switch to power and the Arduino
- Connect the middle pin of the SPDT switch to pin 8 on the Arduino
- Connect one of the outer pins to the red power rail on the breadboard
- While the remaining outer pin will be connected to the black power rail on the breadboard
Connecting the LEDs to the Arduino
- Connect the negative pin of both the LEDs (flat side of the LEDs) to a common ground on the black rail of the breadboard
- Connect the positive pin of each of the LEDs to a 100Ω resistor separately.
- Then, connect each of these resistors in series to pin 5 and 6 respectively.
Code
![Screen Shot 2017-07-17 at 9.53.02 PM.png](/proxy/?url=https://content.instructables.com/F36/FH93/J58FPE14/F36FH93J58FPE14.png&filename=Screen Shot 2017-07-17 at 9.53.02 PM.png)
void setup() {
// put your setup code here, to run once:
// set the switch input, pin 8, as an INPUT pinMode(8, INPUT);
// set both the LED pins, pin 5 and 6 as OUTPUTs pinMode (5, OUTPUT); pinMode (6, OUTPUT); }
void loop() { // put your main code here, to run repeatedly: // read the value of the SPDT switch int switchValue = digitalRead(8);
if (switchValue == 1){ // if the switch is in Position 1, then turn ON the pin 5 LED digitalWrite (5, HIGH); // make sure that the other LED in pin 6 is OFF digitalWrite (6, LOW); } else{ // if the switch is in Position 0, then turn ON the pin 6 LED digitalWrite (6, HIGH); //make sure that the other LED in pun 5 is OFF digitalWrite (5, LOW); } }
Demo
![Arduino SPDT switch](/proxy/?url=https://content.instructables.com/FDM/FLCE/J58FPDFX/FDMFLCEJ58FPDFX.jpg&filename=Arduino SPDT switch)
Since the switch is connected to 3.3V on one side while the other is connected to ground, this can be read as being HIGH or LOW. Depending on where the plate is connected to, this will determine the value that is outputted by the switch, this allowing us to control an LED.
You can see in the demo video that when the SPDT switch is on it's original position the blue LED is on, while the other LED turns on when it is switched. This makes it a very useful input signal in the realm of robotics