How to turn on an LED using a button with Arduino

In this tutorial, we will learn how to control an LED using a push button connected to an Arduino Uno. We will also explore the use of resistors: how to choose them, their function, and how to read their color codes.

 📝 Required components

  • 1 x Arduino Uno (or compatible board)

  • Push button
  • LED (any color)
  • 220Ω resistor (for the LED)
  • 10kΩ resistor
  • Jumper wires

  • Breadboard 

Step 1: Wiring the Electronics 

  1. Connect the LED’s anode (longer leg) to Arduino pin 4 through a 220Ω resistor.
  2. Connect the cathode (shorter leg) of the LED to GND.
  3. Connect one leg of the button to Arduino digital pin 2.
  4. Connect the other leg of the button to GND through a 10kΩ resistor (pull-down).
  5. Now connect the bottom right leg directly to 5V. 
     

The 220Ω resistor limits the current going through the LED to prevent it from burning out. The 10kΩ resistor ensures that the button pin reads LOW when the button is not pressed.


 

🖥️​Step 2: Writing the Code 

Below you will find the complete code to make the LED turn on when the button is pressed:

 Complete Arduino code

This is the complete code to use the button with Arduino:

				
					const int buttonPin = 7;      // Pin connected to the button 
const int ledPin = 13;        // Pin connected to the LED 
 
int buttonState = 0;          // Variable to store button state 
 
void setup() { 
  pinMode(buttonPin, INPUT);  // Set button pin as input 
  pinMode(ledPin, OUTPUT);    // Set LED pin as output 
} 
 
void loop() { 
  buttonState = digitalRead(buttonPin); 
 
  if (buttonState == HIGH) { 
    digitalWrite(ledPin, HIGH); // Turn LED on 
  } else { 
    digitalWrite(ledPin, LOW);  // Turn LED off 
  } 
} 

				
			

🔎 Breaking down the Code💣

Let’s review the code section by section:

				
					buttonPin
				
			

 Is used to detect the button press.

				
					ledPin
				
			

 Controls the LED.

				
					pinMode()
				
			

 Sets up the pins as input or output.

				
					digitalRead()
				
			

 Checks if the button is pressed.

				
					digitalWrite()
				
			

Turns the LED on or off based on the button’s state. 

Understanding Resistors 

Resistors limit the current in a circuit. For LEDs, a 220Ω resistor ensures safe current levels. For buttons, a 10kΩ pull-down resistor ensures the signal is stable (LOW when not pressed). 

Resistor Color Code

Here’s how to read the color bands on resistors: 

  • 220Ω: Red (2), Red (2), Brown (×10) = 220Ω
  • 10kΩ: Brown (1), Black (0), Orange (×1,000) = 10,000Ω

🛠️​Step 3: Troubleshooting 

 

LED does not light up:
  • Check the polarity of the LED (long leg to positive). 
  • Make sure the resistor is not too high in value. 
  • Ensure the button is wired correctly and the code uses the correct pins. 
es_ES