I am working on the RPI-CM4 board with raspbian buster OS.

I am trying to generate the 38KHz PWM wave using pigpio the library. My code is as follows.

#include <pigpio.h>
#include <unistd.h>

#define PWM_PIN 18

int main(int argc, char *argv[])
   gpioCfgClock(1, 0, 1);

   if (gpioInitialise()<0) return 1;

   gpioSetPWMfrequency(PWM_PIN, 38000);

   gpioPWM(PWM_PIN, 128);

   while (1) { sleep(1); }


At the output, I am getting the PWM wave generated @27.63 KHz. pwm_wave

Will you please help me to know, How do I set up a PWM clock to get a 38KHz resolution?

1 Answer 1


pigpio supports two types of PWM.

  1. DMA timed PwM on GPIO 0-31. This has a limited number of frequencies and duty cycle resolution. See gpioSetPWMfrequency for the permitted frequencies.

I am not sure why you seem to be getting 27 kHz, I'd expect 40 kHz from the way it is being setup.

  1. Hardware timed PWM on GPIO 12-13, 18-19. This is fully hardware PWM. See gpioHardwarePWM.

As an aside I would use waves to generate IR carrier. There is a Python example.

  • Using the gpioHardwarePWM I am able to generate the PWM waves > 50KHz after that the wave gets displeased 50% duty cycle. I want to drop down it up to 30KHz. will you please help me to know, how can I achieve this?
    – shiv patil
    Mar 28, 2022 at 5:11
  • Spent some time on this & found that the gpioHardwarePWM will work only on PWM channel-1 (gpio 19-13). ( referance link ) I am able to get the 38KHz wave on channel1 (gpio 19), but in between some pulses are missed. Now, will you please help me to know, 1. how do I configure channels to the hardware PWM? 2. Why the pulses are get missed in-between?
    – shiv patil
    Mar 29, 2022 at 12:56
  • I have solved this problem by using gpioWaveAddGeneric
    – shiv patil
    Mar 30, 2022 at 11:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.