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

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.

Hello, which device type are you using? There is this for the Raspberry Pi: GPIO Interface - Balena Documentation - Also see the kernel documentation referred to here: https://www.kernel.org/doc/Documentation/gpio/sysfs.txt

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 suggest you can add an entry like dtoverlay=gpio-led,gpio=19,label=heart,trigger=heartbeat to your configuration.

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

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 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.

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.

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.

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

thanks

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:

#!/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.

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