Learn to code

ARDUINO & CODES

Previous Post
Controlling a LED with a button for Arduino
Next Post
Creating multiple button states with Arduino

Making a toggle switch button with Arduino

in Arduino Digital / Analog

In the previous tutorial, we already learned how to turn a LED on with a button. The problem arises that if the button is unpressed the LED turns off. If you would like to keep the LED on after the button is pressed we need to remember the state of the button. We can use the boolean function for this. We will use and modify the code we already made in the tutorial: Control a LED with a button

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 1
Breadboard 400 point Breadboard 400 point × 1
Dupont Wires Dupont Wires × 7
LED LED × 1
Tactile Push Button Tactile Push Button × 1
10K Resistor 10K Resistor × 1
220 ohm resistor 220 ohm resistor × 1

Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

Led Button Debounce

The Code

//  Make a Toggle Switch Button
// Define the pins being used
int pinLed = 10;
int pinSwitch = 2;
// declaring variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean LEDstatus = LOW;
void setup()
{
  pinMode(pinLed, OUTPUT);
  digitalWrite(pinLed, LOW);
  pinMode(pinSwitch, INPUT);
}
void loop()
{
  newSwitchState = digitalRead(pinSwitch);
  if ( newSwitchState != oldSwitchState )
  {
    // has the button switch been closed?
    if ( newSwitchState == HIGH )
    {
      if ( LEDstatus == LOW ) {
        digitalWrite(pinLed, HIGH);
        LEDstatus = HIGH;
      }
      else                    {
        digitalWrite(pinLed, LOW);
        LEDstatus = LOW;
      }
    }
    oldSwitchState = newSwitchState;
  }
}

 

Code Explanation

As you can see I have added variables to hold the current and new state of the button. There is a Boolean variable that holds/remember the state of the object, in our case the button. If the program is run for the first time both states should be LOW. We can now transform the button into a switch.

// declaring variables to hold the new and old switch states
boolean oldSwitchState = LOW;
boolean newSwitchState = LOW;
boolean LEDstatus = LOW;

 

An important part of the sketch is to set the oldSwitchState to the current newSwitchState. We will add the below code to change the state of the button. This is is important, since we will use it in our if statements.

 oldSwitchState = newSwitchState;
 

[/vc_column_text]

After we declared our pins and our variables we arrive at the void loop() part of our sketch. In the loop function, the sketch checks whether or not the button is pressed. The sketch will read the signal from the button to give the newSwitchState a LOW or HIGH value. HIGH for pressing the button and LOW if the button is not pressed.

 newSwitchState = digitalRead(pinSwitch);

We need to know a couple of things to make sure that the button works as a switch. Firstly, we need to check the condition whether the newSwitchState is different from the oldSwitchState. For that, we use a comparison operator “!=”. Rember, if the button is pressed it sends a HIGH signal if it is not pressed it sends a LOW signal.

 
  if ( newSwitchState != oldSwitchState )

 

After the check is completed we need to check if the button is sending a HIGH signal meaning that the button is pressed.

  if ( newSwitchState == HIGH )

 

Furthermore, we need to know whether the LEDstatus is HIGH or LOW. In other words, we need to know whether the LED is on or off. If the LEDstatus is LOW and the button is pressed then the program should recognize that we want to turn the LED on.

if ( LEDstatus == LOW ) {
        digitalWrite(pinLed, HIGH);
        LEDstatus = HIGH;
      }

 

The last statement that we will use is an Else statement. If the if statements are false, and the button is pressed, we want the LED to turn off.

else                    {
        digitalWrite(pinLed, LOW);
        LEDstatus = LOW;
      }

 

An important part of the sketch is to set the oldSwitchState to the current newSwitchState. We will add the below code to change the state of the button. This is is important, since we will use it in our if statements.

 oldSwitchState = newSwitchState;
[/vc_column][/vc_row]

2 Comments. Leave new

Why did you use serial.begin(9600)

Reply

    Hi iqbal,

    well, I normally always put the code for Serial Communication into a sketch. However, in the example code of this tutorial, it is not necessary since the code does not print anything to the Serial Monitor.

    Regards

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu