Blog Datasheets Home About me Clients My work Services Contact

G2Labs Grzegorz Grzęda

Exploring PWM functionality on the nRF52

December 1, 2023

Exploring PWM functionality on the nRF52

In this blog post, we will dive into the Pulse Width Modulation (PWM) functionality on the nRF52 platform. PWM is a widely used technique to control the speed of motors and brightness of LEDs, among other things. The nRF52 series of microcontrollers provide powerful PWM capabilities, and we will explore them with extensive examples and explanations.

Introduction to PWM

PWM is a technique where the duty cycle of a square wave signal is modulated to control the average power delivered to a load. It provides a means to control the intensity or speed of a device by adjusting the ratio between the ON and OFF time of the signal.

On the nRF52, the PWM signals are generated using a dedicated PWM peripheral, which offers flexible configuration options. Each PWM signal is associated with a specific timer, enabling independent control of multiple PWM outputs.

Getting Started

To get started with PWM on the nRF52, we need to configure the PWM peripheral and set up the desired output pins. Let’s consider an example scenario where we want to control the brightness of an LED connected to pin P0.17.

First, we need to initialize the PWM peripheral:

1
nrf_pwm_init(PWM0, NRF_PWM_CLK_16MHz, NRF_PWM_MODE_2, 10);

In this example, we are initializing PWM0 with a 16MHz clock and MODE 2. The MODE determines the behavior of the PWM output, with MODE 2 enabling the PWM to stop after the set number of cycles.

Next, we need to configure the output pin:

1
nrf_gpio_cfg_output(17);

Finally, we can set the PWM duty cycle:

1
2
3
nrf_pwm_set_value(PWM0, 0, 50);
nrf_pwm_event_clear(PWM0, NRF_PWM_EVENT_SEQEND0);
nrf_pwm_task_trigger(PWM0, NRF_PWM_TASK_SEQSTART0);

Here, we set the PWM duty cycle of PWM0 to 50%. The duty cycle value ranges from 0 to 100.

Advanced Configuration

The nRF52 allows advanced configuration of the PWM peripheral, enabling more precise control over the PWM output. Let’s explore some of these advanced configurations.

Resolution and Counter Mode

The PWM resolution determines the number of steps available between the minimum and maximum values. Higher resolution provides finer granularity, but requires more processing power.

1
nrf_pwm_configure(PWM0, PWM0_WIDTH_10BIT, PWM_COUNTERTOP_DEFAULT, PWM_MODE_UP);

In this example, we configure PWM0 with a resolution of 10 bits (1024 steps) and use the default counter top and an up-counting mode.

Individual Channel Configuration

The PWM peripheral on the nRF52 allows independent configurations for each channel. Each channel can have its own counter mode, resolution, and duty cycle. This enables fine-grained control over multiple PWM outputs.

1
2
3
4
5
6
7
8
9
nrf_pwm_sequence_t const seq =
{
    .values.p_individual = &duty_cycle,
    .length              = NRF_PWM_VALUES_LENGTH(duty_cycle),
    .repeats             = 0,
    .end_delay           = 0
};

nrf_pwm_sequence_set(PWM0, 0, &seq);

In this example, we set an individual duty cycle for each channel using the nrf_pwm_sequence_set() function.

Dead Time Insertion

The nRF52 PWM peripheral supports dead time insertion, which allows precise control over the time between complementary events. Dead time insertion is useful in applications like motor control to prevent shoot-through.

1
2
3
4
5
6
7
8
9
nrf_pwm_sequence_t const seq =
{
    .values.p_individual = &duty_cycle,
    .length              = NRF_PWM_VALUES_LENGTH(duty_cycle),
    .repeats             = 0,
    .end_delay           = DEAD_TIME
};

nrf_pwm_task_trigger(PWM0, NRF_PWM_TASK_SEQSTART0);

In this example, we set a specific dead time value using the end_delay field in the sequence structure.

Conclusion

The nRF52 provides powerful and flexible PWM functionality for controlling motors, LEDs, and various other devices. In this blog post, we explored the basics of PWM on the nRF52, along with advanced configuration options and examples.

By diving into the extensive examples and explanations provided, you should now have a solid understanding of how to use PWM on the nRF52 and unleash its full potential in your projects. Happy PWM coding!


➡️ Using the ADC module on the nRF52


⬅️ Interfacing sensors with the nRF52


Go back to Posts.