Before proceeding with this demo please follow the Installation of Nordic Zephyr Development Tools in Linux. The purpose of this demo is to demonstrate the Pulse Width Modulation (PWM) capabilities using Zephyr with the Nordic nRF54L15-DK development kit internal LED,
Create the following Zephyr configuration file called proj.conf in the project folder of your choice,
#
# Copyright (c) 2024 Nordic Semiconductor
#
# SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
#
# Logging
CONFIG_LOG=y
# Enable PWM
CONFIG_PWM=y
CONFIG_PWM_LOG_LEVEL_DBG=y
# Enable LED and LED PWM
CONFIG_LED=y
CONFIG_LED_PWM=y
Per standard Zephyr development procedure, then include the CMakeLists.txt file inside the project folder,
cmake_minimum_required(VERSION 3.20.0)
find_package(Zephyr REQUIRED HINTS $ENV{ZEPHYR_BASE})
project(spi)
target_sources(app PRIVATE src/main.c)
Also include the following Zephyr overlay file called nrf54l15dk_nrf54l15.overlay,
/{
pwmleds {
compatible = "pwm-leds";
pwm_led0: pwm_led_0 {
pwms = <&pwm20 0 PWM_MSEC(20) PWM_POLARITY_NORMAL>;
};
};
};
&pwm20 {
status = "okay";
pinctrl-0 = <&pwm20_custom>;
pinctrl-1 = <&pwm20_csleep>;
pinctrl-names = "default", "sleep";
};
&pinctrl {
pwm20_custom: pwm20_custom {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 1, 10)>;
nordic,invert;
};
};
pwm20_csleep: pwm20_csleep {
group1 {
psels = <NRF_PSEL(PWM_OUT0, 1, 10)>;
low-power-enable;
};
};
};
and finally creare a folder called src where the source code main.c of the demo will reside,
/*
* Copyright (c) 2024 Nordic Semiconductor ASA
*
* SPDX-License-Identifier: LicenseRef-Nordic-5-Clause
*/
#include <zephyr/kernel.h>
#include <zephyr/logging/log.h>
/* Include the relevant headers for pwm*/
#include <zephyr/device.h>
#include <zephyr/drivers/pwm.h>
LOG_MODULE_REGISTER(DigiKey_Coffee_Cup, LOG_LEVEL_INF);
/* Define the desired PWM period and pulse */
#define PWM_PERIOD_NS 100000000
#define PWM_PULSE_WIDTH 1400000
/* Get the node identifier for [] through its alias */
#define PWM_LED0 DT_ALIAS(pwm_led0)
/* Initialize and populate struct pwm_dt_spec */
static const struct pwm_dt_spec pwm_led0 = PWM_DT_SPEC_GET(PWM_LED0);
int main(void)
{
int err;
/* Check if the device is ready */
if (!pwm_is_ready_dt(&pwm_led0)) {
LOG_ERR("Error: PWM device %s is not ready", pwm_led0.dev->name);
return 0;
}
/* Control the LED with the control signal generated from the PWM */
err = pwm_set_dt(&pwm_led0, PWM_PERIOD_NS, PWM_PULSE_WIDTH);
if (err) {
LOG_ERR("Error in pwm_set_dt(), err: %d", err);
return 0;
}
return 0;
}
The Zephyr project folder should look like this,
|-- CMakeLists.txt
|-- nrf54l15dk_nrf54l15.overlay
|-- prj.conf
`-- src
|-- main.c
Now proceed to build this Zephyr project as follows in the python virtual environment,
digikey_coffee_cup (venv) $ west build -p always -b nrf54l15dk/nrf54l15/cpuapp -- -DEXTRA_DTC_OVERLAY_FILE=nrf54l15dk_nrf54l15.overlay
Finally connect the nRF54L15-DK Nordic Development kit via the USB interface to flash it,
digikey_coffee_cup (venv) $ west flash
The following video shows the LED in the Nordic nRF54L15-DK development kit flashing with the PWM signal,
The Nordic nRF54L15-DK development kit is an excellent IoT development platform and is available at DigiKey. Have a nice day!
Este articulo esta disponible en español aquí.
This article is also availabe in spanish here.
