Tutorial 18 – Using a Button to Control an LED

Welcome back to CraftedTech Engineering's Arduino tutorial series! In this lesson, we will explore how to use a pushbutton to control an LED. This is a simple project that will introduce you to input devices and help you understand how to interact with the physical world using your Arduino.


🔌 What You’ll Need

  • 1 Arduino Board (e.g., Arduino UNO)
  • 1 LED (any color)
  • 1 220Ω Resistor
  • 1 Pushbutton
  • 1 10kΩ Resistor
  • Breadboard
  • Jumper wires
  • USB cable for Arduino

You can order the required components here:


🧠 What Is a Button-Controlled LED?

In this tutorial, you will wire a button to your Arduino and use it to turn an LED on and off. This exercise teaches you about digital input and digital output in Arduino, providing the foundation for interactive projects.


🧾 Step-by-Step Instructions

🔧 Step 1: Wiring the Button and LED

  • Connect the long leg (anode) of the LED to digital pin 13 on the Arduino.
  • Connect the short leg (cathode) of the LED to one end of the 220Ω resistor.
  • Connect the other end of the resistor to GND (ground) on the Arduino.
  • For the button:
    1. Connect one leg of the pushbutton to digital pin 7 on the Arduino.
    2. Connect the other leg to GND.
    3. Connect a 10kΩ resistor between digital pin 7 and VCC (5V) to act as a pull-up resistor.

💻 Step 2: Arduino Code to Control the LED with a Button


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

🔍 Code Breakdown

  • pinMode(): Sets the LED pin as an output and the button pin as an input.
  • digitalRead(): Reads the state of the button (either HIGH or LOW).
  • digitalWrite(): Turns the LED on (HIGH) or off (LOW) based on the button state.

🧪 Try This!

  • Add multiple buttons to control different LEDs, and experiment with turning them on and off based on button presses.
  • Try adding debouncing to the button input to make it more responsive.

🛠️ Troubleshooting Tips

  • Ensure the button wiring is correct (especially the pull-up resistor).
  • Make sure your button is not stuck in the pressed position.
  • If the LED stays on or off regardless of the button press, check the pin assignments and the wiring of the button.

🧭 What You Learned in This Tutorial

✔️ How to wire and use a pushbutton with Arduino
✔️ How to control an LED based on button input
✔️ The basics of digital input and digital output
✔️ How to use pull-up resistors in digital circuits


📌 Next Up: Tutorial 19 – Using a Potentiometer for Analog Input

In the next tutorial, we’ll learn how to use a potentiometer (variable resistor) to control an analog input value and use it in a project.


📣 Don’t forget to follow CraftedTech Engineering for more hands-on tutorials and project ideas!

🔗 Website: craftedtechengineering.com
📘 Facebook: @CraftedTechEngineering
📺 YouTube: @CraftedTechEngineering

Back to blog

Leave a comment