Reading an SPDT Switch

16,274

18

3

Introduction: 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

  • 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?

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

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

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

Makerspace Contest 2017

Participated in the
Makerspace Contest 2017

Be the First to Share

    Recommendations

    • Crayons Challenge

      Crayons Challenge
    • Metal Contest

      Metal Contest
    • Teach With Tinkercad Contest

      Teach With Tinkercad Contest

    3 Comments

    0
    Ragdoll012
    Ragdoll012

    5 years ago

    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.

    0
    wobbler
    wobbler

    Reply 8 months ago

    Bit of late reply, but see my comment above. When an Arduino is set as input it is effectively high impedance, so it's fine to use a switch like this. If you enable the pull-up internal resistor it's still fine as it's of the order of 20k so nothing will get fried. The only issue with wiring switches across a power supply like this is really dependent on the switch. If it breaks before it makes (as most do), no problem, but some switches, especially rotary wafer switches, sometimes don't and as a result, you'd be shorting out the power supply, if only for a short time. I once tried this with light switches, putting L onto NO and neutral on the NC while I used the Common to feed either live or neutral (I was younger and dafter and thought I was saving a wire). The switch wasn't happy about it so now I never do it, even on low power circuits

    0
    wobbler
    wobbler

    8 months ago

    There is no need to use actually use a double throw switch or, if you do use one, to use three wires. If an Arduino I/O pin is defined as input for a switch the easiest way is to simply connect two wires and use the common and either the NC or NO. It doesn't really matter whether you use the NO or the NC because the code decides what happens when the button is in one direction or the other, all that will happen is the switch will read true in the other direction if the NO is used instead of the NC. Connect one to the input and the other to ground, it doesn't matter which goes to which as it's a switch. Just remember that when a switch is open it gives TRUE and when it's closed it gives FALSE as it will be pulled to ground.

    You also don't need to connect a pullup resistor as the Arduino has a built-in pull up resistor on the input pins which you can switch on with the following code:
    pinMode(pinNo,INPUT_PULLUP);

    Better explanation and diagram is here:
    https://docs.arduino.cc/tutorials/generic/digital-...