CM Home technical documentation Buy

RGB LEDs

This article illustrates how to use the RGB LEDs available on the CM-Home board

GPIO lines used:

  • GPIO 36 Red 0=on
  • GPIO 35 Green 0=on
  • GPIO 34 Blue 0=on

Python example

import RPi.GPIO as GPIO
import time

RED_LED=36
GREEN_LED=35
BLUE_LED=34

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(RED_LED,GPIO.OUT)
GPIO.setup(GREEN_LED,GPIO.OUT)
GPIO.setup(BLUE_LED,GPIO.OUT)

for i in range(5):
    GPIO.output(RED_LED,GPIO.LOW)
    GPIO.output(GREEN_LED,GPIO.HIGH)
    GPIO.output(BLUE_LED,GPIO.HIGH)
    time.sleep(0.5)

    GPIO.output(RED_LED,GPIO.HIGH)
    GPIO.output(GREEN_LED,GPIO.LOW)
    GPIO.output(BLUE_LED,GPIO.HIGH)
    time.sleep(0.5)

    GPIO.output(RED_LED,GPIO.HIGH)
    GPIO.output(GREEN_LED,GPIO.HIGH)
    GPIO.output(BLUE_LED,GPIO.LOW)
    time.sleep(0.5)

GPIO.output(RED_LED,GPIO.HIGH)
GPIO.output(GREEN_LED,GPIO.HIGH)
GPIO.output(BLUE_LED,GPIO.HIGH)

Define the LED behaviour with a dts overlay

Using a dts overlay and the Linux Kernel functions it is possible to define a set of standard behaviour to each color of the on-board RGB led.

In this example we will define the following funcions;

  • Green led = Heartbeat. The blink speed of this color depends from the CPU load
  • Red led = MMC0. The red will blink on any writing access to the MicroSD
  • Led blu = Simply off

Save this ASCII content on a file named cm3-home-leds.dts:

/dts-v1/;
/plugin/;
/ {
    compatible = "brcm,bcm2708";

    fragment@0 {
        target = <&leds>;
        __overlay__ {

            rgb_red: rgb_red {
                label = "rgb_red";
                gpios = <&gpio 36 1>;
                linux,default-trigger = "mmc0";
            };


            rgb_green: rgb_green {
                label = "rgb_green";
                gpios = <&gpio 35 1>;
                linux,default-trigger = "heartbeat";
            };

            rgb_blue: rgb_blue {
                label = "rgb_blue";
                gpios = <&gpio 34 1>;
                linux,default-trigger = "none";
            };
        };
    };
};

Instead of "none" in linux,default-trigger = "none"; you can use one of these other triggers:

[none] kbd-scrolllock kbd-numlock kbd-capslock kbd-kanalock kbd-shiftlock kbd-altgrlock kbd-ctrllock kbd-altlock kbd-shiftllock kbd-shiftrlock kbd-ctrlllock kbd-ctrlrlock timer oneshot heartbeat backlight gpio cpu0 cpu1 cpu2 cpu3 default-on input panic mmc0

Compile the DTS overlay:

sudo apt-get update
sudo apt-get install device-tree-compiler

sudo dtc -@ -I dts -O dtb -o /boot/overlays/cm-home-leds.dtbo cm-home-leds.dts 

Ad it in /boot/config.txt:

#CM3-Home led definition
dtoverlay=cm-home-leds

Reboot then check if the overlay has been loaded by typing:

sudo vcdbg log msg

The following directories should be present:

$ ls -al /sys/class/leds/rgb*
lrwxrwxrwx 1 root root 0 Oct  5 12:59 /sys/class/leds/rgb_blue -> ../../devices/platform/leds/leds/rgb_blue
lrwxrwxrwx 1 root root 0 Oct  5 12:59 /sys/class/leds/rgb_green -> ../../devices/platform/leds/leds/rgb_green
lrwxrwxrwx 1 root root 0 Oct  5 12:59 /sys/class/leds/rgb_red -> ../../devices/platform/leds/leds/rgb_red

Greetings

Many thanks to Mauro Soligo for his contribute.

Links


Products related

CM-Panel-Basic

Features Index Buy

All-in-one 7 inch touch WiFi terminal powered by Raspberry Pi CM4S
  • 7 inch TFT display 800x480
  • Capacitive touch
  • MIPI Camera connector
  • WiFi @ 2.4 GHz
CM-Panel-POE

Features Index Buy

All-in-one 7 inch touch POE terminal powered by Raspberry Pi CM4S
  • 7 inch TFT display 800x480 pixel
  • Capacitive touch
  • Embedded micro UPS for safe shutdown
  • Power Over Ethernet @ 10/100 Mbit
  • Hi-resolution audio up to 384KHz@32bit
  • Real Time Clock with backup battery
  • 3 USB Host port
  • 1 RS485/422/RS232 port
  • 1 Relay
  • MIPI Camera connector
  • WiFi @ 2.4 GHz (optional)
CM-Home

Features Index Buy

Home Automation board powered by Raspberry Pi CM4S lite module
  • Power-in @ 12 to 24VDC
  • Double opto-isolated RS-485 ports
  • 2 relays
  • 2 USB 2.0 host port
  • Ethernet port at 10/100Mbit
  • MIPI Camera connector
  • Stereo audio out on 3.5 mm jack

Home page CM Home technical documentation Buy