Arduino: How to Control a Digital Output (LED) Using a Digital Input

Controlling digital outputs with digital inputs is one of the most fundamental concepts in electronics and embedded programming. In this tutorial, we’ll walk through how to use a push button connected to pin 7 as an input, and an LED connected to pin 13 as an output, using an Arduino board.

 

First, we will control an LED with a Button Using an External Pull‑down Resistor

  • Digital Input (Pin 7): A push button is connected to pin 7. With a pull-down resistor, the pin stays LOW when the button is not pressed, and it goes HIGH when pressed.
  • Digital Output (Pin 13): An LED is connected to pin 13. The microcontroller can turn it ON (HIGH) or OFF (LOW) depending on the input state.
  • LED cannot be connected to Pin 13 directly; there must be a series resistor to protect it from over current.

This setup makes the LED respond directly to the button press.

 

Circuit Connections

  1. Button (Input on Pin 7):

·       Connect one leg of the push button to pin 7.

·       Connect the other leg to +5V.

·       Place a 10kΩ pull-down resistor between pin 7 and GND.

This ensures the pin reads LOW when the button is not pressed.

 

  1. LED (Output on Pin 13):

·       Connect the LED’s anode (+) to pin 13 through a 330Ω resistor.

·       Connect the LED’s cathode (-) to GND.

 

Arduino Code Example

// Pin configuration

const int inputPin = 7;    // Button connected to pin 7

const int outputPin = 13;  // LED connected to pin 13

 

void setup() {

  pinMode(inputPin, INPUT);   // Using external pull-down resistor

  pinMode(outputPin, OUTPUT); // Set pin 13 as output

}

 

void loop() {

  int buttonState = digitalRead(inputPin); // Read button state

 

  if (buttonState == HIGH) {

    // Button pressed (HIGH because of pull-down setup)

    digitalWrite(outputPin, HIGH); // Turn LED ON

  } else {

    // Button not pressed

    digitalWrite(outputPin, LOW);  // Turn LED OFF

  }

}



How It Works

  • The pull-down resistor keeps pin 7 LOW when the button is idle.
  • Pressing the button connects pin 7 to +5V, making it HIGH.
  • The Arduino sketch continuously checks the button state:

This simple loop demonstrates digital input/output control.

 

Key Takeaways

  • With a pull-down resistor, we can stabilize the input sensing.
  • Always use resistors with LED to prevent damage.
  • Pin 13 is convenient for beginners because many Arduino boards already have an onboard LED connected to it. But we used an external LED connected on Pin 13.

 



Now we will control the LED with the Button Using an External Pull‑Up Resistor

In this variation, we’ll use an external 10kΩ pull‑up resistor on pin 7 to keep the input stable. The LED remains connected to pin 13 as the output.

 

How External Pull‑Ups Work

  • A pull‑up resistor ties the input pin to +5V when the button is not pressed.
  • When the button is pressed, the pin is connected to GND, overriding the pull‑up and making the input LOW.
  • This means the logic is inverted compared to a pull‑down setup:
    • Button not pressed → HIGH
    • Button pressed → LOW

 

Circuit Connections

  1. Button (Input on Pin 7):
    • Connect one leg of the push button to pin 7.
    • Connect the other leg to GND.
    • Place a 10kΩ resistor between pin 7 and +5V (external pull‑up).
    • This ensures the pin reads HIGH when the button is idle.

 

  1. LED (Output on Pin 13):
    • Connect the LED’s anode (+) to pin 13 through a 330Ω resistor.
    • Connect the LED’s cathode (-) to GND.

 

Arduino Code Example

// Pin configuration

const int inputPin = 7;    // Button connected to pin 7

const int outputPin = 13;  // LED connected to pin 13

 

void setup() {

  pinMode(inputPin, INPUT);   // Using external pull-up resistor

  pinMode(outputPin, OUTPUT); // Set pin 13 as output

}

 

void loop() {

  int buttonState = digitalRead(inputPin); // Read button state

 

  if (buttonState == LOW) {

    // Button pressed (LOW because of pull-up setup)

    digitalWrite(outputPin, HIGH); // Turn LED ON

  } else {

    // Button not pressed

    digitalWrite(outputPin, LOW);  // Turn LED OFF

  }

} 





How It Works

  • The 10kΩ resistor keeps pin 7 HIGH when the button is idle.
  • Pressing the button connects pin 7 directly to GND, overriding the pull‑up and making it LOW.
  • The Arduino checks the input:
    • If LOW → LED turns ON.
    • If HIGH → LED turns OFF.

 



Now we will use Arduino's Inbuilt Pull-Up Resistor for Button Input

In the earlier version of the circuit, we used an external pull‑down & pull-up resistors to keep the input pin LOW & HIGH respectively when the button was not pressed. Arduino also provides a convenient alternative: internal pull‑up resistors.

 

How Inbuilt Pull‑Up Work

  • When you configure a pin as INPUT_PULLUP, Arduino internally connects a ~20kΩ–50kΩ resistor between the pin and +5V.
  • This means the pin naturally reads HIGH when idle.
  • Pressing the button connects the pin to GND, pulling it LOW.

So, the logic is inverted compared to the pull‑down setup:

  • Button not pressed → HIGH
  • Button pressed → LOW

 

Circuit Connections (Simplified)

  1. Button (Input on Pin 7):
    • Connect one leg of the push button to pin 7.
    • Connect the other leg to GND.
    • No external resistor is needed — Arduino’s internal pull‑up does the job.

 

  1. LED (Output on Pin 13):
    • Connect the LED’s anode (+) to pin 13 through a 330Ω resistor.
    • Connect the LED’s cathode (-) to GND.

 

Arduino Code Example

// Pin configuration

const int inputPin = 7;    // Button connected to pin 7

const int outputPin = 13;  // LED connected to pin 13

 

void setup() {

  pinMode(inputPin, INPUT_PULLUP); // Enable internal pull-up resistor

  pinMode(outputPin, OUTPUT);      // Set pin 13 as output

}

 

void loop() {

  int buttonState = digitalRead(inputPin); // Read button state

 

  if (buttonState == LOW) {

    // Button pressed (LOW because of pull-up setup)

    digitalWrite(outputPin, HIGH); // Turn LED ON

  } else {

    // Button not pressed

    digitalWrite(outputPin, LOW);  // Turn LED OFF

  }

}




Key Differences from Pull‑Down Setup

  • No external resistor required → simpler wiring.
  • Logic inversion → pressed = LOW, released = HIGH.
  • Noise immunity → internal pull‑ups are reliable for most button circuits.

 

Why Use Inbuilt Pull‑Ups?

  • Saves components → fewer resistors on your breadboard.
  • Cleaner wiring → easier for beginners and quick prototypes.
  • Standard practice → many Arduino tutorials and libraries assume pull‑ups.

 

With this, we now have three complete approaches for the same circuit:

  • External pull‑down resistor
  • Internal pull‑up resistor
  • External pull‑up resistor

 

Practical Notes

  • Internal pull‑ups are weaker (~20kΩ–50kΩ), but sufficient for button inputs.
  • For long wires or noisy environments, external resistors may still be preferred.
  • Always remember to invert your logic when switching between pull‑down and pull‑up setups.

  

Variations You Can Try

  • Toggle Mode: Instead of mirroring the button, make the LED toggle ON/OFF with each press (requires state tracking and debounce logic).
  • Debouncing: Mechanical buttons can cause multiple rapid signals. Add a small delay (delay(50)) or software debounce logic to stabilize input.
  • Multiple LEDs: Control several LEDs from different buttons, or use one button to cycle through LED patterns.
  • Sensors instead of buttons: Replace the button with a sensor (motion detector, light sensor) to automate LED control.
  • And so on…