SPDT Two Way Switch With Two LEDs
by jaishnus2019 in Circuits > Arduino
391 Views, 1 Favorites, 0 Comments
SPDT Two Way Switch With Two LEDs
Today I will show you how to use SPDT switch with two LEDs using Arduino UNO.
Download Arduino IDE
https://www.arduino.cc/en/software
Components Required
1. LED (2 Qty)
2. Breadboard
3. SPDT Switch
4. Arduino UNO
5. Jumper Wires
Schematic Diagram for SPDT and the Connections
This is the Diagrams about the connections.
First picture is the schematic diagram.
Second picture shows the connections with real components.
Code
Code-
This the code for the program-
// Learn SPDT switch two-way with 2 LED // Virtual Program (without Arduino) -> https://www.tinkercad.com/things/3uNGAyy7bvY // Program- int tp_no1 = 5; //pin when the switch is focused in terminal 1 int tp_no2 = 4; //pin when the switch is focused in terminal 2 int led_1 = 0; //LED for SPT&1 int led_2 = 13; //LED for SPT&2 int value_1 = 0; //value for SP&T1 (F/A) int value_2 = 0; //value for SP&T2 (F/A) // mostly 'Away' is default and we keep it's value as '0'. Value 1 is 'Focused'. void setup () { //pin setup pinMode(led_1,OUTPUT); pinMode(led_2,OUTPUT); // LED pins are OUTPUTS. pinMode(tp_no1,INPUT); pinMode(tp_no2,OUTPUT); // Non-power supply pins of SPDT are INPUTS. Serial.begin(9600); //Start Serial Monitor at 9600 br. } void loop () { //main code value_1 = digitalRead(tp_no1); //Value becomes the Switch value (Throw 1) value_2 = digitalRead(tp_no2); //Value becomes the Switch value (Throw 2) if (value_1 == 1) { // if throw to end 1 is focused and throw to end 2 is away. digitalWrite(led_1,HIGH); //LED 1 is on (as the switch is focused to it's end) digitalWrite(led_2,LOW); //LED 2 is off (as the switch is away from it's end) Serial.println(1); } else if (value_2 == 1) { // if throw to end 2 is focused and throw to end 1 is away. digitalWrite(led_2,HIGH); //LED 2 is on (as the switch is focused to it's end) digitalWrite(led_1,LOW); //LED 1 is off (as the switch is away from it's end) Serial.println(2); } else { //if both dosen't work (that'll only happen 1%) Serial.println("Error"); } }
Downloads
Conclusion
1. Save and Verify the code in your PC.
2. Connect Arduino UNO board to your PC using the cable.
3. Go to 'Board' option in tools menu and click 'Arduino Uno'. Similarly, go to 'Port' option and click the given port.
4. Click 'Upload' button as shown in the picture.