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.
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.
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:
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.
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!
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.
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.
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);
}
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.
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.
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);
}
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:
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.