# BalenaOS options to blinking LED connected to GPIO (On startup and runtime)

**URL:** <https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126>\
**Category:** balenaOS\
**Tags:** gpio, led, raspberrypi4\
**Created:** [October 12, 2023, 8:21am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126 "2023-10-12T08:21:08Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Shpatz](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shpatz](https://forums.balena.io/u/Shpatz)\
**Post date:** [October 12, 2023, 8:21am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/1 "2023-10-12T08:21:08Z")

</div>

Hi all,

We’re looking for a way to blink/auto toggle GPIOs directly from the kernel without using software loops/routines

Do you provide a way to control the GPIOs or the Device Tree to turn the GPIO to LED so I could use the trigger FD to achieve my request?

many thanks.

---

<div class="post-metadata">

**Author:** ![alanb128](https://sea2.discourse-cdn.com/balena/user_avatar/forums.balena.io/alanb128/32/2839_2.png) [@alanb128](https://forums.balena.io/u/alanb128)\
**Post date:** [October 13, 2023, 1:16am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/2 "2023-10-13T01:16:31Z")

</div>

Hello, which device type are you using? There is this for the Raspberry Pi: [GPIO Interface - Balena Documentation](https://docs.balena.io/learn/develop/hardware/gpio/#file-system-access) - Also see the kernel documentation referred to here: [https://www.kernel.org/doc/Documentation/gpio/sysfs.txt](https://www.kernel.org/doc/Documentation/gpio/sysfs.txt)

---

<div class="post-metadata">

**Author:** ![TJvV](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TJvV](https://forums.balena.io/u/TJvV)\
**Post date:** [October 13, 2023, 7:30am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/3 "2023-10-13T07:30:45Z")

</div>

Hi,

You may want to be more specific in what you intend to do.

If you want to do something from boot without software interaction, device tree overlays are probably the way to go.  
[gpio-led](https://github.com/raspberrypi/linux/blob/rpi-5.10.y/arch/arm/boot/dts/overlays/gpio-led-overlay.dts) suggest you can add an entry like `dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat` to your configuration.

---

<div class="post-metadata">

**Author:** ![Shpatz](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shpatz](https://forums.balena.io/u/Shpatz)\
**Post date:** [October 16, 2023, 9:38am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/4 "2023-10-16T09:38:23Z")

</div>

thanks for your replies,  
@alanb128 yes I’m aware of these documentations, but they’re not testify my needs.  
I need a way to turn a specific to behave like LED, so when I export it the OS will provide a “trigger” FD  
For example:  
/sys/class/gpio/gpio4/trigger

@TJvV  
What about after boot? say we want to change the blink rate of a GPIO in real time.  
Currently we are doing it using goroutines, but it would be much better by setting a GPIO FD with a dutycycle

---

<div class="post-metadata">

**Author:** ![TJvV](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TJvV](https://forums.balena.io/u/TJvV)\
**Post date:** [October 16, 2023, 11:20am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/5 "2023-10-16T11:20:48Z")

</div>

This should also give you an entry under `/sys/class/leds/` where you can manipulate the led.

According to the sources, you should also be able to set the trigger source to a [timer](https://github.com/raspberrypi/linux/blob/rpi-5.10.y/drivers/leds/trigger/ledtrig-timer.c) with a `delay_on` and `delay_off` in milliseconds.

Note I haven’t tried this myself yet; I hope to have some time to test things tomorrow.

---

<div class="post-metadata">

**Author:** ![TJvV](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TJvV](https://forums.balena.io/u/TJvV)\
**Post date:** [October 17, 2023, 8:15pm UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/6 "2023-10-17T20:15:18Z")

</div>

Coming back with a few findings,  
Tested this on a raspberrypi4 with balenaOS 4.0.26.

Adding the following to your configuration (“Define DT overlays”): `"gpio-led,label=test-led,gpio=26,trigger=timer"` gives you an entry `/sys/class/leds/test-led` that can be used to control GPIO 26. The default values for the timer trigger is 500ms on, 500ms off.

To change the frequency and duty cycle of the pulse, you can change `/sys/class/leds/test-led/delay_on` and `/sys/class/leds/test-led/delay_off`. (ex. `echo 1000 > /sys/class/leds/test-led/delay_on` to have it on for a full second)

If you want to hook it up to a GPIO input, you can change `/sys/class/leds/test-led/trigger` (or the trigger in the `dtoverlay` config) to `gpio` and write the relevant GPIO number to `/sys/class/leds/test-led/gpio`.

Sadly, I haven’t been successful in changing the timer delays, or the GPIO assignment in `config.txt` line (yet), only through sysfs after booting.

---

<div class="post-metadata">

**Author:** ![Shpatz](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shpatz](https://forums.balena.io/u/Shpatz)\
**Post date:** [October 22, 2023, 7:01pm UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/7 "2023-10-22T19:01:44Z")

</div>

Many thanks!

By implementing your solution we’re now controlling 3 LEDs which are connected to GPIOs.

I’ve tried many configurations trying to control `delay_on` and `delay_off` on boot, but without success.  
Still the solution is great.

---

<div class="post-metadata">

**Author:** ![Shpatz](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shpatz](https://forums.balena.io/u/Shpatz)\
**Post date:** [November 22, 2023, 9:48am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/9 "2023-11-22T09:48:20Z")

</div>

We still need a simple solution for “changing the timer delays on boot”

thanks

---

<div class="post-metadata">

**Author:** ![TJvV](https://avatars.discourse-cdn.com/v4/letter/t/858c86/32.png) [@TJvV](https://forums.balena.io/u/TJvV)\
**Post date:** [November 23, 2023, 8:17am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/10 "2023-11-23T08:17:42Z")

</div>

Hi,

I think your best bet for now is to simply change it when your container starts up.  
That’s where you have the most control.

it can be as simple as wrapping your current command in a shell script.  
Something like:

```auto
#!/bin/sh

LED_DIR=/sys/class/leds/test-led

if [-n "${led_on_ms}"]; then
  echo "${led_on_ms}" > ${LED_DIR}/delay_on
fi
if [-n "${led_off_ms}"]; then
  echo "${led_off_ms}" > ${LED_DIR}/delay_off
fi

<actual command> $@

```

This should set the `delay_on` and `delay_off` to values if specified in environment variables, and then call your actual command with the parameters you gave the script.

---

<div class="post-metadata">

**Author:** ![Shpatz](https://avatars.discourse-cdn.com/v4/letter/s/9de053/32.png) [@Shpatz](https://forums.balena.io/u/Shpatz)\
**Post date:** [November 23, 2023, 8:58am UTC](https://forums.balena.io/t/balenaos-options-to-blinking-led-connected-to-gpio-on-startup-and-runtime/369126/11 "2023-11-23T08:58:54Z")

</div>

Of course, we’re already doing that, but we need a solution for the time before the container starts up
