Continue to Site

Welcome to our site!

Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

  • Welcome to our site! Electro Tech is an online community (with over 170,000 members) who enjoy talking about and building electronic circuits, projects and gadgets. To participate you need to register. Registration is free. Click here to register now.

How to Increase PWM freq?

Status
Not open for further replies.

AweLucid

New Member
Hi.
I have a project that uses a PIC16F877 to control the speed and direction of 3 motors driven by motor controllers. I had to code my own PWM generators to be able to control all three individually. However with this method the fastest PWM signal I can get is well within the audible range and my motors make a loud whine when the duty cycle is not %100.

What is a good easy way to increase my PWM frequency? I am thinking to filter the PWM signals to an analog voltage that varies between 0 and 5V and then use that voltage to control the duty cycle of a PWM generator circuit or IC. The problem here is I don't know a good circuit or IC that can convert voltage into duty cycle at a set frequency...
Your advice is greatly appreciated.
Thank you.
 
The 18F877 is limited to 5 MIPs ie 20MHz xtal. If you switch to a faster PIC like an 18F452 you can get 10 MIPs (10MHz xtal and HSPLL = 40MHz). That doubles your PWM frequency.

Also, there are some 18F series PICs with multiple PWM generators like the 18F1330.

You can also stick with the 16F877 and add external hardware like 3 comparator oscillators driven from the analogue filtered voltage (as you said) but there will be some issues like the slow response time to changes in PWM and the 16F877 is not a particularly powerful PIC for simultaneous 3 motor speed control.

There is another option of 3 smaller PICs, one for each motor.

What is it that you are making and what controls the motor speeds/directions? Maybe there is a much better way to approach the project than just trying to get a 16F877 to do everything. :)
 
Hi Mr RB. Thank you for your response.
This is for a Kinetic Art piece and projection environment in which concentric fabric hallways rotate around one another. So essentially there are 3 big wheels that the motors are driving, the largest is 15' in diameter. (if you are curious video of the piece is at www.awelucid.com ->installations -> labyrinth.

So I already made the project. and circuit is soldered, so I would prefer to use the same MCU and I am hoping for solution that doesn't require significant changes if possible, even if it is not the most elegant engineering. I am driving the 24V gear motors with a 15A MOSFET motor driver circuit Unfortunately this shorts off the motor when the PWM is hi, and thus exagerates the motor vibration. (I was using an L298 motor driver circuit in which the noise was MUCH quieter, but it could not handle the peak draw of my motors, particularly when they changed directions quickly or several times in a row and several ended up bursting into flames.) The new drivers work great accept the motors are now very noisy. When I change the frequency of the PWM in my code, the frequency of the sound also changes on the motors as expected. As an unsatisfying solution I have been running the motors and drivers with a 12V power supply, and so the volume (and MAX speed) of the motors is significantly reduced.

I do not need super quick speed changes as the size of the wheels prevents it. So your 3 comparator oscillator solution is very enticing to me. I have never used or built one before, are they available as discrete components.... Can you recommend a schematic or IC for this purpose?
Thanks
 
Post some code. It might be possible to reduce the resolution of the PWM to increase the frequency.

I guess that you have a counter counting from 0 to TOP. When the counter reaches TOP, you reset the counter back to zero. Then you have three values that you compare with your counter to generate the three PWM signals. Decreasing the TOP value increases the frequency but also decreases the duty cycle resolution.

You could also generate pseudorandom 8-bit numbers instead of using a linear counter. That way you get (statistically) maximum frequency around 50% duty and also reduce the generated noise (it spreads to a wider spectrum and reduces the power of the fundamental frequency).
 
Last edited:
That's a nice webpage AweLucid and an interesting animated sculpture concept.

I think maybe before going into specifics of the motor driver it might be worthwhile to work on the specific needs of the design and look at the best ways to achieve those needs.

More info would help, ie; how fast do the motors go, do they need different speeds, gearing, volts/amps needed at the motor to get the right speeds, do you need smooth accel/decel or rough accels or even no accels, and any more info would be good.

As you mentioned quietness as a priority I would lean towards a linear driving solution which should be very quiet and might also be the simplest system to make. Of course it depends on your requirements but it might be possible just to use simple motor on/off control with some analogue parts to give a crude smoothing of accels and decels.
 
You must be able to get a much higher PWM frequency.
Did you code in .asm?????

If you want 16bit resolution and you have 10 Mhz clock, the max PWM frequency is 152 Hz. With 8bit resolution it is 39 kHz. As I said before, the frequency depends on the resolution. Taking one bit off the resolution doubles the frequency.

Of course code optimization helps too. But it is impossible to say that he must be able to get much higher frequency because he never told what the frequency is at the moment and what is the resolution he needs.. and never showed any code.
 
Last edited:
The problem here is I don't know a good circuit or IC that can convert voltage into duty cycle at a set frequency...
Perhaps something based on this? In simulation it gives a ~40kHz PWM output with a duty cycle variable from ~7% to 100% by either a 0-5V DC control signal or a 5V PWM control signal of any frequency > 1kHz.
 
Thanks everyone for your help.

Mr. T, I did indeed try to lower the resolution of the PWM and increase its frequency, and unfortunately can only achieve a higher pitch/ more irritating noise. I am using a 20Mhz cyrstal on the clock. My PWM is built using the interupt timer2 module which unfortunately I have to significantly prescale to 1:4 and postscale 1:16 (otherwise there isn't time to run the code and causes problems). So thie measured interupts time is approximately every .18ms, and if i have an 8 bit PWM resolution i get a period of approximately 1.4ms or 695 Hz. I am told I need 4kHz to get beyond audible frequencies, which is over 7 times faster than my current code allows. Your suggestion about generating psudorandom values, would that be to randomize the frequency (the number of interupts per PWM) or would this randomize the duty cycle? Is this a known or common solution to eliminate noise?

Mr. RB: These are 3 gear motors that are 24Vs, one is smaller than the others, and they generally draw between 1-3A when loaded but have times when they draw up to 8 or more. They also use timing pulleys and belts to drive the sculpture. they move independently in different directions and speeds. their speed of the large wheels is between 0 and 1 time per second. and thus the motors spin at probably 6 times that speed. Right now the inputs to the MCU (which controls the speed and direction of the motors) are simple switches for direction and potentiometers. I would like to add programmable automations and eventually make it sound or motion reactive, which is why i chose the 40pin PIC for plenty more inputs. regarding the linear drive system, do you mean a linear actuator, I dont understand how that would turn these things turn. I have already invested a lot of work and money into the motors and drive system that are installed and they seem to do the job, minus this issue with the vibration. I am definietly open to offloading the PWM work away from the PIC.

Alec- thanks for your schematic. That potentially looks like a great solution.
*Does anyone have comments on this schematic before I order the parts?.... or if there are alternative schematics, or a discrete components already built for this purpose that would be helpful.

Many thanks to everyone.

Awe Lucid
www.awelucid.com
 
Hi AweLucid, thank you for providing the extra information. :)

When I said a "linear solution" that was in reference to using smooth linear DC voltage to drive the motors, not PWM. However your motor currents of 3A to 8A rule out linear voltage driving I think, as it would get too hot.
 
Ah, I see from post #9 that your PWM rate is only 695 Hz. Just increase the value of C4 to at least 4.7μF and my circuit should work ok with your existing PWM as the input signal.
If you think 40kHz is too high a rate you can reduce it by increasing R1 and R3. R3 may need adjusting anyway if Q2's gain is unusually high or low, so best make R3 a 10k trimmer.
Note that only 2 gates of the CD40106 are used per motor. So the one IC will handle 3 motors.
For anyone wanting an explanation of the circuit:
R4,C4 smooth the input voltage at Q2 base.
U1a, R1, C1 form a 40kHz oscillator.
Q2,R3 form a constant-current source controlled by the smoothed input voltage.
At the beginning of each oscillator cycle R2,C2 provide a brief pulse to turn on Q1 and so discharge C3.
U1b output goes high in response.
C3 then charges up from the current source.
When the C3 voltage reaches the upper Schmitt-trigger threshold of U1b the output of U1b goes low.
The resulting 'high' time of U1b output in each cycle, i.e. the duty cycle, is thus controlled by the Q2 base voltage.
 
My PWM is built using the interupt timer2 module

Could you post some code? Maybe the contents of the timer2 interrupt routine?

I am told I need 4kHz to get beyond audible frequencies

You need at least 20kHz.

Your suggestion about generating psudorandom values, would that be to randomize the frequency (the number of interupts per PWM) or would this randomize the duty cycle? Is this a known or common solution to eliminate noise?

Basically you would replace your triangle signal (the counter value) with random signal (uniform probability). It would pretty much randomize the entire (PWM) signal, but the average ON-time would be same as with traditional PWM (statistically). This is not a common solution, but other kind of randomness in PWM is used a lot to reduce noise (to spread it to a wider frequency spectrum).

Anyway I think the circuit alec_t posted is a good solution. Should be cheap and easy to build.
 
Last edited:
@AweLucid
+1 on the already mentioned 18f1330. It has three hardware PWM modules, no timer interrupt required, up to 14bit resolution, and 11 bits at 20khz. Need more pins? then a 18f4431 has the same Power Control Module with 4 PWM channels. Set up the PWM time base, and period registers, then it's just writing values to the duty cycle registers when required.

The 18f4431 should be a drop in replacement for the 16f877. Use a 10Mhz crystal and the 4xPLL instead of the 20Mhz crystal.
 
Last edited:
i have done a project a few years ago i used adc with potansiyometer0-5v changing frequancy with soft ware pwm
(not ccpmodul) if u need cod i can send
 
Hi. Thanks so much to everyone for all your help and suggestions. Replacing my mcu with the pic18f4431 might be a good solution, the pins match up closely and it has 4 pwm channels that will probably work well. I need to take a closer look at the details of this solution and contemplate any possible complications that it may entail. Otherwise I will build Alec_t's circuit. I really appreciate this community for the experienced advice i could never find in my local community.

Cheers
 
Status
Not open for further replies.

New Articles From Microcontroller Tips

Back
Top