I'd like to generate two PWM signals with SAME duty cycle (50%) but DIFFERENT frequencies on raspberry pi 4B. The difference between the frequencies is within +/-80%. The two GPIOs I'll use are GPIO 18 and 19.

I also would like to have a range of frequencies too while time passing by. For example: f1 is freq of PWM channel 1 and f0 is freq of PWM channel 0.

At t = 0sec, f1=1000Hz, f0=1800Hz

t=1sec, f1 = 30Hz, f0=20Hz

t=2sec, f1 = 10Hz, f0=10Hz


I'm using wiringPi lib. So far I'm able to achieve variable frequency by changing the parameter "clock" using pwmSetClock( ). However, this function changes the PWM frequency of both channel 0 and 1. How can I set the frequencies separately for the two channels? I also thought about changing the "range" parameter using pwmSetRange(). However that seems changing both channels frequencies too.

I attached my code if that helps. Thank you.

#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>

#define N 10

int main (void) {

  if (wiringPiSetupGpio() == -1)
    exit (1) ;

  int clock_scale[N] = {4095, 2000, 1000, 500, 200, 100, 50, 20, 10, 8}; 
  int range = 4095, duty_cycle = 2047;
  int pin_HW_PWM = 18;

  pwmSetRange (range);
  pwmWrite(pin_HW_PWM, duty_cycle);

  while(1) {
    for(int i = 0; i < N; ++i)  {
      printf("clock_scale[%d] = %d\n", i, clock_scale[i]);

  return 0;
  • Rpi4B's all the on board PWM pins have the same frequency. Perhaps you can consider the cheapy ICs or modules: (1) ICL8038CCPD DIP14 ICL8038 DIP Precision Waveform Generator/Voltage Controlled Oscillator new and original IC - AliExpress €1.65/5 pieces aliexpress.com/item/… / to continue ,...
    – tlfong01
    Dec 1, 2020 at 6:52
  • (2) AD9833 Programmable Microprocessors Serial Interface Module Sine Square Wave DDS Signal Generator Module GY-9833 - AliExpress €1.37 aliexpress.com/item/…
    – tlfong01
    Dec 1, 2020 at 6:53

1 Answer 1


pigpio will let you set the frequencies to different values.

Python hardware_PWM

Note that the dutycycle is set out of a million. I.e. 500000 is 50%.

If you always want 50% duty you could use the clocks as well. See Python hardware_clock.

You can experiment from the command line by using the pigs hp and hc commands.


C clock

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.