# PWM @ 140hz?

I need a period of 7.1ms and be able to modify duty cycle within that. The closest I can get looks to be 125hz, is that correct? Is there a shield that will facilitate this or am I stuck with 125hz or bitbanging?

The PWM is a function of the timers and the oscillator, it is not something that can be corrected by a shield. You can use a shield (extra hardware) to build another PWM circuit and have the period to just what you want.

The closest I can get looks to be 125hz, is that correct?

Without going into it I can't say, how are you altering it.

As a final point do you really need it to be that precise?

I am interfacing with an actuator for controlling vanes on a turbo charger. We are trying to retrofit new technology on ancient diesels. According to the documentation you can modify the interrupts oif the AVR8 timer to produce 125hz (122hz). You can also produce other frequencies but that is by adjusting the duty. I need to be able to adjust the duty from 0-100% @ 140hz. A friend with an oscilloscope got this reading and was able to apply that frequency to the actuator and have it move appropriately. I have asked him to check and see if 125hz works as well because as best as I can tell 140hz is an oddball frequency for PWM.

I am a programmer not an engineer, but am picking up quickly.

140Hz PWM should be pretty easy to do in software, depending on how many steps you need and how evenly spaced the steps need to be... (although if you're controlling real-world thing with that, you'll need to be a little careful about having the SW do other things...)

That's my concern, I will be reading other analog and digital inputs and calculating duty cycle on that.

About generating 140Hz, don't know if you have duamilanove or mega board but fairly easy to do in both cases using 16bit-timer. For example, here is one possibility with mega board but there are many other ways to do...

``````int outputPsuB = 45;  // Timer5-B

void setup()
{
// PSU outputs via timer5
pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B

TCCR5A = B00100010; // Phase correct PWM change at OCRA
TCCR5B = B10010;  // prescaling by 8 the system clock
OCR5A = 7143; // 139,9972Hz
OCR5B = 2357; // 33% PWM
}
``````

PWM = OCR5B/OCR5A

Thanks, I have the duamilanove. So this will set the frequency to 133,9072hz and then I control duty cycle analogWrite?

Ok, here is the code for a duamilanove board using the unique 16-bit timer.

``````int outputPsuB = 10;  // Timer1-B

void setup()
{
pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B

TCCR1A = B00100001; // PWM, Phase and frequency correct - change at OCR1A
TCCR1B = B10010;  // prescaling by 8 the system clock
OCR1A = 7143; // 139.9972Hz
OCR1B = 2357; // 33% PWM
}
``````

To control duty cycle, you just do it for example by changing OCR1B but leaving constant OCR1A which defines your 139.9972Hz frequency close enough from 140Hz

``````OCR1B= <new value>;
``````

where PW=OCR1B/OCR1A

If you want to use timer2 which only has 8 bits, you’ll get less PW resolution along with a frequency of 140.1345Hz instead of 139.9972Hz

``````int outputPsuB = 3;  // Timer2-B

void setup()
{
pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B

TCCR2A = B00100001; // PWM, Phase correct - change at OCR2A
TCCR2B = B1110;  // prescaling by 256 the system clock
OCR2A = 223; // 140.1345Hz
OCR2B = 50; // 22.42% PWM
}
``````

To control duty cycle, you just do it for example by changing OCR2B but leaving constant OCR2A which defines your 140.1345Hz frequency close enough from 140Hz

``````OCR2B= <new value>;
``````

where PW=OCR2B/OCR2A

Ah OK, thanks for clarifying!

Another quesion, the PWM voltage at 100% duty is only 5v? What if I need 10v?

You need a driver to step-up voltage because all arduino outputs are TTL ;)

Not sure what that means for me, I'm such a noob. My input voltage is 12v, is the PWM still 5v?

Not sure what that means for me, I'm such a noob. My input voltage is 12v, is the PWM still 5v?

I guess this thread will help some thanks.

http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?action=print;num=1232562874

what is your arduino board type and what is your power supply voltage powering your arduino ?

In any case, your arduino output pin generating PWM 140Hz will always be TTL (off=0V - On=5V) even if your board power supply is for example 12V.

what is your vane actuator spec: voltage & current needed to operate properly ?

depending on your actuator spec, you'll need to design a driver to either step-up voltage from TTL to fhigher voltage and/or boost current if your actuator needs more that max current tolerated by arduino TTL output

In resumé: your arduino can software generate PWM 140Hz with a TTL logic level output but you might need to adapt this signal to your actuator spec

Deulimanove. Vane actuator specs are unknown, so far we have just based it off of the signal the is being generated to it. Someone was able to use a signal generator from an oscilloscope @ 10v to control the vanes. I've been looking for some premade solution to ramp up the voltage from 5v to 10v without much luck.

I'm such a noob when it comes to this stuff, any help is appreciated. They same controller also accepts CAN bus but I have been unable to find any further information or cheap solutions.

So if OCR5B/OCR5A gives you a 33% PWM, how do you use that in the analogWrite() function? I know you're supposed to do analogWrite(PWM_PIN,DUTY_CYCLE) where DUTY_CYCLE is a number between 0 and 255. If you already set the duty cycle by setting OCR5B/OCR5A, do you just put the same thing? i.e. analogWrite(PWM_PIN, 84)?

normally or officially, arduino libraries does not let you choose any PWM frequency because it can be tricky or corrupting official subroutines as millis() or micros() so best to know what you're doing on assembly langage. This means i don't use analogwrite() but only relies on setting OCRnB versus OCRnA to directly set my PWM duty cycle.

I guess the confusing thing to me is this. I have an RC car I want to drive with outputs from the Arduino. The IR receiver on the car operates at 66.8Hz, where a duty cycle of 10% on PWM1 is stop, 13.5% is FWD, and 8.5% is reverse. Same goes for PWM2 for right and left. It also uses a square wave. Looking through the different timer alteration code and such, I don't see any feasible way of actually outputting a 66.8Hz square wave with a duty cycle of 13.5%. This code:

``````int outputPsuB = 45;  // Timer5-B

void setup()
{
// PSU outputs via timer5
pinMode(outputPsuB, OUTPUT);  // select Pin as ch-B

TCCR5A = B00100010; // Phase correct PWM change at OCR5A
TCCR5B = B10010;  // prescaling by 8 the system clock
OCR5A = 14970; // 66,8002672Hz
OCR5B = 1497; // 10% PWM
}
``````

Sets the timers and everything, but doesn't produce a square wave. What am I doing wrong?

1) From a radio-communication or opto-communication design perspective, you'll never get precisely a pure timing so an approximate 13,5% will work by setting OCR5B=2021.

2) Where did you put your scope to see it was not square wave output & did you load the arduino pin ?

3) Just leave not-connected pin45 and see what scope says. If you get square wave, this means you have an electronic driver issue

5) otherwise try with another timer hence another pin