Arduino LED with Button Tutorial - Control an LED with a Push Button
***above-1st-paragraph-new.shtml**
Introduction
In this beginner Arduino LED with button project, you will learn how
to control an LED using a push button. The circuit will allow you to
turn an LED on or off by simply pressing a button connected to your
Arduino board.
Controlling outputs with user inputs is a fundamental and extremely
important concept in electronics and embedded systems because you'll use
inputs and output in every system you design.
This simple LED button circuit is a great way to get started with
Arduino as it introduces fundamental topics like digital input/output,
if-else logic and button presses as events that trigger code.
By the end of this tutorial, you will understand how to build the
circuit on a breadboard, write the code to sense button presses and
drive the LED accordingly.
Let's get started on our first introductory project!
Required Components
Arduino Uno board
LED (any color)
1k ohm resistor
Push button
Breadboard
Jumper wires
Circuit Diagram :Arduino LED with button
The layout diagram shows how to place components on the breadboard.
Note that you have to place the LED the right way round -the flat side
of the LED (cathode - or negative side) connects to ground (GND or 0V).
Ground (0V) and supply voltage (5V) are connected from pins in the
lower pin header of the Arduino Uno, while control pins 2 and 7 are
connected from the
top connector.
Diagram using fritzing
The schematic shows the pin connections and components.
Diagram using fritzing
Example Sketch : Arduino LED with button
You can copy and paste the code below into the Arduino IDE (in a new
sketch) replacing everything that is in the new sketch window (See "Uploading the code" below).
// LED pin
int ledPin = 7;
// Button pin
int buttonPin = 2;
voidsetup(){
// Set LED pin as output
pinMode(ledPin, OUTPUT);
// Set button pin as input with internal pull-up resistor
pinMode(buttonPin, INPUT_PULLUP);
}
voidloop(){
// Read the state of the button pin
int buttonState = digitalRead(buttonPin);
// Check if button is pressed
if(buttonState == LOW){
// Button pressed, turn LED on
digitalWrite(ledPin, HIGH);
}else{
// Button not pressed, turn LED off
digitalWrite(ledPin, LOW);
}
}
Short Code Explanation
The following bullet points explain how the code works in short form.
LED is connected to pin 7 and set as OUTPUT
Button is connected to pin 2 and set as INPUT_PULLUP
Read button state and check if LOW (pressed)
If pressed, turn LED ON by writing HIGH to pin 7
If not pressed, turn LED OFF by writing LOW
This will turn the LED on when the button is pressed and off when released.
Detailed Code Explanation
This is a more detailed explanation of the code operation, explaining all the code used in the Arduino with LED Button tutorial.
The code is divided into two main parts - setup() and loop().
setup() runs once at the start to initialize pins.
ledPin and buttonPin variables declare the pin numbers used.
pinMode() sets the mode of each pin - ledPin as OUTPUT to control the LED, buttonPin as INPUT_PULLUP to read the button.
The loop() function runs continuously after setup().
digitalRead(buttonPin) reads the state of the button pin - HIGH or LOW.
The code check if the reading is LOW (if(buttonState == LOW)) which means the button is pressed.
If pressed, digitalWrite(ledPin, HIGH) turns the LED on by writing HIGH to the pin.
If not pressed, LED is turned off with digitalWrite(ledPin, LOW).
Setting the button pin as INPUT_PULLUP means there is an internal
resistor pulling the pin HIGH. Pressing the button connects it to
ground, pulling the pin LOW.
So each time the loop runs, it checks the button state and toggles the LED accordingly.
This provides a simple switch to turn the LED on/off by pressing/releasing the button.
Explaining the button press.
The only thing that can cause confusion is that when you press the
button the input to the microcontroller goes to 0V. This is
counter-intuative as you expect a button to produce a high voltage for
on.
The reason for this is the internal pull-up resistor in the
microcontroller. You don't get pull-downs (at least not in most chip
designs). When the pin is left alone the pull-up pulls the pin voltage
to 5V (hence the name pull-up). This sets the inactive state of the input pin to 5V (high).
Note:
You could add your own pull-down resistor (100kOhms), so that the
button input reads "1" for a button press. You would turn off the
internal
pullup (change INPUT_PULLUP to INPUT), and change the code to turn on
the LED when the input signal is high (5V), and change the circuit so
that the
button connects to 5V (not GND).
But, because it is more work, it is easier to just change the sense of the input value in software!
When the pin is shorted to ground - by you pressing the button, the
voltage at the pin is pulled to 0V - the input voltage at the
microcontroller pin is 0V representing "0". So a press = "0".
The other end of the internal resistor is still at 5V, but because
the internal pull-up resistor is of high value 50k~100kOhms only a small
current flows (while you press the button) so it is safe. Small current
= low heating therefore does not blow up.
Inverting the input logic for the button
In the code, when you press the button, and the input is set to 0V,
the code interprets this to mean turn on the LED i.e. it inverts the
value of the button - the Zero input value is translated to a "One" (or
5V) at the output pin.
The relevant part of the code is this:
// Check if button is pressed if(buttonState == LOW){
// Button pressed, turn LED on digitalWrite(ledPin, HIGH);
Uploading the Code
There are a few steps to uploading the code using the Arduino IDE:
Connect the Arduino Uno to the PC with a USB cable.
Select the Arduino Uno hardware.
Open a new sketch.
Paste the code above into the new page (overwrite everything).
Press the upload button (right arrow at top).
You can find a more detailed tutorial on the Arduino IDE page.
Testing the Circuit
Just push the button OK!
When pushed the LED lights up.
When released the LED is off.
Troubleshooting Arduino LED with button
Even though this is a simple circuit there are two things that can go wrong.
Putting the LED the wrong way.
Putting the switch button the wrong way.
The LED
If LED doesn't light its probably the wrong way round - an LED
has polarity - it is actually a diode (Light Emitting Diode) so current
will flow when positive voltage connects to the positive terminal, and
negative voltage connects to the negative terminal.
If you get the LED round the wrong way it won't light up so you have
to be able to tell which side is positive and which is negative.
There are two ways to do this:
The flat side of the LED is negative (look from the top to see which is the flat and round side).
The short lead is negative (not much if you've chopped the legs off - hence why 1. is available).
The switch
I can never remember which way round the switch is placed! Even
though there are 4 connections two pairs are connected together. Usually
I use a multimeter to check but this is not crucial - you want the pins
that are not connected (unless the button is pressed) to be left and
right in the above layout diagram. If your switch is
the wrong way round (after programming - the LED will always be on) then
turn it by 90 degrees.
Note: These types of
switches do not naturally fit into a breadboard so I use a pair of
flat-nosed pliers to flatten the legs so they will go into the
breadboard.
Conclusion
Even though it is a simple circuit it demonstrates the fundamental operation when you use microcontrollers:
Reading inputs using the function digitalRead().
Setting outputs using the function digitalWrite().
It also shows that you need to wire up circuits careful as you can
easily get components the wrong way round. In this case, no damage will
occur. But for more expensive chips you want to get it right (putting
the power supply the wrong way around will blow up any chip). So, always
double check before powering the board i.e. before plugging in the USB
cable. This helps avoid accidental damage.
Next Tutorial : Why the button bounces (causing errors), and how to fix it.
Written by John Main who has a degree in Electronic Engineering.
Note: Parts of this page were written using claude-instant as a research assistant.
How to get accurate DHT22 digital humidity sensor readings with an Arduino. Did you know it also measures temperature as Well? Find out why, in this page...
A PIR sensor lets your Arduino sense movement without contact. This tutorial covers PIR sensor basics, connecting one to an Arduino board and coding a motion detector.
Arduino Hall Effect Sensor: Add magnetic sensing superpowers to your Arduino projects with an easy-to-use hall effect sensor. With full code and layout...
Comments
Have your say about what you just read! Leave me a comment in the box below.
Don’t see the comments box? Log in to your Facebook account, give Facebook consent, then return to this page and refresh it.