Learn to code

ARDUINO & CODES

Previous Post
Arduino Tutorial: 4.0 Introduction into digital / analog pins
Next Post
Making a toggle switch button with Arduino

Controlling a LED with a button for Arduino

in Arduino Digital / Analog

In this tutorial, you will learn how to control a LED by pushing a button.

We have learned in earlier tutorials. how to automatically blink an LED with Arduino. That program will run as long as there is a power for the Arduino. Let’s go a bit further and try to control the LED with a button. We use a pushbutton to control the Blinking. In other words, we are going to make a simple program which allows you to control the LED by pushing 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

We will use one resistor for the LED and one for the Pushbutton.

When the pushbutton is open (unpressed) there is no connection between the two legs of the pushbutton, so the pin is connected to ground (through the pull-down resistor) and we read a LOW. When the button is closed (pressed), it makes a connection between its two legs, connecting the pin to 5 volts, so that we read a HIGH.

The Code

//example sketch pushbutton
const int buttonPin = 2;     // the number of the pushbutton pin
const int ledPin =  10;      // the number of the LED pin
void setup() {
  // initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);
}
void loop() {
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (digitalRead(buttonPin) == HIGH) {
    // turn LED on:
    digitalWrite(ledPin, HIGH);
  } else {
    // turn LED off:
    digitalWrite(ledPin, LOW);
  }
}

Code Explanation

this code is very simple, this following tutorial has some more code and features. As mentioned before I find it more easy to read to give names to variables instead of numbers.  I added a variable for the button and attached it to pin 2. Furthermore, we declared a constant variable for the LED and attached it to pin 10.

We need to set the button variable to an input mode and set the LED to an output mode.

// initialize the LED pin as an output:
  pinMode(ledPin, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);

In the void loop () part we are going to control the LED by using an If statement.

The If statement above will check the following condition and checks if it is true. We use a comparison operator “==”. See the blog post: Understanding Arduino operators for more information. If the button is pressed it sends a HIGH signal to the Arduino. Therefore, we need to check if the condition is met that the buttonPin is HIGH. If that condition is not met the loop goes to the Else Statment where the LED is turned off.

If you uploaded the sketch you will see that the LED is turned on every time you push the button. But what if we want that the LED will stay on if the button is pressed and turned off when the button is pressed again. For that, we will have use to button as a switch and determine the state in which it last was.  That is what we will explore in the next tutorial: Arduino Tutorial: 4.2 Make a Toggle Switch button

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