0
0
Power-electronicsProgramBeginner · 2 min read

Embedded C Program to Control LED with Button

Use if (button_pin == PRESSED) { led_pin = ON; } inside a loop to turn on an LED when a button is pressed, and turn it off otherwise.
📋

Examples

InputButton pressed
OutputLED turns ON
InputButton not pressed
OutputLED stays OFF
InputButton pressed then released
OutputLED turns ON then OFF
🧠

How to Think About It

To control an LED with a button, first read the button's state continuously. When the button is pressed (input is active), set the LED pin to high to turn it on. When the button is not pressed, set the LED pin low to turn it off. This simple check inside a loop creates real-time control.
📐

Algorithm

1
Initialize LED pin as output and button pin as input
2
Start an infinite loop
3
Read the button pin state
4
If button is pressed, set LED pin high
5
Else, set LED pin low
6
Repeat the loop
💻

Code

embedded_c
#include <avr/io.h>
#define BUTTON_PIN PD2
#define LED_PIN PD3

int main(void) {
    DDRD &= ~(1 << BUTTON_PIN); // Set button pin as input
    DDRD |= (1 << LED_PIN);      // Set LED pin as output

    while (1) {
        if (PIND & (1 << BUTTON_PIN)) { // Button pressed (assuming active high)
            PORTD |= (1 << LED_PIN);     // Turn LED ON
        } else {
            PORTD &= ~(1 << LED_PIN);    // Turn LED OFF
        }
    }
    return 0;
}
Output
LED turns ON when button pressed, OFF when released
🔍

Dry Run

Let's trace pressing and releasing the button through the code

1

Initialize pins

BUTTON_PIN (PD2) set as input, LED_PIN (PD3) set as output

2

Button pressed

PIND & (1 << BUTTON_PIN) is true, so LED_PIN set high, LED turns ON

3

Button released

PIND & (1 << BUTTON_PIN) is false, so LED_PIN cleared, LED turns OFF

StepButton Pin StateLED Pin StateLED Status
1Input mode setOutput mode setLED OFF initially
2Pressed (1)Set high (1)LED ON
3Released (0)Set low (0)LED OFF
💡

Why This Works

Step 1: Pin Setup

We set the button pin as input to read its state and the LED pin as output to control the LED.

Step 2: Reading Button State

Inside the loop, we check if the button pin reads high (pressed) using PIND & (1 << BUTTON_PIN).

Step 3: Controlling LED

If pressed, we set the LED pin high to turn it on; otherwise, we clear it to turn the LED off.

🔄

Alternative Approaches

Using pull-up resistor and active low button
embedded_c
#include <avr/io.h>
#define BUTTON_PIN PD2
#define LED_PIN PD3

int main(void) {
    DDRD &= ~(1 << BUTTON_PIN); // Button pin input
    PORTD |= (1 << BUTTON_PIN); // Enable pull-up resistor
    DDRD |= (1 << LED_PIN);     // LED pin output

    while (1) {
        if (!(PIND & (1 << BUTTON_PIN))) { // Button pressed (active low)
            PORTD |= (1 << LED_PIN);      // LED ON
        } else {
            PORTD &= ~(1 << LED_PIN);     // LED OFF
        }
    }
    return 0;
}
This method uses internal pull-up resistor and assumes button connects pin to ground when pressed.
Using interrupt on button press
embedded_c
// Interrupt-based LED control example omitted for brevity
Using interrupts can save CPU cycles but is more complex to implement.

Complexity: O(1) time, O(1) space

Time Complexity

The program runs in a continuous loop checking button state, which is a constant time operation each cycle.

Space Complexity

Uses fixed memory for registers and variables; no extra memory allocation.

Which Approach is Fastest?

Polling the button in a loop is simple and fast for small programs; interrupt-based methods can be more efficient for complex systems.

ApproachTimeSpaceBest For
Polling in loopO(1)O(1)Simple projects, easy to understand
Using pull-up resistorO(1)O(1)Reliable button input without external resistor
Interrupt-basedO(1)O(1)Power saving and responsive in complex systems
💡
Always configure button pins with proper pull-up or pull-down resistors to avoid floating inputs.
⚠️
Beginners often forget to set the button pin as input or miss enabling pull-up resistors, causing unreliable button reads.