07 Working with I/O Pins
analogWrite()
Input/Output and Analog/Digital I/O Comparison
Pins in the Arduino controller can be configured as inputs or outputs. Additionally they can be either an analog or digital signal. The following table summarizes this variation and the commands used for each.
IMPORTANT Note: The analogWrite function has nothing to do with the analog pins or the analogRead function.
Writes an analog (PWM) value to a digital pin. Can be used to light a LED at varying brightnesses or drive a motor at various speeds. After a call to analogWrite(), the pin will generate a steady square wave of the specified duty cycle until the next call to analogWrite() (or a call to digitalRead() or digitalWrite() on the same pin). The frequency of the PWM signal on most pins is approximately 490 Hz. Pins 3 and 11 on the Leonardo also run at 980 Hz.
You do not need to call pinMode() to set the pin as an output before calling analogWrite().
On the RP2040 controller, the PWM generator source clock restricts the legal combinations of frequency and ranges. For example, at 1MHz only about 6 bits of range are possible. When you define an analogWriteFreq and analogWriteRange that can’t be fulfilled by the hardware, the frequency will be preserved but the accuracy (range) will be reduced automatically. Your code will still send in the range you specify, but the core itself will transparently map it into the allowable PWN range. The frequency used can be adjusted by the analogWriteFreq() function.
analogWrite(pin, value)
pin: the pin to write to.
value: the duty cycle: between 0 (always off) and 255 (always on).
Note: on other arduino platforms the resolution for analogRead is only 10 bits (0 to 1023) but on the RP2040 it is 12 bit (0 to 4095)