3 Pin Toggle Switch Arduino: Find out how to wire it on an Arduino. Including examples showing you Exactly How to use them in your Programs.

The great thing about using a 3 pin toggle switch with an Arduino is that, unlike a push button, you can see exactly what the toggle switch is doing. It's either going to be pushed forwards or pushed backwards. Often close to these positions you will see annotations that tell you what the switch is controlling.

3 pin toggle switch Arduino examples.

The left toggle switch is a through hole mount, right angled power switch (low current),while the right one is a more traditional rounded lever toggle switch that can take a lot more current.

You can make a simulated toggle switch using just a push-and-release button, by using the memory in the microcontroller to change what happens after each push. But by using a real 3 pin toggle switch you get that memory built in.

3 Pin Toggle Switch Arduino: Uses of toggle switches

You can use a toggle switch to route different signals to an Arduino input e.g. for an oscilloscope you could route the input via a capacitor resulting selection of A.C. or D.C operation.

Alternatively you can use it as an input, showing the desired state of an operation e.g. high or low as an input to control operation of software based on the state of an input pin in the microcontroller.

Often you will connect them so that power input can be from an external source while the other in selects an internal battery voltage for the power source (or one pin not connected at all - for the 'off' state - so it becomes a power on/off switch).

A Summary of usage examples:

  • Signal Router: As mentioned previously; selection of A.C. or D.C. signal input.

  • Mode Selector: Use to signal what action should be made i.e. indicate to software what mode to execute.

  • Power Switch: Selection of a different power source to the center pin or used as an on/off switch.

  • Motor Control: Setting the direction of a motor i.e. directing a different polarity voltage to the motor.

3 Pin Toggle Switch Arduino: What is a toggle switch

A toggle switch is a type of electrical switch that has a lever or handle which can be moved up or down. You can also get switches with a dual set of (3) pins so you get a switch that can control two separate signals. 

The switch toggles between two states (using an internal spring mechanism), hence the name "toggle switch" - it toggles and is kept in either state. The spring mechanism forces the lever to be kept in either position until the user moves it to the opposite toggle position.

Note: Toggle switches are rated for different voltages/current.

They are rated for different voltage and current so if you are making a project with high voltage or high current make sure your toggle switch can handle it!

3 Pin Toggle Switch Arduino: Connecting a Toggle Switch

To use a toggle switch as a control input connect the center pin to the Arduino's digital input pin, connect one pin to ground (GND), and the other pin to 5V (VCC).

When the lever is switched to the ON position, it will connect the digital input pin to VCC, causing the Arduino to read a HIGH signal. When the lever is switched to the OFF position, it will connect the digital input pin to GND, causing the Arduino to read a LOW signal.

You can use this as a control input in projects where you could turn on/off an LED or motor, or toggle between different modes or functions within your project.

3 Pin Toggle Switch Arduino: Example Sketch1

3 pin toggle switch on an arduino with 2 LED indicators
Image generated with Fritzing

The example below reads the state of a 3 pin toggle switch on an Arduino and outputs its state to the serail port. If low then LED1 is turned on (LED2 off) and if high LED2 is turned on (LED1 off).

Connect toggle switch middle pin to Arduino pin 2.
Connect bottom pin of toggle switch to VCC.
Connect top pin of toggle switch to GND.

Note: GND is connected to toggle switch top pin and VCC is connected to toggle switch bottom pin so you: get push forwards = HIGH, and push backwards = LOW.

Connect Arduino pin 4 to an LED (round side).
    Connect the same LED (flat side) via a resistor (10k to ground).

Connect Arduino pin 5 to an LED (round side).
    Connect the same LED (flat side) via a resistor (10k to ground).

// Example1: 3 pin toggle switch for the Arduino.
//
// Turn on a different LED depending on
// the toggle switch input state.

static const int TSW_PIN = 2; // set input pin for switch
static const int LED1_PIN = 4;
static const int LED2_PIN = 5;
static int TSW_State = 0; // variable for storing switch status

void setup() {

  pinMode(TSW_PIN, INPUT); // set pin as input
  pinMode(LED1_PIN, OUTPUT); // set pin as output
  pinMode(LED2_PIN, OUTPUT); // set pin as output

  Serial.begin(115200); // initialize serial communication
}

void loop() {
  TSW_State = digitalRead(TSW_PIN); // read state of switch
  Serial.println(TSW_State); // print state of switch

  if(TSW_State==0) {
    digitalWrite(LED1_PIN,HIGH);
    digitalWrite(LED2_PIN,LOW);

  } else {
    digitalWrite(LED1_PIN,LOW);
    digitalWrite(LED2_PIN,HIGH);
  }
delay(300);
}

3 Pin Toggle Switch Arduino: Alternative Connection

3 pin toggle switch on an arduino with 2 LED indicators floating high
Image generated with Fritzing

If you don't want to wire up the toggle switch to VCC you can use the internal pullup of an Arduino pin and use only a ground connection. In this case; for the same use as the control input as before:

Connect the center pin to the Arduino's digital input pin, connect one pin to ground (GND), and leave the other pin floating (unconnected).

In the code make the toggle switch input pin an PULL-UP input. This will operate in exactly the same way as the code in the last example.

Note: The reason you may want to use the previous code example (Ex1) is that some microcontrollers do not have pull ups on all pins. Arduino Uno/Nano do have pull up resistors that you switch into the circuit under program control.

When the toggle switch is set to the ground pin 0V is fed to the input of the Arduino; (LOW detected). When the toggle switch is set to the floating input, the pull up, on the Arduino pin pulls up the Arduino input to 5V; (HIGH detected).

The only difference in wiring from the last example is that the VCC connection is removed from the toggle switch.

3 Pin Toggle Switch Arduino: Example Sketch2

This example uses internal pull ups to obtain the required functionality.

// Example2: 3 pin toggle switch for the Arduino.
//
// Turn on a different LED depending on
// the toggle switch input state.

static const int TSW_PIN = 2; // set input pin for switch
static const int LED1_PIN = 4;
static const int LED2_PIN = 5;
static int TSW_State = 0; // variable for storing switch status

void setup() {

  pinMode(TSW_PIN, INPUT_PULLUP); // set pin as input
  pinMode(LED1_PIN, OUTPUT); // set pin as output
  pinMode(LED2_PIN, OUTPUT); // set pin as output

  Serial.begin(115200); // initialize serial communication
}

void loop() {
  TSW_State = digitalRead(TSW_PIN); // read state of switch
  Serial.println(TSW_State); // print state of switch

  if(TSW_State==0) {
    digitalWrite(LED1_PIN,HIGH);
    digitalWrite(LED2_PIN,LOW);

  } else {
    digitalWrite(LED1_PIN,LOW);
    digitalWrite(LED2_PIN,HIGH);
  }
delay(300);
}

3 Pin Toggle Switch Arduino: Conclusion

You can use a toggle switch as a mode input control but there are other uses for a toggle switch, such as power on/off or signal routing.

Using a toggle switch is quite easy as shown in the examples. There are two flavours:

  1. A toggle switch with GND and a floating pin.
  2. A toggle switch with GND and VCC connections.

Since the Arduino Uno/nano has internal pullups you can use the 1st method if you want to - 1 less wire to connect!



Comments

Have your say about what you just read! Leave me a comment in the box below.

Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.



Privacy Policy | Contact | About Me

Site Map | Terms of Use