Note: these numbers are estimates, based on datasheets and measurements. They don't include everything and may be wrong.
LED: fade multiple
One PWM peripheral can be used to control multiple LEDs as well, by using different channels. In that case, the frequency (or period) will be the same for all LEDs, but the duty cycle (percent on) will vary per channel and therefore per LED.
On the right, we have a single PWM instance but we use two goroutines to fade both LEDs in and out. Both LEDs are using a different PWM channel.
Finding the correct PWM peripheral and pins
You may be wondering how we picked the right PWM peripheral and pins? It’s actually all in the documentation if you know where to look:
- Arduino Nano 33 IoT
- Adafruit Circuit Playground Bluefruit (special case, see below)
- Adafruit Circuit Playground Express
- Raspberry Pi Pico
Let’s look at the first, the Arduino Nano 33 IoT. In the Pins table, there are these two rows:
Pin Hardware pin Alternative names PWM A2
PA11
TCC1
(channel 1),TCC0
(channel 3)A3
PA10
I2S_SCK_PIN
TCC1
(channel 0),TCC0
(channel 2)
In this table you can see that we actually could have used two different PWM peripherals! Either TCC1 or TCC0. Both are supported for both pins, and importantly both use different channels for each pin. For TCC1 (as used on the right) channel 1 and 0 are used, while for TCC0 channel 2 and 3 would have been used.
Question: can you verify that the peripheral used for the other 3 boards corresponds to the PWM peripheral and channel used in the simulator on the right?
You might have noticed that three out of the four boards list PWMs, but one does not: the Circuit Playground Bluefruit. This is because the PWM peripherals on this chip can be connected to any pin. So what happens instead is that the machine.PWM
type will not look up the pin in a table or something, but will instead use the first available unused channel. This brings a lot of flexibility, especially when you are working with special device types like servos (which will be addressed later in this tour).