Pulse Width Modulation

Pulse Width Modulation (PWM) can be used to generate a digital square wave signal, that again can be used as kind of analog voltage, because the digital signal will be smoothed out either directly or through a low pass filter.

Using the analogWrite(pin, value) function you can set the duty cycle or percentage, the pin will be high, i.e. 3,3 V.

You can also change the number of bits used for generating the PWM signal using the analogWriteResolution(bits) function. By default the PWM is using 8 bits (0-255) resolution. but it can go up to 12 bits (0-4095) providing a finer resolution .

The default PWM frequency is 732 Hz originating from 48 MHz divided by a 16 bits prescaler (65535). You can also see the Arduino PWM tutorial for more information about the use of PWM.

Below is an example of eight PWM signals applied to pin D0 to D7, with the same duty cycle in pairs D0/D4, D1/D5, D2/D6 and D3/D7, but using different PWM resolutions, where D0 to D3 use 8 bits and D4 to D7 use 12 bits.

If you want to use the above example on your Arduino Zero or Arduino M0 please note that the pins don’t match directly with the RFzero as the below table shows. But if you rename them accordingly and omit those that are not available the above example will work.

PinArduino Zero pin nameArduino M0 pin name
4Not availableNot available
5Not availableNot available

Instead of PWM you can also use the RFzero to make a real analog voltage by using the DAC output.