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

The circuit consists of three main components:
- Arduino Uno – the brain of the operation.
- Pushbutton – a momentary switch that connects two pins when pressed.
- Red LED – the output device.
Wiring Details
-
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.
- One leg of the button (pin
-
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.
- The anode (long leg, pin
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

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
- Constants:
BUTTON_PINis set to 2,LED_PINto 13. Using constants makes the code easier to read and modify. setup():pinMode(BUTTON_PIN, INPUT_PULLUP)configures pin 2 as an input with the internal pull-up resistor enabled. This means the pin reads HIGH by default, and LOW when the button is pressed (connecting it to GND).pinMode(LED_PIN, OUTPUT)sets pin 13 as an output to drive the LED.
loop():digitalRead(BUTTON_PIN)reads the button state. If it’s LOW (button pressed), we turn the LED on withdigitalWrite(LED_PIN, HIGH). Otherwise, we turn it off.
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
- Button wiring: Ensure one leg goes to the Arduino pin and the other to GND. If you connect both legs to the same row, the button won’t work.
- LED polarity: The anode (long leg) must connect to the digital pin, and the cathode (short leg) to GND. Reversing them won’t damage the LED, but it won’t light up.
- Pull-up not enabled: If you forget
INPUT_PULLUPand use plainINPUT, the pin will float and the LED may flicker randomly. - Wrong pin numbers: Double-check that your code matches the wiring. In this example, button on pin 2, LED on pin 13.
Using the Velxio Simulator

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.

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

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

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:
- Toggle mode: Modify the code so each press toggles the LED on/off instead of holding it.
- Multiple LEDs: Add more LEDs and buttons to create a simple input panel.
- Debounce: Implement software debouncing using a 50ms delay or a timer.
- Serial monitor: Print the button state to the Serial Monitor for debugging.
- 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!