1

This question already has an answer here:

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.

marked as duplicate by dhruvvyas90, Jacobm001, hildred, nc4pk, mpromonet Jul 6 '15 at 18:05

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

  • 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 1 '15 at 5:27
  • 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 1 '15 at 6:59
  • 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. stackoverflow.com/a/27750179/1753722 – dhruvvyas90 Jul 1 '15 at 7:58
  • 1
    You can try issuing commands on terminal like this 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 1 '15 at 8: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 1 '15 at 10:12
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.

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