0
0
Embedded Cprogramming~5 mins

LED-based debugging patterns in Embedded C

Choose your learning style9 modes available
Introduction

LED-based debugging helps you see what your program is doing by making lights blink in certain ways. It is useful when you cannot use a screen or a computer to check your program.

When your embedded device has no display or serial output.
When you want a quick way to check if a part of your code runs.
When you want to show error codes or status using simple light signals.
When debugging hardware that is hard to connect to a computer.
When you want to confirm that your program reached a certain point.
Syntax
Embedded C
void led_on();
void led_off();
void delay_ms(int milliseconds);

// Example pattern function
void blink_led(int times, int delay_time) {
    for (int i = 0; i < times; i++) {
        led_on();
        delay_ms(delay_time);
        led_off();
        delay_ms(delay_time);
    }
}

Use functions like led_on() and led_off() to control the LED.

Use delays to make the blinking visible to the human eye.

Examples
Basic functions to control LED and wait.
Embedded C
void led_on() {
    // Turn on LED hardware pin
}

void led_off() {
    // Turn off LED hardware pin
}

void delay_ms(int ms) {
    // Wait for ms milliseconds
}
Blinks the LED once with half a second on and off.
Embedded C
void blink_once() {
    led_on();
    delay_ms(500);
    led_off();
    delay_ms(500);
}
Blinks the LED a number of times equal to an error code, then pauses.
Embedded C
void blink_error_code(int code) {
    for (int i = 0; i < code; i++) {
        led_on();
        delay_ms(200);
        led_off();
        delay_ms(200);
    }
    delay_ms(1000); // Pause before repeating
}
Sample Program

This program simulates blinking an LED 3 times to show an error code. It prints messages to show when the LED is on or off.

Embedded C
#include <stdio.h>
#include <unistd.h> // For usleep

void led_on() {
    printf("LED ON\n");
}

void led_off() {
    printf("LED OFF\n");
}

void delay_ms(int ms) {
    usleep(ms * 1000); // Sleep for ms milliseconds
}

void blink_error_code(int code) {
    for (int i = 0; i < code; i++) {
        led_on();
        delay_ms(200);
        led_off();
        delay_ms(200);
    }
    delay_ms(1000); // Pause before repeating
}

int main() {
    int error_code = 3;
    printf("Starting LED error code blink pattern...\n");
    blink_error_code(error_code);
    return 0;
}
OutputSuccess
Important Notes

LED blinking speed should be slow enough to see clearly (usually 200-500 ms).

Use different blink patterns to represent different statuses or errors.

Remember to add pauses between patterns so they are easy to distinguish.

Summary

LED-based debugging uses blinking lights to show program status.

Simple on/off patterns can represent errors or checkpoints.

This method is useful when no screen or serial output is available.