Go Down

Topic: PWM (Read 1 time) previous topic - next topic

ahooper

I am trying to get my head around PWM and using Timer2 to run a ferite coil. My aim is to find the best frequency to opperate the coil at at a reduced duty cycle. something around 4Khz and 15%DC. My circuit is reasonably simple with pin11 connected to an optoisolator then to a fet and to the coil.

Until a HDD crash a while back i had the perfect thing that i muddled together and tried to recreate it but just seem to be working myself in to a corner and not getting anywhere.

I started with this..

 pinMode(3, OUTPUT);
 pinMode(11, OUTPUT);
 TCCR2A = _BV(COM2A1) | _BV(COM2B1) | _BV(WGM21) | _BV(WGM20);
 TCCR2B = _BV(CS22);
 OCR2A = 180;
 OCR2B = 50;

By adjusting the values I'm getting the perfect frequency but don't seem to be getting any variation on the pulse width.

Grumpy_Mike

It is the value of OCR2A that changes the width.

ahooper

Thanks Mike,

That would make sense as to why it was pulling over 8A while it was set like that.

So if I reduce OCR2A and increase OCR2B the the output on pin11 should give me a lower duty cycle with higher frequency.
Do you actually get exactly the same on both Pin3 and 11?

Regards
Andrew

Grumpy_Mike

Just change OCR2B and the frequency stays the same but the mark space ratio changes.
Yes both pins will change if enabled.

ahooper

Brilliant.
The frequency is still way too low so I assume just creep both values up until it hits the spot. Doing it the manual way this seemed to be the sweet spot for the coil.

digitalWrite(11, HIGH);
delayMicroseconds(50);
digitalWrite(11, LOW);
delayMicroseconds(200 - 50);

Pulled just over 1A from the battery and gave a nice stable output. running a 16Mhz processor but as soon as i start adding bits like checking the button presses and running the other devices this will throw the above way out.

dc42

Be aware that optoisolators are quite slow at turning off, so getting a PWM signal through them gets harder as the frequency increases. I did manage to get a 25KHz pwm signal through a standard opto isolator a while ago, but I only needed to go as low as 30% duty cycle and I had to use extra components to get the opto to turn off fast enough.

Why do you need the opto isolator anyway, if all you are driving is a coil?
Formal verification of safety-critical software, software development, and electronic design and prototyping. See http://www.eschertech.com. Please do not ask for unpaid help via PM, use the forum.

ahooper

The opto and circuit seems to be working fine, I only need to go up to about 4Khz so well within the range.

Its a BIG coil and there is a ton of EMF once its running. I don't want this anywhere near the arduino. V1 of this thing had enough kick to fry the FET's then either cause the arduino to lock up or in one instance killed pin11.

I have had a similar issue some years back when i built a PWM controller for my milling machine, The idea was to isolate the motors from the computer but as soon as the opto was in place it started to do strange things, removed it and all was good. it manifested itself by ramping the speed up then becoming unstable but just after that it would crank on full noise.


ahooper

someone else has suggested that you cant adjust the frequency like I am trying to do. Is that correct?

What would be really cool is a tone function that had a second setting that would allow you to adjust the duty cycle. that would be perfect for a couple of project that im working on. \

Grumpy_Mike

Quote
someone else has suggested that you cant adjust the frequency like I am trying to do. Is that correct?

No it is not. I use this code for changing the frequency of a modulated IR beam.

Code: [Select]
void setIrModOutput(){  // sets pin 3 going at the IR modulation rate
  pinMode(3, OUTPUT);
  TCCR2A = _BV(COM2B1) | _BV(WGM21) | _BV(WGM20); // Just enable output on Pin 3 and disable it on Pin 11
  TCCR2B = _BV(WGM22) | _BV(CS22);
  OCR2A = 51; // defines the frequency 51 = 38.4 KHz, 54 = 36.2 KHz, 58 = 34 KHz, 62 = 32 KHz
  OCR2B = 26;  // deines the duty cycle - Half the OCR2A value for 50%
  TCCR2B = TCCR2B & 0b00111000 | 0x2; // select a prescale value of 8:1 of the system clock
}


I also use the same code but with different numbers for generating 125KHz signal for an RFID coil.

Quote
What would be really cool is a tone function that had a second setting that would allow you to adjust the duty cycle.

Why are you using the word tome here. You are not looking for a change in audio as you alter the duty cycle are you.
As I have told you before OCR2B changes the duty cycle.

ahooper

Quote
Why are you using the word tome here. You are not looking for a change in audio as you alter the duty cycle are you.
As I have told you before OCR2B changes the duty cycle.


With the coils I am using the first part is to find the ideal frequency they run at, depending on how tight the coil is and if i have used a powdered iron core or just an iron core and how well packed the bundle is i can achieve higher frequencies with less core noise. then I can adjust the duty cycle so as to reduce the current being consumed by the coil and tweak the output to whatever load I have it connected to. No point in over driving the coil if i don't have to.

OCR2B has been tweaked to provide the ideal duty cycle but the frequency I was using was too low and i was not able to hear a change by altering OCR2A.


Grumpy_Mike

The two registers are not independent. You can only make OCR2B a value up to the value of OCR2A, therefore you do not have very much control over the duty cycle when OCR2A is at low values.
The normal PWM sets this register at a maximum and uses other registers like the pre scaller to alter the frequency in lumps to preserve the 256 step PWM resolution.

Go Up