# 10 bit PWM on Mega 2560

Hi everyone,

I'm building a resistor bank to test batteries at the moment. I can measure ampere with an accuracy of 0.5 A. Unfortunately the 8 bit PWM limits my ability to control amps to an accuracy of +- 2 A.
There are some posts about 10 bit PWM on the Arduino.

Here is what I found out:

I use PWM on Pin 6 and 7, that is Timer4, if I'm right.
Changing the frequency is easily done:

clear the last 3 bits of the 8 bit number with
TCCR4B &= ~7;
set prescaler
TCCR4B |= n;
with n = 1 for 31000 Hz, n = 2 for 4000 Hz, n = 3 for 490 Hz (default), n = 4 for 120 Hz, n = 5 for 30 Hz and n = 6 for 20 Hz
I got it from here.

That's what I got working.

According to this post it should be possible to change the PWM resolution to 10 bit, but I have absolutely no idea how to do that.

Do I have to change sbi(TCCR4A, WGM40) to sbi(TCCR4A, WGM41) in wiring.c?

Is there a way to change that in my program? I don't want to mess around with the arduino code and break something.

Maybe someone can point me in the right direction.

Thanks a lot

Time to read all about the 16-bit Counter/Timer registers in the datasheet. There are 15 PWM modes, most of them are 16-bit but there are two 10-bit PWM modes: Fast and Phase Correct. Pick one and learn what registers to set and how.

Is there another way, but the hard way?

I'm new to microcontroller, but I'm willing to learn...

16 bit resolution would be even better. I don't care about the exact waveform. Or should I when driving mosfets?

What I need is 10 or more bit resolution and the ability to set the frequency.

On page 148 in the datasheet I found the Modes of Operation. Is that the right place to look?

Mode 8 - 15 look like they are 16-bit.

So I need to set WGMn3, WGMn2, WGMn1 and WGMn0, right?

On the Secrets of Arduino PWM page the Mode is set by _BV(XXXXX), but when I write something like:

TCCR4A = _BV(WGM43) | _BV(WGM41) | _BV(WGM40);

the Arduino freezes. That's not the right way, is it?

Do you need a fairly exact frequency or just something in a general range? What frequency or frequencies do you want?

I think that Fast mode 16-bir PWM will have a maximum frequency of 244 Hz. Phase Correct mode will be half that.

I think that Fast mode 10-bit PWM will have a maximum frequency of 15,625 Hz.