Skip to content

GPIO-Controlled LED Example Using libgpiod

This example demonstrates how to toggle an LED using the libgpiod API. While we use an LED for demonstration, GPIOs can also be used to control other devices by switching voltage levels (high/low). The code is sourced from the official libgpiod repository and adapted for practical use.

The libgpiod project provides a modern interface for interacting with GPIO lines in Linux, replacing the older sysfs-based GPIO interface which is now considered legacy. It offers both a C API and command-line tools for efficient GPIO control.

libgpiod official website

Refer to libgpiod project

Hardware Connection

This example utilizes the onboard blue LED which is connected to GPIO 83. No additional wiring is required.

Running the C Example

Clone the Example Code

git clone https://github.com/kneron/kneopi-examples.git
cd kneopi-examples
cd peripherals/C/gpio_led

Info

The code is sourced from official libgpiod repository

Build the Example

bash build.sh

Run the Example

./blink_led

ctrl+c to exit the example

Running the Python Example

Clone the Example Code

git clone https://github.com/kneron/kneopi-examples.git
cd kneopi-examples
cd peripherals/Python/gpio_led

Info

The code is sourced from official libgpiod repository

Run the Example

How to Use chmod to Make a Script Executable?

Before running the Python script, ensure it has execute permission.

You can grant permission using the command:

chmod +x toggle_line_value.py

Then, you can execute it directly by:

./toggle_line_value.py

If you prefer running it with python3, you can still use:

python toggle_line_value.py

./toggle_line_value.py