1

I am trying to control a fan using Raspberry Pi. For that I tried to use the PWM pin in raspbery Pi. I am using Qt C++ to control the fan. I installed the wiring Pi and run the Wiring pi using command line and I checked the man page of Wiring Pi. The man page not given any exact detail to set frequency and change duty cycle

I failed to set a fixed frequency(25KHz) and set duty cycle from 0 to 100% using wiring pi and man page details.

How toset a fixed frequency and vary duty cycle in Raspberry Pi PWM pin using wiring Pi or any other solution I can use in my Qt C++ code to control the PWM pin.

Ghanima
  • 15,855
  • 15
  • 61
  • 119
Arun Kumar K S
  • 303
  • 1
  • 3
  • 11
  • That question not asked any duty cycle details – Arun Kumar K S Jul 01 '15 at 04:54
  • 2
    As per I understand, you can change duty cycle by pwmWrite(pin_no,[0-1023]) where 1023 being 100 % duty cycle or always on. And you can set freq by setting up proper pwmClock and pwmRange parameters as per mentioned in that link. Make sure to use mark space mode. Hope it helps. – dhruvvyas90 Jul 01 '15 at 05:27
  • I checked this but when I change duty cycle it affects frequency. How to set a fixed frequency and change duty cycle. – Arun Kumar K S Jul 01 '15 at 06:49
  • 1
    Did you set pwmSetMode(PWM_MODE_MS); ? Personally I have not used PWM and I don't have an oscilloscope so that I can test (and experiment with wiringPi) code but by reading documentation of wiringPi, this should work ideally. – dhruvvyas90 Jul 01 '15 at 06:59
  • I used the code like this if (wiringPiSetup () == -1) exit (1) ; pwmSetMode(PWM_MODE_MS); pinMode (1, PWM_OUTPUT) ; pwmSetClock(500); pwmSetRange(10); pwmWrite (1, 5);
        for (;;)
        {
    
            delay (1000);
        }
    
    
    

    When I change pwmWrite (1, 5); to pwmWrite (1, 200); It affect frequency also in oscilloscope. Is there any worong in my code

    – Arun Kumar K S Jul 01 '15 at 07:29
  • 1
    Can you change your code sequence a little bit ? Use pwmSetMode(PWM_MODE_MS); after pinMode(1,PWM_OUTPUT); line. It looks like sequence is very important. Here is a reference code. http://stackoverflow.com/a/27750179/1753722 – dhruvvyas90 Jul 01 '15 at 07:58
  • 1
    You can try issuing commands on terminal like this http://stackoverflow.com/a/21344373/1753722 if you don't want to go and change your code every now and then. Hope it helps. – dhruvvyas90 Jul 01 '15 at 08:01
  • 1
    Great. Glad to hear that. :) You can add your sample code as an answer and accept it as a correct one so that others can refer to it in case they stumble across this link. – dhruvvyas90 Jul 01 '15 at 10:12

1 Answers1

1

My pigpio library has several ways of setting PWM.

The more usually used will be hardware timed PWM on any gpio with a (default) frequency of 800Hz and a dutycycle which may be varied between 0 and (a default) 255. For C see gpioPwm.

If that does not suit there is more flexible PWM generated by the two hardware PWM channels. A frequency in the range 1Hz-125MHz (I doubt anything above 30MHz will have any useful effect) with a selectable dutycycle between 0 (off) and 1000000 (fully on). For C see gpioHardwarePWM.

joan
  • 71,024
  • 5
  • 73
  • 106