Section outline

  • 🚦 Our First Interaction – Button + LED

    Now that you’ve seen how to writre a code to blink an LED, let’s make things more interactive! In this section, we’ll build a simple circuit where pressing a button will turn an LED on or off. This introduces how inputs (like buttons) can control outputs (like LEDs).

    • 🔧 What You’ll Need:

      • Arduino UNO (or compatible board)
      • Breadboard
      • 1 LED (any color)
      • 1 Push Button
      • 1 Resistor (220Ω for LED)
      • 1 Resistor (10kΩ for button pull-down)
      • Jumper wires
    • 🧩 Step-by-Step Instructions:

      1. Connect the LED:
        • Connect the longer leg (anode) of the LED to digital pin 13 via a 220Ω resistor.
        • Connect the shorter leg (cathode) to the GND rail on the breadboard.
      2. Set up the button:
        • Place the button on the breadboard across the gap so it straddles the middle.
        • Connect one leg of the button to 5V on the Arduino.
        • Connect the opposite leg to digital pin 2 and also to GND via a 10kΩ resistor (this is called a pull-down resistor).

      🔌 Circuit Diagram:

      Arduino LED circuit Diagram

       
    • 💻 Sample Code:

      int ledPin = 13;
      int buttonPin = 2;
      int buttonState = 0;
      
      void setup() {
        pinMode(ledPin, OUTPUT);
        pinMode(buttonPin, INPUT);
      }
      
      void loop() {
        buttonState = digitalRead(buttonPin);
      
        if (buttonState == HIGH) {
          digitalWrite(ledPin, HIGH);
        } else {
          digitalWrite(ledPin, LOW);
        }
      }