Introduction: Controlling LED by Button With Arduino Uno R3

About: PrimeRobotics is a E-Commerce site, which focus on supplying right products to Electronics Hobbyists, Enthusiast & Students.

In this experiment, you will learn how to turn on/off an LED by using an I/O port and a button. The "I/O port" refers to the INPUT and OUTPUT port. Here the INPUT port of the Arduino Uno board is to read the output of an external device. Since the board itself has an LED (connected to Pin 13), you can use the LED for convenience

Step 1: ​Components

- Arduino Uno board * 1

- USB cable * 1

- Resistor (10kΩ) * 1

- LED * 1

- Button * 1

- Capacitor 104 * 1

- Breadboard * 1

- Jumper wires

Step 2: ​Principle

Buttons are a common component used to control electronic devices. They are usually used as switches to connect or break circuits. Although buttons come in a variety of sizes and shapes, the one used here is a 6mm mini-button as shown in the following pictures.

Pin 1 is connected to pin 2 and pin 3 to pin 4. So you just need to connect either of pin 1 and pin 2 to pin 3 or pin 4.

Step 3:

The following is the internal structure of a button. The symbol on the right below is usually used to represent a button in circuits.

Step 4:

Since the pin 1 is connected to pin 2, and pin 3 to pin 4, when the button is pressed, the 4 pins are connected, thus closing the circuit.

Step 5: The Schematic Diagram

Step 6: ​Procedures

Connect one end of the buttons to pin 12 which connects with a pull-down resistor and a 0.1uF (104) capacitor (to eliminate jitter and output a stable level when the button is working). Connect the other end of the resistor to GND and one of the pins at the other end of the button to 5V. When the button is pressed, pin 12 is 5V (HIGH). Set the pin 12 as High level by programming and pin 13 (integrated with an LED) as High at the same time. Then release the button (pin 12 changes to LOW) and pin 13 is Low. So we will see the LED lights up and goes out alternately as the button is pressed and released.

Step1: Build the circuit.

Step 2:Download the code from

Step 3: Upload the sketch to the Arduino Uno board

Click the Upload icon to upload the code to the control board.

If "Done uploading" appears at the bottom of the window, it means the sketch has been successfully uploaded.

Now, press the button, and the LED on the SunFounder Uno board will light up.

Step 7: Code

//Controlling Led By Button

//Turns on and off a LED ,when pressings button attach to pin12




const int buttonPin = 12; //the button connect to pin 12

const int ledPin = 13;//the led connect to pin13


int buttonState = 0; // variable for reading the pushbutton status

void setup()


pinMode(buttonPin, INPUT); //initialize thebuttonPin as input

pinMode(ledPin, OUTPUT); //initialize the led pin as output



void loop()


//read the state of the button value

buttonState = digitalRead(buttonPin);

//and check if the key is pressed

//if it is,the state is HIGH

if (buttonState == HIGH )


digitalWrite(ledPin, HIGH); //turn the led on




digitalWrite(ledPin, LOW); //turn the led off




Step 8: Code Analysis

Code Analysis 1 Define variables

Connect the button to pin 12. LED has been connected to pin 13. Define a variable buttonState to restore the state of the button.

Code Analysis 2-2 Set the input and output status of the pins

We need to know the status of the button in this experiment, so here set the buttonPin as INPUT; to set HIGH/LOW of the LED, we set LedPin as OUTPUT.

Code Analysis 2-3 Read the status of the button

buttonPin(Pin12) is a digital pin; here is to read the value of the button and store it in buttonState.

digitalRead (Pin): Reads the value from a specified digital pin, either HIGH or LOW.

Code Analysis 2-4 Turn on the LED when the button is pressed

In this part, when the buttonState is High level, write ledPin as High and the LED will be turned on. As one end of the button has been connected to 5V and the other end to pin 12, when the button is pressed, pin 12 is 5V (HIGH). And then determine with the if(conditional); if the conditional is true, then the LED will light up.

Else means that when the if(conditional) is determined as false, run the code in else.