• Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors
HomeProject3-way switch and Arduino
Previous Next

3-way switch and Arduino

Posted by: Jia Qun , May 19, 2016

With this small tutorial, I’m going to show you how to read the position of a 3-way switch using just one Arduino’s analog PIN.

A 3-way switch (sometimes called ON-OFF-ON or SPTT) has 3 PINs: the common (on the center) and the two outputs. When the lever is up or down, the common PIN is connected to one of the output ones; when the lever is in the central position, the common PIN is not connected (floating).

Using only two resistors, you can read the lever position with an analog PIN:

3sbutton-1

To understand how the circuit above works, let’s inspect what happens in the three different positions of the lever:

  • when the lever is in the central position (2), the AN0 PIN is connected to 5V through the R1 resistor (pull-up): the read value is about 1023
  • when the lever is in the top position (3), the AN0 PIN is connected to a voltage divider (R1 and R2). Being the two resistors of the same value, the voltage at the AN0 PIN is about 2.5V and therefore the read value is 512
  • when  the lever is in the bottom position (1), the AN0 PIN is connected to the ground; the read value is 0.
     

The sketch is very simple:

#define BUTTON_PIN A0
int previousState;
 
void setup() {
 
  Serial.begin(9600);
  previousState = 0;
}
 
void loop() {
 
  int analogValue = analogRead(BUTTON_PIN);
 
  int actualState;
  if(analogValue < 100) actualState = 1;
  else if(analogValue < 900) actualState = 3;
  else actualState = 2;
 
  if(previousState != actualState) {
 
    previousState = actualState;
    Serial.print("Button state: ");
    Serial.println(actualState);
  }
}

I added a tolerance threshold to the read values.

Demo

The circuit is very simple, I built it on a prototype board:

3sbutton-4 3sbutton-3

3sbutton-2

For more details,please refer to origianl post

http://www.lucadentella.it/en/2014/08/01/interruttore-a-tre-posizioni-e-arduino/

Share!
Tweet

Jia Qun

About the author

Leave a Reply Cancel reply

You must be logged in to post a comment.

Category

  • Home
  • pcDuino
  • WiKi
  • Store
  • Distributors