Introduction: Reading an SPDT Switch

Picture of Reading an SPDT Switch

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.

Step 1: Tools and Supplies

Picture of Tools and Supplies
  • Arduino 101 or Arduino Uno
  • SPDT switch
  • Breadboard
  • Jumper Wires
  • Blue LED
  • Yellow LED
  • 2 pieces of 100Ω resistor

Step 2: How an SPDT Switch Works?

Picture of How an SPDT Switch Works?

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.

Step 3: Circuitry

Picture of Circuitry

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.

Step 4: Code

Picture of Code

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); } }

Step 5: Demo

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

Comments

Ragdoll012 (author)2017-07-19

I'm not sure if the Arduino I/O pins will sink current while set up as a digital input, but i would recommend replacing the jumper from the 3V3 rail to the switch with a resistor. The pin will still read as high and it will possibly protect your Arduino from excessive currents.

About This Instructable

698views

13favorites

License:

Bio: Aerospace Engineer working in Software Development with a passion for Hardware.
More by TechMartian:Flying Captain America's Shield - RC PlaneWide Angle ShotsLED Ping Pong With Shift Register
Add instructable to: