| nRF5 SDK v12.0.0 |

Choose documentation: | nRF5 SDK | S130 SoftDevice API | S132 SoftDevice API | S212 SoftDevice API | S332 SoftDevice API

All Data Structures Functions Variables Typedefs Enumerations Enumerator Groups Pages
PWM Driver Example
This information applies to the nRF52 Series only.

The PWM Driver Example demonstrates how to take advantage of the PWM driver when creating an application that uses the Pulse Width Modulation (PWM) peripheral.

This example consists of several demos that show how to generate various types of pulses. The LEDs on the development board are used to visualize the PWM output channels.

You can find the source code and the project file of the example in the following folder: `<InstallFolder>\examples\peripheral\pwm_driver`

# Demos

Five different demos are available. Use Button 1 and Button 2 to switch to the previous or next demo, respectively. The number of the started demo is output on UART.

Note that the CPU is not at all involved in the playback process in demos 2 - 5. It stays in sleep mode and is only woken up when a different demo is started.

## Demo 1

This demo plays back a sequence with different values for individual channels (LED 1 - LED 4). Only four values are used (one per channel). Every time the values are loaded into the compare registers, they are updated in the provided event handler. The values are updated in such a way that increase and decrease of the light intensity can be observed continuously on succeeding channels (one second per channel).

## Demo 2

This demo plays back two concatenated sequences:

- Sequence 0: Light intensity is increased in 25 steps during one second.
- Sequence 1: LED blinks twice (100 ms off, 100 ms on), then stays off for 200 ms.

The same output is generated on all 4 channels (LED 1 - LED 4). The playback is repeated in a loop.

## Demo 3

This demo uses only one channel, which is reflected on LED 1. The LED blinks three times (200 ms on, 200 ms off), then it stays off for one second.

This scheme is performed three times before the peripheral is stopped.

## Demo 4

This demo uses all three PWM peripheral instances:

- PWM0 drives LED 1 and LED 2: Subsequent 2-bit binary values are presented every 500 ms.
- PWM1 drives LED 3: During 500 ms, the LED increases and decreases the light intensity, then it stays off for 1500 ms.
- PWM2 drives LED 4: For 500 ms, the LED stays off, then during 1500 ms it increases and decreases the light intensity.

Simple playback with grouped loading mode is used for PWM0, and complex playback with common loading mode is used for both PWM1 and PWM2. See Driver configuration for information about the loading modes.

## Demo 5

This demo, similarly to Demo 1, plays back a sequence with different values for individual channels. Unlike demo 1, however, it does not use an event handler. Therefore, the PWM peripheral does not use interrupts and the CPU can stay in sleep mode.

The LEDs (1-4) blink separately. They are turned on for 125 ms each, in counterclockwise order (looking at the board).

# Testing

Test the PWM Driver Example application by performing the following steps:

1. Compile and program the application.
2. Start a terminal emulator like PuTTY and connect to the used COM port with the following UART settings:
   - Baud rate: 115.200
   - 8 data bits
   - 1 stop bit
   - No parity
   - HW flow control: RTS/CTS
3. Switch from one demo to another using Button 1 and Button 2.
4. Check the number of the started demo on the UART output.
5. Observe that the LEDs in a given demo are driven according to the description of the demo.