3-way switch and Arduino

by luca

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:


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() {
  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: ");

I added a tolerance threshold to the read values.


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

3sbutton-4 3sbutton-3


Related Posts


Mark Sunday October 18th, 2015 - 01:02 PM

Luca thanks for the code. I would like to add another 3-way switch so I would have two. But i’m not a coder so I don’t know how to do it. Can you help?

luca Monday October 19th, 2015 - 08:25 AM

Hi Mark, simply use a different analog PIN (for example A1):
#define BUTTON_PIN2 A1
int analogValue2 = analogRead(BUTTON_PIN2);


Leave a Comment

four + 12 =