Bird
Raised Fist0
Arduinoprogramming~10 mins

Multiple LED and button control in Arduino - Step-by-Step Execution

Choose your learning style10 modes available

Start learning this pattern below

Jump into concepts and practice - no test required

or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Concept Flow - Multiple LED and button control
Start Setup
Initialize LEDs and Buttons pins
Enter Loop
Read Button States
Check Each Button
Button1?
Turn ON corresponding LED
Turn OFF LEDs if buttons not pressed
Repeat Loop
The program sets up multiple LEDs and buttons, then continuously reads button states to turn on or off the matching LEDs.
Execution Sample
Arduino
const int ledPins[] = {2, 3, 4};
const int buttonPins[] = {5, 6, 7};

void setup() {
  for (int i=0; i<3; i++) {
    pinMode(ledPins[i], OUTPUT);
    pinMode(buttonPins[i], INPUT_PULLUP);
  }
}

void loop() {
  for (int i=0; i<3; i++) {
    if (digitalRead(buttonPins[i]) == LOW) {
      digitalWrite(ledPins[i], HIGH);
    } else {
      digitalWrite(ledPins[i], LOW);
    }
  }
}
This code reads three buttons and turns on the corresponding LED when a button is pressed.
Execution Table
Stepi (index)Button Pin ReadButton StateLED Pin ActionLED State
10digitalRead(5)LOW (pressed)digitalWrite(2, HIGH)LED 2 ON
21digitalRead(6)HIGH (not pressed)digitalWrite(3, LOW)LED 3 OFF
32digitalRead(7)HIGH (not pressed)digitalWrite(4, LOW)LED 4 OFF
40digitalRead(5)LOW (pressed)digitalWrite(2, HIGH)LED 2 ON
51digitalRead(6)LOW (pressed)digitalWrite(3, HIGH)LED 3 ON
62digitalRead(7)HIGH (not pressed)digitalWrite(4, LOW)LED 4 OFF
70digitalRead(5)HIGH (not pressed)digitalWrite(2, LOW)LED 2 OFF
81digitalRead(6)LOW (pressed)digitalWrite(3, HIGH)LED 3 ON
92digitalRead(7)LOW (pressed)digitalWrite(4, HIGH)LED 4 ON
Exit----Loop repeats indefinitely
💡 The loop runs forever, continuously checking button states and updating LEDs.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 5After Step 7Final
iundefined01210loops 0 to 2 repeatedly
buttonPins[i][5,6,7]56765cycles through 5,6,7
digitalRead(buttonPins[i])undefinedLOWHIGHHIGHLOWHIGHvaries per step
ledPins[i][2,3,4]23432cycles through 2,3,4
LED StateOFFONOFFOFFONOFFchanges per button state
Key Moments - 3 Insights
Why does the LED turn ON when the button state is LOW?
Because the buttons use INPUT_PULLUP mode, pressing the button connects the pin to ground, making the reading LOW. The code turns the LED ON when it reads LOW (pressed). See execution_table rows 1 and 5.
Why do we use a loop to check buttons and LEDs?
The loop lets the program check each button and control each LED one by one repeatedly. This way, all buttons are monitored continuously. See variable_tracker for how 'i' cycles through 0 to 2.
What happens if no button is pressed?
All buttons read HIGH, so the code turns all LEDs OFF. See execution_table rows 2 and 3 where LEDs 3 and 4 are OFF.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at Step 1. What is the state of LED connected to pin 2?
ABlinking
BON
COFF
DUndefined
💡 Hint
Check the 'LED State' column at Step 1 in execution_table.
At which step does the button on pin 6 first read LOW (pressed)?
AStep 7
BStep 2
CStep 5
DStep 9
💡 Hint
Look at the 'Button State' column for button pin 6 in execution_table.
If button on pin 5 is never pressed, what would happen to LED on pin 2?
AIt stays OFF
BIt stays ON
CIt blinks
DIt toggles randomly
💡 Hint
Refer to key_moments about button LOW means pressed and LED ON.
Concept Snapshot
Multiple LED and button control in Arduino:
- Use arrays for LED and button pins
- Setup pins: LEDs as OUTPUT, buttons as INPUT_PULLUP
- In loop, read each button
- If button pressed (LOW), turn ON matching LED
- Else, turn OFF LED
- Loop runs forever checking all buttons
Full Transcript
This Arduino program controls multiple LEDs using multiple buttons. It sets up three LEDs on pins 2, 3, and 4, and three buttons on pins 5, 6, and 7. Each button is configured with INPUT_PULLUP mode, so pressing the button connects the pin to ground and reads LOW. The loop reads each button's state one by one. If a button is pressed (reads LOW), the program turns ON the corresponding LED. If not pressed (reads HIGH), it turns the LED OFF. This process repeats forever, so the LEDs always reflect the current button states. The execution table shows step-by-step how the program reads buttons and controls LEDs. The variable tracker shows how the index 'i' cycles through 0 to 2 to handle all buttons and LEDs. Key moments clarify why LOW means pressed and why the loop is needed. The visual quiz tests understanding of LED states and button readings at different steps.

Practice

(1/5)
1. What is the main advantage of using arrays to control multiple LEDs and buttons in Arduino?
easy
A. It allows controlling many LEDs and buttons efficiently with loops.
B. It makes the LEDs brighter.
C. It reduces the power consumption of the Arduino.
D. It automatically fixes wiring errors.

Solution

  1. Step 1: Understand arrays usage

    Arrays store multiple pin numbers for LEDs and buttons in one place.
  2. Step 2: Use loops for control

    Loops can iterate over arrays to read buttons and control LEDs easily.
  3. Final Answer:

    It allows controlling many LEDs and buttons efficiently with loops. -> Option A
  4. Quick Check:

    Arrays + loops = efficient control [OK]
Hint: Think how arrays help repeat actions for many pins [OK]
Common Mistakes:
  • Thinking arrays make LEDs brighter
  • Confusing arrays with power saving
  • Assuming arrays fix wiring automatically
2. Which Arduino pin mode is best to use for buttons to avoid external resistors?
easy
A. OUTPUT
B. INPUT_PULLUP
C. INPUT
D. ANALOG

Solution

  1. Step 1: Recall button wiring

    Buttons need a way to avoid floating input pins, usually with pull-up resistors.
  2. Step 2: Use built-in pull-up

    INPUT_PULLUP mode activates Arduino's internal pull-up resistor, no external resistor needed.
  3. Final Answer:

    INPUT_PULLUP -> Option B
  4. Quick Check:

    Button mode = INPUT_PULLUP [OK]
Hint: Use INPUT_PULLUP to simplify button wiring [OK]
Common Mistakes:
  • Using OUTPUT mode for buttons
  • Forgetting pull-up resistor causes unstable reads
  • Using ANALOG mode for digital buttons
3. What will be the output on the LEDs if the following code runs and button 2 is pressed?
const int buttonPins[] = {2, 3, 4};
const int ledPins[] = {8, 9, 10};

void setup() {
  for (int i = 0; i < 3; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  for (int i = 0; i < 3; i++) {
    if (digitalRead(buttonPins[i]) == LOW) {
      digitalWrite(ledPins[i], HIGH);
    } else {
      digitalWrite(ledPins[i], LOW);
    }
  }
}
medium
A. Only LED on pin 8 turns ON.
B. All LEDs turn ON.
C. Only LED on pin 9 turns ON.
D. No LEDs turn ON.

Solution

  1. Step 1: Identify button pressed

    Button 2 is pressed, which is buttonPins[1] (pin 3).
  2. Step 2: Match LED to button

    LED on ledPins[1] (pin 9) turns ON because buttonPins[1] is LOW.
  3. Final Answer:

    Only LED on pin 9 turns ON. -> Option C
  4. Quick Check:

    Button 2 pressed lights LED 9 [OK]
Hint: Match button index to LED index in arrays [OK]
Common Mistakes:
  • Assuming all LEDs turn on when one button pressed
  • Confusing pin numbers with array indexes
  • Ignoring INPUT_PULLUP logic (LOW means pressed)
4. The following code is intended to turn ON an LED when its matching button is pressed, but the LED never lights up. What is the error?
const int buttonPins[] = {2, 3};
const int ledPins[] = {8, 9};

void setup() {
  for (int i = 0; i <= 2; i++) {
    pinMode(buttonPins[i], INPUT_PULLUP);
    pinMode(ledPins[i], OUTPUT);
  }
}

void loop() {
  for (int i = 0; i <= 2; i++) {
    if (digitalRead(buttonPins[i]) == LOW) {
      digitalWrite(ledPins[i], HIGH);
    } else {
      digitalWrite(ledPins[i], LOW);
    }
  }
}
medium
A. digitalRead should be replaced with analogRead.
B. Buttons should be set as OUTPUT, not INPUT_PULLUP.
C. LED pins should be set as INPUT.
D. The for loops use <= 2 instead of < 2, causing out-of-bounds access.

Solution

  1. Step 1: Check loop conditions

    Loops run from i = 0 to i <= 2, which means i = 0,1,2 (3 iterations).
  2. Step 2: Check array sizes

    buttonPins and ledPins have only 2 elements (indexes 0 and 1). Accessing index 2 causes error.
  3. Final Answer:

    The for loops use <= 2 instead of < 2, causing out-of-bounds access. -> Option D
  4. Quick Check:

    Loop index out of range causes failure [OK]
Hint: Use < array length, not <=, in loops over arrays [OK]
Common Mistakes:
  • Using <= instead of < in loops
  • Setting button pins as OUTPUT
  • Confusing digitalRead with analogRead
5. You want to control 4 LEDs with 4 buttons so that pressing a button toggles its LED ON or OFF (not just ON while pressed). Which approach is best to implement this behavior?
hard
A. Use arrays for pins and track LED states in a separate boolean array, toggling state on button press detection.
B. Use digitalWrite to turn LED ON when button is pressed and OFF otherwise.
C. Use INPUT mode for buttons and analogWrite for LEDs.
D. Connect LEDs directly to buttons without Arduino code.

Solution

  1. Step 1: Understand toggle behavior

    Toggle means LED changes state only when button is pressed, not continuously.
  2. Step 2: Track LED states

    Store LED ON/OFF states in a boolean array and change state only on button press event.
  3. Step 3: Use arrays for pins

    Arrays help manage multiple buttons and LEDs efficiently with loops.
  4. Final Answer:

    Use arrays for pins and track LED states in a separate boolean array, toggling state on button press detection. -> Option A
  5. Quick Check:

    Toggle needs state tracking + arrays [OK]
Hint: Track LED states in array to toggle on button press [OK]
Common Mistakes:
  • Turning LED ON only while button pressed
  • Using analogWrite for simple ON/OFF LEDs
  • Wiring LEDs directly to buttons without control