setPwmFrequency(pin, frequency, dutycycle)
Sets the PWM (Pulse Width Modulation) wave frequency in Hz and duty cycle.
Parameters
pin - The GPIO pin number.
frequency - PWM frequency (in Hz). Available ranges are between 19 and 37500.
dutycycle - PWM duty cycle. Available ranges are between 0 and 255.
Returns
None
Example
#include <Arduino.h>
int GPIO_PIN = 12; // GPIO12
void setup() {
pinMode(GPIO_PIN, PWM_OUTPUT);
}
void loop() {
// Set PWM to 100 Hz and 25% duty cycle
setPwmDutyCycle(GPIO_PIN, 100, 64);
delay(1000);
// Set PWM to 1 Khz and 50% duty cycle
setPwmDutyCycle(GPIO_PIN, 1000, 128);
delay(1000);
// Set PWM to 10 kHz and 75% duty cycle
setPwmDutyCycle(GPIO_PIN, 10000, 192);
delay(1000);
}
Notes
- The default PWM frequency is 490 Hz
- PWM_OUTPUT mode uses the internal PWM hardware of the BCM283x SoC.
The available PWM pins are the followings:
GPIO PIN | Available in RPi (40-pins) connector | PWM channel |
---|---|---|
12 | YES | 0 |
13 | YES | 1 |
18 | YES | 0 |
19 | YES | 1 |
40 | NO | 0 |
41 | NO | 1 |
45 | NO | 1 |
52 | NO | 0 |
53 | NO | 1 |