The latest development version of this page may be more current than this released 4.0.0 version.

PWM Blinky

Browse source code on GitHub

Overview

This application blinks an LED using the PWM API. See Blinky for a GPIO-based sample.

The LED starts blinking at a 1 Hz frequency. The frequency doubles every 4 seconds until it reaches 128 Hz. The frequency will then be halved every 4 seconds until it returns to 1 Hz, completing a single blinking cycle. This faster-then-slower blinking cycle then repeats forever.

Some PWM hardware cannot set the PWM period to 1 second to achieve the blinking frequency of 1 Hz. This sample calibrates itself to what the hardware supports at startup. The maximum PWM period is decreased appropriately until a value supported by the hardware is found.

Requirements

The board must have an LED connected to a PWM output channel. The PWM controlling this LED must be configured using the pwm_led0 devicetree alias, usually in the BOARD.dts file.

Wiring

No additional wiring is necessary if pwm_led0 refers to hardware that is already connected to an LED on the board.

In these other cases, however, manual wiring is necessary:

Board

Wiring

Nucleo F401RE

connect PWM2 (PA0) to an LED

Nucleo L476RG

connect PWM2 (PA0) to an LED

STM32F4 Discovery

connect PWM2 (PA0) to an LED

Nucleo F302R8

connect PWM2 (PA0) to an LED

Nucleo F103RB

connect PWM1 (PA8) to an LED

Nucleo WB55RG

connect PWM1 (PA8) to an LED

ESP32-DevKitC-WROOM

connect GPIO2 to an LED

ESP32-S2-Saola

connect GPIO2 to an LED

ESP32-C3-DevKitM

connect GPIO2 to an LED

Building and Running

To build and flash this sample for the nRF52840 DK:

west build -b nrf52840dk/nrf52840 samples/basic/blinky_pwm
west flash

Change nrf52840dk/nrf52840 appropriately for other supported boards.

After flashing, the sample starts blinking the LED as described above. It also prints information to the board’s console.

See also

PWM Interface