Skip to content
Home » Guide » [Embedded AI] Linux Embedded Tutorial – Light up LED

[Embedded AI] Linux Embedded Tutorial – Light up LED

Lighting up an LED is one of the simplest yet most fundamental experiments in embedded development. While this may seem like a beginner’s task, it offers a great introduction to hardware interfaces, kernel drivers, and user-space programming. This article will guide you through the process of lighting up an LED via a Linux system, helping you gain hands-on experience with embedded development.

1. Hardware Preparation

For this tutorial, we’ll use the LKD3568 single-board computer from Neardi Technology. The board features the RK3568 embedded processor based on the ARM architecture. This processor is widely utilized in smart hardware and IoT devices, making it perfect for embedded development experiments.

The LKD3568 development board has 2 LED status indicators, so no additional hardware is required. The following table outlines the LEDs and their respective pin configurations:

LED

Pin NamePin Number
GreenGPIO0_B715
GreenGPIO4_C4148

You can control these LEDs either by using the LED device subsystem or by directly manipulating the GPIO pins.

2. Methods for Lighting the LED

2.1 Control LED via Command Line

2.1.1 Finding the LED Device

To locate the LED device path, we can use the following command:

 

				
					ls /sys/class/leds/
				
			

This will display the available LED devices on your system.

2.1.2 Controlling LED Brightness

You can control the LED’s brightness using the echo command. Be aware that to interact with device directories in Linux, you need superuser privileges.

Here are the commands to turn the LED on and off:

•To turn the LED on:

				
					sudo -s  # Gain superuser privileges
echo 0 > /sys/class/leds/work1/brightness
				
			

• To turn the LED off:

				
					sudo -s  # Gain superuser privileges
echo 1 > /sys/class/leds/work1/brightness
				
			

These commands change the brightness of the LED to simulate its state.

2.2 Writing a C Program to Control the LED

You can also control the LED using a C program. Here’s a simple C code example that demonstrates how to control the LED by manipulating the /sys/class/leds/work1/brightness file:

				
					#include <stdio.h>
#include <stdlib.h>

int main() {
    FILE *fp = fopen("/sys/class/leds/work1/brightness", "w");
    if (!fp) {
        perror("Failed to open brightness file");
        return -1;
    }
    fprintf(fp, "1");
    fclose(fp);
    printf("LED is turned on.\n");
    return 0;
}
				
			

To compile and run this program, use the following commands:

				
					gcc -o led led.c
./led
				
			

The program will turn the LED on by writing a value of “1” to the brightness file.

2.3 Control LED Using Python

For those who prefer Python, you can also control the LED using a Python script. Here’s an example Python script to toggle the LED on and off:

1.Create a Python file:

				
					touch led.py
				
			

2. Write the following Python code:

				
					# -*- coding: utf-8 -*-
import os
import time

def control_led(brightness):
    led_path = "/sys/class/leds/work1/brightness"  # LED path
    try:
        with open(led_path, "w") as fp:
            fp.write(str(brightness))  # Write brightness value
        print(f"LED is turned {'on' if brightness == 1 else 'off'}.")
    except IOError as e:
        print(f"Failed to open brightness file: {e}")
    except Exception as e:
        print(f"An error occurred: {e}")

if __name__ == "__main__":
    control_led(1)
    time.sleep(1)
    control_led(0)
    time.sleep(1)
    control_led(1)
    time.sleep(1)
    control_led(0)
    time.sleep(1)
				
			

3. Run the Python script with superuser privileges:

				
					sudo python3 led.py
				
			

This will turn the LED on and off at regular intervals.

3. Common Issues and Solutions

3.1 Permission Issues

If you encounter a “Permission denied” error while trying to control the LED, it’s most likely due to insufficient privileges. To resolve this, make sure to execute the commands with superuser privileges:

				
					sudo -s  # Gain superuser privileges
				
			

3.2 Device Not Recognized

If the LED device isn’t recognized by your system, you might need to check if the appropriate kernel driver is loaded. You can check the kernel log for relevant messages using this command:

				
					dmesg | grep -i led
				
			

If the device is not detected, ensure that the necessary kernel module for LED control is loaded.

4. References

For further reading and reference, you can check out these resources:

LKD3568 Datasheet