Go back

Button-Controlled LED: Your First Arduino Input Project

Introduction

Welcome to your first Arduino input project! In this tutorial, you’ll build a circuit where a pushbutton controls an LED. When you press the button, the LED lights up; release it, and the LED turns off. This simple project introduces you to reading digital inputs, using internal pull-up resistors, and writing clean Arduino code.

We’ll use the Velxio in-browser simulator, which provides a SPICE-accurate environment so you can test your circuit without any hardware. Let’s dive in!

Circuit Walkthrough

Circuit diagram of button-led on the Velxio simulator canvas

The circuit consists of three main components:

Wiring Details

  1. Button to Arduino:

    • One leg of the button (pin 1.l) connects to Arduino digital pin 2 via a blue wire (#00aaff).
    • The other leg (pin 2.l) connects to GND via a black wire.
  2. LED to Arduino:

    • The anode (long leg, pin A) connects to Arduino digital pin 13 via a red wire (#ff0000).
    • The cathode (short leg, pin C) connects to GND via a black wire.

Notice that the button does not have an external pull-up resistor. Instead, we’ll use the Arduino’s internal pull-up resistor in the code. When the button is not pressed, the input pin is pulled HIGH; when pressed, it reads LOW.

Code Walkthrough

Source code for button-led in the Velxio Monaco editor

Here’s the complete Arduino sketch:

// Button LED Control
// Press button to turn LED on

const int BUTTON_PIN = 2;
const int LED_PIN = 13;

void setup() {
  pinMode(BUTTON_PIN, INPUT_PULLUP);
  pinMode(LED_PIN, OUTPUT);
}

void loop() {
  int buttonState = digitalRead(BUTTON_PIN);

  if (buttonState == LOW) {
    digitalWrite(LED_PIN, HIGH);
  } else {
    digitalWrite(LED_PIN, LOW);
  }
}

Explanation

Key Concepts

Digital Input and Pull-Up Resistors

When a digital pin is configured as an input, it reads either HIGH (5V) or LOW (0V). Without a pull-up or pull-down resistor, the pin can float between states, causing erratic readings. The internal pull-up resistor (typically 20k-50k ohms) ensures a stable HIGH when the button is open. Pressing the button connects the pin directly to GND, overriding the pull-up and giving a LOW reading.

Debouncing

Mechanical buttons often bounce, causing multiple rapid transitions. In this simple example, we ignore debouncing because the LED responds quickly and the effect is minimal. For more critical applications, you can add a small delay or use a debounce library.

Using Constants

Defining pin numbers as constants at the top of the sketch makes it easy to change wiring without hunting through the code. It also improves readability.

Common Pitfalls and Debugging Tips

Using the Velxio Simulator

Velxio editor toolbar with compile and run controls

Click the Run button in the toolbar to compile and start the simulation. You can then click the pushbutton on the canvas to see the LED respond.

Velxio canvas toolbar above the simulation viewport

Use the canvas toolbar to pause, reset, or adjust simulation speed.

Velxio component picker modal browsing parts by category

To add more components, open the component picker and browse categories like “Input” or “Output”.

Velxio Library Manager modal listing installed Arduino libraries

If you need additional libraries (e.g., for debouncing), use the Library Manager.

Suggested Extensions

Once you have the basic circuit working, try these enhancements:

  1. Toggle mode: Modify the code so each press toggles the LED on/off instead of holding it.
  2. Multiple LEDs: Add more LEDs and buttons to create a simple input panel.
  3. Debounce: Implement software debouncing using a 50ms delay or a timer.
  4. Serial monitor: Print the button state to the Serial Monitor for debugging.
  5. PWM brightness: Replace the digital write with analogWrite() to control LED brightness based on button press duration.

Call to Action

Ready to build your own button-controlled LED? Open the live example on Velxio and start experimenting: Button LED Example. No hardware needed – just your browser. Happy tinkering!


Share this post on:
David Montero

Written by

David Montero

Creator of Velxio, the open-source circuit and Arduino simulator.

GitHub velxio.dev

Related posts


Previous Post
Fade an LED with PWM on Arduino – Beginner Tutorial
Next Post
Build a Traffic Light Simulator with Arduino on Velxio