Home Forums MOCHAbin Forum Hardware discussions Tricolor LEDs – how to? Reply To: Tricolor LEDs – how to?

#50512
dhliu
Keymaster

technosf was right,

1. There are three tri-color LEDs as led1, led2 and led3 with red, green and blue color each
You may find 9 sub-directories under /sys/class/leds, they are:

Led1:red
Led1:green
Led1:blue
Led2:red
Led2:green
Led2:blue
Led3:red
Led3:green
Led3:blue

Under the shell, give value 0 to 255 as brightness to the associated led
For example:
echo 100 > /sys/class/leds/led3\:red/brightness
echo 100 > /sys/class/leds/led1\:blue/brightness

The script below will display blue green cyna red magenta yellow white from LED1 to LED3 then turn off

#!/bin/bash

function rgbled {
local idx=$1
local color=$2
local r=0
local g=0
local b=0

case “$color” in
black)
r=0; g=0; b=0;
;;
blue)
r=0; g=0; b=255;
;;
green)
r=0; g=255; b=0;
;;
cyna)
r=0; g=255; b=255;
;;
red)
r=255; g=0; b=0;
;;
magenta)
r=255; g=0; b=255;
;;
yellow)
r=255; g=255; b=0;
;;
white)
r=255; g=255; b=255;
;;
*)
r=0; g=0; b=0;
;;
esac

echo $r > /sys/class/leds/led${idx}\:red/brightness
echo $g > /sys/class/leds/led${idx}\:green/brightness
echo $b > /sys/class/leds/led${idx}\:blue/brightness
}

# turn off all leds
for led in {1..3};
do
rgbled $led
done

# leds demo
for color in blue green cyna red magenta yellow white;
do
for led in {1..3};
do
rgbled $led $color
sleep 0.3
done

sleep 1
for led in {1..3};
do
rgbled $led
done
sleep 0.3
done

Signup to our newsletter

Technical specification tables can not be displayed on mobile. Please view on desktop