toggle light switch

Toggle Switch With Arduino

Arduino Projects

Hey guys, welcome back to Techatronic. In this article, we will teach you how you can make a toggle switch using Arduino. A toggle switch is that switch if you press it once it will turn the LED on and keep it on until you press the switch again. For making this project we are using an Arduino UNO microcontroller board. If you like this project you can read more amazing projects on Arduino. You can also check out our E-book on Arduino which contains 10+ projects with well-explained codes. We are provided you with the two circuit diagrams. It’s your choice to make the project either with one push button or two. Codes for both of the configurations are also provided below. You have to make the connections and after that upload, the code to the Arduino.

How Does it Work?

Here we are using a push button that servers as a toggle switch. You can read the working of a push button with Arduino UNO for a better understanding of the project. When you press the push button the corresponding LED will turn on. For turning the LED off you have to press the button again. In the code, we use a status variable that initially holds a false value, and when you press the button its value changes. A push-button completes the circuit when you press it and breaks the circuit when released. The Arduino keeps tracking the state of the LED and then invert its state when you push the button. The complete project looks like this.

toggle light switch

Components Required

  • Arduino UNO
  • Breadboard
  • Two pushbuttons
  • Two LEDs
  • 220-ohm Resistors
  • USB cable for uploading the code
  • Jumper wires

Circuit Diagram for the Project

  • For one toggle switch

  • For two toggle switch

toggle light switch

Make the connections according to the given diagrams. Connect the 5-volts pin of the Arduino with the one side of the push-button and the GND pin of the Arduino with the other side of the push-button through a 220-ohm resistor as shown. Attach the digital-2 pin of the Arduino to the push-button where the GND wire is connected. Join the digital-12 pin of the Arduino to the positive wire of the LED and the negative wire of the LED to the GND of Arduino. Use the breadboard for making common connections. Similarly, make the same connections for the second LED and push-button. Connect the positive wire of the LED with the digital-11 pin of the Arduino and the push-button with the digital-3 pin of the Arduino as shown.

toggle switch arduino code

NOTE: Please upload the code given below to the Arduino. Make sure that you upload the code according to the circuit you made.

  • For one toggle switch
 // TECHATRONIC.COM  
 int button = 2; // Push Button Pin D 2  
 int led = 12;  // Led Pin D 12  
 int status = false;  
 void setup()  
 {  
 pinMode(led, OUTPUT);  
 pinMode(button, INPUT_PULLUP);   
 }  
 void loop()  
 {  
 if (digitalRead(button) == true)  
 {  
 status = !status;  
 digitalWrite(led, status);  
 }   
 while(digitalRead(button) == true);  
 delay(50);  
 }  
  • For two toggle switch
 // TECHATRONIC.COM  
 int button1 = 2; // Push Button1 Pin D 2  
 int button2 = 3; // Push Button2 Pin D 3  
 int led1 = 12;  // Led1 Pin D 12  
 int led2 = 11;  // Led2 Pin D 11  
 int status1 = false;  
 int status2 = false;  
 void setup()  
 {  
 pinMode(led1, OUTPUT);  
 pinMode(led2, OUTPUT);  
 pinMode(button1, INPUT_PULLUP);   
 pinMode(button2, INPUT_PULLUP);   
 }  
 void loop()  
 {  
 if (digitalRead(button1) == true) // For Button 1  
 {  
 status1 = !status1 ;  
 digitalWrite(led1, status1);  
 }   
 if (digitalRead(button2) == true) // For Button 2  
 {  
 status2 = !status2 ;  
 digitalWrite(led2, status2);  
 }  
 while(digitalRead(button1) == true);  
 while(digitalRead(button2) == true);  
 delay(50);  
 }  

We hope that you learn to make a toggle switch using Arduino and please try to make it on your own. Also, do check out more tutorials on Arduino and Raspberry Pi written by us. If you are facing any problems let us know in the comments section below.

HAPPY LEARNING!

Spread the love, Share this post with friends