RP2040_PWM Library for multichannel hardware-based PWMs

RP2040_PWM library GitHub release
How To Install Using Arduino Library Manager


Features

This PWM-wrapper library enables you to use Hardware-PWM blocks on RP2040-based boards to create and output PWM any GPIO pin. These purely hardware-based PWM channels can generate from very low (lowest is 7.5Hz) to very high PWM frequencies (in the MHz range, up to 62.5MHz).


The most important feature is they're purely hardware-based PWM channels. Therefore, their operations are not blocked by bad-behaving software functions / tasks.

This important feature is absolutely necessary for mission-critical tasks. These hardware PWM-channels, still work even if other software functions are blocking. Moreover, they are much more precise (certainly depending on clock frequency accuracy) than other software timers using millis() or micros(). That's necessary if you need to control external systems (Servo, etc.) requiring better accuracy.

The PWM_Multi example will demonstrate the usage of multichannel PWM using multiple Hardware-PWM blocks (slices). The 8 independent Hardware-PWM slices are used to control 8 different PWM outputs, with totally independent frequencies and dutycycles.

Being hardware-based PWM, their executions are not blocked by bad-behaving functions / tasks, such as connecting to WiFi, Internet or Blynk services. You can also have many (up to 16) PWM output signals to use.

The RP2040 PWM block has 8 identical slices. Each slice can drive two PWM output signals, or measure the frequency or duty cycle of an input signal. This gives a total of up to 16 controllable PWM outputs. All 30 GPIO pins can be driven by the PWM block

This non-being-blocked important feature is absolutely necessary for mission-critical tasks.


Currently Supported Boards

This RP2040_PWM library currently supports the following boards:

  1. RP2040-based boards such as NANO_RP2040_CONNECT, RASPBERRY_PI_PICO, ADAFRUIT_FEATHER_RP2040 and GENERIC_RP2040, etc. using either Arduino-mbed mbed_nano or mbed_rp2040 core or Earle Philhower's arduino-pico core.


Changelog

Initial Releases v1.0.0

  1. Initial coding to support RP2040-based boards such as Nano_RP2040_Connect, RASPBERRY_PI_PICO, etc. using either RP2040 ArduinoCore-mbed mbed_nano or mbed_rp2040 core or Earle Philhower's arduino-pico core
  2. The purely hardware-based PWM channel can generate from very low (lowest is 7.5Hz) to very high PWM frequencies (in the MHz range, up to 62.5MHz).

Examples:

  1. PWM_Multi

Debug Terminal Output Samples

1. PWM_Multi on MBED RaspberryPi Pico

The following is the sample terminal output when running example PWM_Multi on RaspberryPi Pico, running ArduinoCore-mbed mbed_rp2040 core, to demonstrate the ability to provide high PWM frequencies and the accuracy of Hardware-based PWM, especially when system is very busy.

Starting PWM_Multi on RaspberryPi Pico
RP2040_PWM v1.0.0
=============================================================
Index	Pin	PWM_freq	DutyCycle	Actual Freq
=============================================================
0	    25	7.50		10		    7.50
1	    1	8.00		20		    8.00
2	    2	10.00		30		    10.00
3	    3	1000.00		40		    1000.00
4	    4	2000.00		50		    2000.00
5	    5	3000.00		60		    3000.00
6	    6	8000.00		70		    8000.00
7	    7	9999.00		80		    9999.00
=============================================================

Releases v1.0.1

  1. Fix bug generating wrong frequency. See Wrong frequency #1

Releases v1.0.2

  1. Fix bug not changing frequency dynamically. See Change the PWM frequency #2

Examples:

  1. PWM_Multi
  2. PWM_DynamicFreq

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.