# ATtiny85 and high frequency PWM for driving a ferrite transformer

Some background: I have an ATtiny85 running at 8 MHz under the Arduino IDE. I have been gifted a large ferrite transformer and some MOSFETs & drivers to drive it. However I am lacking a suitable PWM signal to actually make it work. I tried using analog write, however the current quickly got out of control due to the switching frequency simply being far too slow.

I know a little bit about programming here and there. I've also worked with the Arduino Uno, which I am also using as a programmer for the ATtiny. However I have a limited understanding of the inner workings of microprocessors. So I looked for a library to make all of this work, and I found this:

https://github.com/micooke/PWM

However, I am still unsure how to actually make this work, or adjust the pulse width with a potentiometer. Think a 4047 with a deadlocked frequency, but with an adjustable pulse width. To keep it simple I don't have a clue how to make this work, and it is driving me crazy.

• 1) define what you mean by "High Frequency PWM", 1 kHz, 100 kHz , 1 MHz? Any uC has certain limitations. Be aware of those limitations before you want something from a uC, it might simply be unable to deliver. I have a limited understanding of the inner workings of microprocessors I do not see how that is relevant, being able to use it does not mean you need to know how it does what it does. I don't have a clue Well, get the clue then, look for projects which use a potmeter and a uC, learn how they read out the potmeter. Look at projects using PWM, see how they do it. – Bimpelrekkie Sep 22 '17 at 7:05
• The default PWM frequency of the Arduino library is somewhere around 400Hz at 8 MHz clock freq IIRC. To go higher, you need to start manipulating the registries directly, and then you might as well abandon AnalogWrite altogether and use the registries to set the duty cycle as well. Take some time to study the datasheet. That part is gonna take you at least a few hours. Maybe days or weeks, depending on how quickly you are able to find, absorb, and organize the information therein. – Dampmaskin Sep 22 '17 at 7:11
• According to my friend 32khz is around what I am looking for. – Atton Mathews Sep 22 '17 at 7:50
• How do you plan to drive the MOSFETs from your Arduino? What topology? Do you know the parameters of your transformer? – winny Sep 22 '17 at 8:04
• @MarcusMuller XY problem? – winny Sep 22 '17 at 18:22

In an ATtinyXX there are two timers. Timer0 can be clocked at the same CPU frequency, by setting the corresponding bits (see below) in the TCCR0B (for timer 0).

At 8 MHz and 8 bit, the PWM will have a frequency of $\frac{8\ MHz}{2^8} = 31.25\ kHz$.

Timer 1, instead, can be also clocked by the PLL (up to 64 MHz), which multiplies the internal frequency by 8, yielding a maximum frequency of $\frac{64\ MHz}{2^8}= 250\ kHz$.

Here's the timer 0 control register B:

And here's the CS0x setting table:

Here's the timer 1 control register:

There you must set the correct clock prescaler value according to the table:

To enable PLL you must set bits PCKE and PLLE in PLLCSR.

• Well that looks like fun – Atton Mathews Sep 22 '17 at 8:03

There is a fairly good "turn-to" PWM chip that can have its oscillation frequency set with a resistor and it has a 0 to 1 volt dc control voltage to give you 0% to 100% on the PWM output: -

I have a limited understanding of the inner workings of microprocessors

This is a good starting point if you have a DAC output that can generate the control voltage.