Arduino IDE: turn on LEDs using a button (if) #4.1

Arduino IDE

Welcome back to our programming tutorial using the Arduino IDE. Today we will show how to turn a LED on and off using a button and the conditional if construct.

You can take a look at the previous chapters of the course here:

In this lesson you will learn how to use the push buttons to turn an LED on and off. Pressing the first button will light up the LED, while pressing the second button will turn off the LED.
List of the required componentsi:

Everything you need is present in the Elegoo Advanced Starter Kit.

Here is the connections diagram:

if example LED connection

And the electric circuit:

if LED test connection
Switches are very simple components. When you press a button, two contacts are connected, so that electricity can flow through them. The small touch switch that is used in this lesson has four connections.

Although the switch body is square, the pins protrude from two opposite sides of the switch. This means that the pins allow the switch to be inserted only in one direction on the breadboard, this is the only correct direction to avoid short circuits.

Remember that the LED must have the shorter (negative) pin connected to the left.
if construct driven LED circuit

And finally, here is the source code to enable the circuit:


Once the code has been loaded onto your Arduino board, pressing the left button will turn the LED on, pressing the right one instead will turn the LED off.

The first part of the code defines three variables, one for each of the three digital pins that will be used.

The “ledPin” is the output pin, “buttonApin” is the first of the two buttons to turn the LED on and off, while “buttonBpin” is the second button of the two.

The setup function defines ledPin as a normal OUTPUT, now let’s see how to deal with the other two inputs. In this case the pinMode of the two switches will be set as shown below at “INPUT_PULLUP”:

Setting the input mode as INPUT_PULLUP means that the pin will be used as input: if nothing else is connected to the output pin it must be “pulled up” therefore in HIGH mode. In other words, the default value of the input will be HIGH unless it is brought to LOW by pressing the button that has the other pair of pins is connected to the GND output. When a button is pressed, it connects the input pin to the GND pin therefore the first will no longer be in HIGH mode.

Since the input is normally in HIGH, and is brought to LOW only when the button is pressed, the direction of the button is set in reverse. We will deal with this later in the “loop” function.

In the “loop” function there are two “if” statements. One for each button. Each of them performs a “digitalRead” reading of the appropriate input.

Remember that when a button is pressed the corresponding input will be set to LOW, if button A is brought to LOW then the “digitalWrite” function on ledPin will be set to HIGH, by turning on the led. Similarly, when button B is pressed, ledPin is set to LOW by turning off the led.

The IF construct on this example deals totally with the INPUT_PULLUP feature of the digital pins.

Previous articles:

Follow us to keep yourself updated with the news!

Simone Candido è un ragazzo appassionato del mondo tech nella sua totalità. Simone ama immedesimarsi in nuove esperienze, la sua filosofia si basa sulla irrefrenabile voglia di ampliare a 360° le sue conoscenze abbracciando tutti i campi del sapere, in quanto ritiene che il sapere umano sia il connubio perfetto tra cultura umanistica e scientifica.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.