Adding a dimmable LED node
This example demonstrates how to add support for a dimmable LED node to your board in an overlay file.
To implement this example, you can either edit the devicetree files manually or use nRF Connect for VS Code with its Devicetree language support and the Devicetree Visual Editor (recommended).
For more advanced LED control, you can also use the LEDs module of the Common Application Framework (CAF), which provides additional features, such as LED effects and power management integration.
Implementation overview
This example uses Zephyr’s generic binding for PWM LED controllers named pwm-leds.
The devicetree bindings provide the structure for the content of the devicetree nodes. The compatible property defines the compatibility of a devicetree node with a devicetree binding. For more information, read the documentation about devicetree bindings in Zephyr.
See also the following related documentation:
Pulse Width Modulation (PWM) - Zephyr’s PWM driver API documentation
Devicetree Guide - Zephyr’s devicetree user guide
Open or create the overlay file to edit
Overlay files are a category of devicetree’s Input and output files. These files can override node property values in multiple ways.
You can add them to your configuration manually or by using nRF Connect for VS Code (see How to create devicetree files).
Add the PWM LED controller node
Complete the following steps:
Add the controller node
pwmledsunder the root node in devicetree.Add the
pwm-ledsbinding for the driver to pick up.Add LEDs as child nodes on the
pwmledscontroller node, withpwmsandlabelproperties.Make sure the
pwmsproperty of thephandle-arraytype points to a PWM instance. The PWM instance takes the pin number as its only parameter.
The following code example uses pins 6 and 7 on the GPIO port 0 (&pwm0):
/ { // Devicetree root
// ...
pwmleds { // Controller node
compatible = "pwm-leds"; // Compatible binding
led_pwm_1 {
pwms = <&pwm0 6>; // Pin assigned to GPIO port 0 for LED 1
label = "PWM LED 1";
};
led_pwm_2 {
pwms = <&pwm0 7>; // Pin assigned to GPIO port 0 for LED 2
label = "PWM LED 2";
};
};
};
Configure the PWM node
Enable the referenced PWM0 node by setting status to "okay" and configuring the node’s channels.
Use the &pwm0 node label reference on the root of the file:
&pwm0 {
status = "okay"; // Status
ch0-pin = <6>; // Pin assignment for channel 0
ch1-pin = <7>; // Pin assignment for channel 1
};
Enable the LED PWM driver
Add the following line to your prj.conf file:
CONFIG_LED_PWM=y
Once you have added the LED PWM driver, build your application and program it to your board.