16 Bit PWM output on an Arduino?

16 Bit PWM output on an Arduino?

Following on with step 1a from the article: Making a high resolution ADC from an Arduino Mini Pro

In order to make a high resolution DC source we need a 16 bit PWM output from the Arduino. The standard libraries give you access to 8bits on the PWM pins, this only gives 256 discrete levels, if you had 16bits it will yield 65535 steps. Timer1 on the Mini Pro and UNO is capable of going into 16bit mode. With just a little Googling I found a library that give access to the 16bit timer modes, as well as letting you reprogram the PWM output frequency, the library was created by the user ‘runnerup’  AKA Sam Knight (as the copyright header in the lib indicates) in the Arduino forums: Here

The library is located here: PWM Frequency Library , the library also is supposed to work on both  the Arduino Mega and UNO.

Timer1 is the only 16bit timer on the atmega328 (UNO and Mini Pro) and it only comes out on two pins, 9 and 10. If you were to use the Mega  it has many more pins capable of 16 bit mode on the PWM outputs. I got the below timer/bit/pins table from: Advanced Arduino: direct use of ATmega counter/timers

timer bits channel Arduino pin Mega pin
timer0 8 A 6 13
timer0 8 B 5 4
timer1 16 A 9 11
timer1 16 B 10 12
timer2 8 A 11 10
timer2 8 B 3 9
timer3 16 A 5
timer3 16 B 2
timer3 16 C 3
timer4 16 A 6
timer4 16 B 7
timer4 16 C 8
timer5 16 A 44
timer5 16 B 45
timer5 16 C 46

 

Being able to reprogram the frequency is very important since it has an effect on the actual resolution that can be achieved.  See the table at the bottom for an output from the demo program from the PWM Frequency library that shows the differences for frequency to resolution. Since 131 Hz gives almost 16 bits of resolution at 61069 steps, this is what frequency I am going to use as I experiment. This will give approximately 81uV DC filtered step sizes on average using a 5V Mini Pro, we will try to verify this later, the best meter I have here is good for 100uV resolution in the 11V range, I will try to borrow one that has higher resolution in the next couple of weeks.

 

So lets try it out…

 

I wrote a quick program to try a few things: first did the frequency set?  second does high resolution PWM work on both channels 9 and 10 independently ( there was a note in the library that you would lose the second channel )?  third is it really high resolution?

We set PWM on channel 9 and 10 separately at a frequency of 131HZ  ( reciprocal of 131Hz is 0.0076335…. seconds) the period should be about 7.63mS. See screen shot, yes to both of those!

Arduino Mini Pro running 131Hz 16Bit mode PWM
Arduino Mini Pro running 131Hz 16Bit mode PWM

 

 

 

 

 

 

 

Now does the 16 bit resolution work. The reciprocal of 131Hz divided by 61069 steps is ~125nS.  This picture shows where I set the PWM to a setting of 1. Yes it works!

Arduino Mini Pro running 131Hz 16Bit mode PWM 125ns
Arduino Mini Pro running 131Hz 16Bit mode PWM 125ns

 

 

 

 

 

 

Here it is with PWM set to 2 and yes it is now ~250nS

Arduino Mini Pro running 131Hz 16Bit mode PWM 250ns
Arduino Mini Pro running 131Hz 16Bit mode PWM 250ns

 

 

 

 

 

 

 

Now onto designing a filter to get DC out of this….

 

 

 

 

 

Demonstrate Frequency Effect On Resolution Table ( I only ran it to 3931 HZ, but you get the idea….)

1 Hz 7813 steps 12 bits
11 Hz 2841 steps 11 bits
21 Hz 5953 steps 12 bits
31 Hz 4033 steps 11 bits
41 Hz 3049 steps 11 bits
51 Hz 2451 steps 11 bits
61 Hz 2050 steps 11 bits
71 Hz 14085 steps 13 bits
81 Hz 12346 steps 13 bits
91 Hz 10990 steps 13 bits
101 Hz 9901 steps 13 bits
111 Hz 9010 steps 13 bits
121 Hz 8265 steps 13 bits
131 Hz 61069 steps 15 bits
141 Hz 56738 steps 15 bits
151 Hz 52981 steps 15 bits
161 Hz 49690 steps 15 bits
171 Hz 46784 steps 15 bits
181 Hz 44199 steps 15 bits
191 Hz 41885 steps 15 bits
201 Hz 39801 steps 15 bits
211 Hz 37915 steps 15 bits
221 Hz 36200 steps 15 bits
231 Hz 34633 steps 15 bits
241 Hz 33196 steps 15 bits
251 Hz 31873 steps 14 bits
261 Hz 30652 steps 14 bits
271 Hz 29521 steps 14 bits
281 Hz 28470 steps 14 bits
291 Hz 27492 steps 14 bits
301 Hz 26579 steps 14 bits
311 Hz 25724 steps 14 bits
321 Hz 24923 steps 14 bits
331 Hz 24170 steps 14 bits
341 Hz 23461 steps 14 bits
351 Hz 22793 steps 14 bits
361 Hz 22161 steps 14 bits
371 Hz 21564 steps 14 bits
381 Hz 20998 steps 14 bits
391 Hz 20461 steps 14 bits
401 Hz 19951 steps 14 bits
411 Hz 19465 steps 14 bits
421 Hz 19003 steps 14 bits
431 Hz 18562 steps 14 bits
441 Hz 18141 steps 14 bits
451 Hz 17739 steps 14 bits
461 Hz 17354 steps 14 bits
471 Hz 16986 steps 14 bits
481 Hz 16633 steps 14 bits
491 Hz 16294 steps 13 bits
501 Hz 15969 steps 13 bits
511 Hz 15656 steps 13 bits
521 Hz 15356 steps 13 bits
531 Hz 15066 steps 13 bits
541 Hz 14788 steps 13 bits
551 Hz 14520 steps 13 bits
561 Hz 14261 steps 13 bits
571 Hz 14011 steps 13 bits
581 Hz 13770 steps 13 bits
591 Hz 13537 steps 13 bits
601 Hz 13312 steps 13 bits
611 Hz 13094 steps 13 bits
621 Hz 12883 steps 13 bits
631 Hz 12679 steps 13 bits
641 Hz 12481 steps 13 bits
651 Hz 12289 steps 13 bits
661 Hz 12103 steps 13 bits
671 Hz 11923 steps 13 bits
681 Hz 11748 steps 13 bits
691 Hz 11578 steps 13 bits
701 Hz 11413 steps 13 bits
711 Hz 11252 steps 13 bits
721 Hz 11096 steps 13 bits
731 Hz 10944 steps 13 bits
741 Hz 10797 steps 13 bits
751 Hz 10653 steps 13 bits
761 Hz 10513 steps 13 bits
771 Hz 10377 steps 13 bits
781 Hz 10244 steps 13 bits
791 Hz 10114 steps 13 bits
801 Hz 9988 steps 13 bits
811 Hz 9865 steps 13 bits
821 Hz 9745 steps 13 bits
831 Hz 9627 steps 13 bits
841 Hz 9513 steps 13 bits
851 Hz 9401 steps 13 bits
861 Hz 9292 steps 13 bits
871 Hz 9185 steps 13 bits
881 Hz 9081 steps 13 bits
891 Hz 8979 steps 13 bits
901 Hz 8880 steps 13 bits
911 Hz 8782 steps 13 bits
921 Hz 8687 steps 13 bits
931 Hz 8593 steps 13 bits
941 Hz 8502 steps 13 bits
951 Hz 8413 steps 13 bits
961 Hz 8325 steps 13 bits
971 Hz 8239 steps 13 bits
981 Hz 8155 steps 12 bits
991 Hz 8073 steps 12 bits
1001 Hz 7993 steps 12 bits
1011 Hz 7913 steps 12 bits
1021 Hz 7836 steps 12 bits
1031 Hz 7760 steps 12 bits
1041 Hz 7685 steps 12 bits
1051 Hz 7612 steps 12 bits
1061 Hz 7541 steps 12 bits
1071 Hz 7470 steps 12 bits
1081 Hz 7401 steps 12 bits
1091 Hz 7333 steps 12 bits
1101 Hz 7267 steps 12 bits
1111 Hz 7201 steps 12 bits
1121 Hz 7137 steps 12 bits
1131 Hz 7074 steps 12 bits
1141 Hz 7012 steps 12 bits
1151 Hz 6951 steps 12 bits
1161 Hz 6891 steps 12 bits
1171 Hz 6832 steps 12 bits
1181 Hz 6774 steps 12 bits
1191 Hz 6718 steps 12 bits
1201 Hz 6662 steps 12 bits
1211 Hz 6607 steps 12 bits
1221 Hz 6553 steps 12 bits
1231 Hz 6499 steps 12 bits
1241 Hz 6447 steps 12 bits
1251 Hz 6395 steps 12 bits
1261 Hz 6345 steps 12 bits
1271 Hz 6295 steps 12 bits
1281 Hz 6246 steps 12 bits
1291 Hz 6197 steps 12 bits
1301 Hz 6150 steps 12 bits
1311 Hz 6103 steps 12 bits
1321 Hz 6057 steps 12 bits
1331 Hz 6011 steps 12 bits
1341 Hz 5966 steps 12 bits
1351 Hz 5922 steps 12 bits
1361 Hz 5879 steps 12 bits
1371 Hz 5836 steps 12 bits
1381 Hz 5793 steps 12 bits
1391 Hz 5752 steps 12 bits
1401 Hz 5711 steps 12 bits
1411 Hz 5670 steps 12 bits
1421 Hz 5630 steps 12 bits
1431 Hz 5591 steps 12 bits
1441 Hz 5552 steps 12 bits
1451 Hz 5514 steps 12 bits
1461 Hz 5476 steps 12 bits
1471 Hz 5439 steps 12 bits
1481 Hz 5402 steps 12 bits
1491 Hz 5366 steps 12 bits
1501 Hz 5330 steps 12 bits
1511 Hz 5295 steps 12 bits
1521 Hz 5260 steps 12 bits
1531 Hz 5226 steps 12 bits
1541 Hz 5192 steps 12 bits
1551 Hz 5158 steps 12 bits
1561 Hz 5125 steps 12 bits
1571 Hz 5093 steps 12 bits
1581 Hz 5061 steps 12 bits
1591 Hz 5029 steps 12 bits
1601 Hz 4997 steps 12 bits
1611 Hz 4966 steps 12 bits
1621 Hz 4936 steps 12 bits
1631 Hz 4905 steps 12 bits
1641 Hz 4876 steps 12 bits
1651 Hz 4846 steps 12 bits
1661 Hz 4817 steps 12 bits
1671 Hz 4788 steps 12 bits
1681 Hz 4760 steps 12 bits
1691 Hz 4731 steps 12 bits
1701 Hz 4704 steps 12 bits
1711 Hz 4676 steps 12 bits
1721 Hz 4649 steps 12 bits
1731 Hz 4622 steps 12 bits
1741 Hz 4596 steps 12 bits
1751 Hz 4569 steps 12 bits
1761 Hz 4543 steps 12 bits
1771 Hz 4518 steps 12 bits
1781 Hz 4492 steps 12 bits
1791 Hz 4467 steps 12 bits
1801 Hz 4442 steps 12 bits
1811 Hz 4418 steps 12 bits
1821 Hz 4394 steps 12 bits
1831 Hz 4370 steps 12 bits
1841 Hz 4346 steps 12 bits
1851 Hz 4322 steps 12 bits
1861 Hz 4299 steps 12 bits
1871 Hz 4276 steps 12 bits
1881 Hz 4254 steps 12 bits
1891 Hz 4231 steps 12 bits
1901 Hz 4209 steps 12 bits
1911 Hz 4187 steps 12 bits
1921 Hz 4165 steps 12 bits
1931 Hz 4143 steps 12 bits
1941 Hz 4122 steps 12 bits
1951 Hz 4101 steps 12 bits
1961 Hz 4080 steps 11 bits
1971 Hz 4059 steps 11 bits
1981 Hz 4039 steps 11 bits
1991 Hz 4019 steps 11 bits
2001 Hz 3999 steps 11 bits
2011 Hz 3979 steps 11 bits
2021 Hz 3959 steps 11 bits
2031 Hz 3939 steps 11 bits
2041 Hz 3920 steps 11 bits
2051 Hz 3901 steps 11 bits
2061 Hz 3882 steps 11 bits
2071 Hz 3863 steps 11 bits
2081 Hz 3845 steps 11 bits
2091 Hz 3826 steps 11 bits
2101 Hz 3808 steps 11 bits
2111 Hz 3790 steps 11 bits
2121 Hz 3772 steps 11 bits
2131 Hz 3755 steps 11 bits
2141 Hz 3737 steps 11 bits
2151 Hz 3720 steps 11 bits
2161 Hz 3702 steps 11 bits
2171 Hz 3685 steps 11 bits
2181 Hz 3669 steps 11 bits
2191 Hz 3652 steps 11 bits
2201 Hz 3635 steps 11 bits
2211 Hz 3619 steps 11 bits
2221 Hz 3602 steps 11 bits
2231 Hz 3586 steps 11 bits
2241 Hz 3570 steps 11 bits
2251 Hz 3554 steps 11 bits
2261 Hz 3539 steps 11 bits
2271 Hz 3523 steps 11 bits
2281 Hz 3508 steps 11 bits
2291 Hz 3492 steps 11 bits
2301 Hz 3477 steps 11 bits
2311 Hz 3462 steps 11 bits
2321 Hz 3447 steps 11 bits
2331 Hz 3433 steps 11 bits
2341 Hz 3418 steps 11 bits
2351 Hz 3403 steps 11 bits
2361 Hz 3389 steps 11 bits
2371 Hz 3375 steps 11 bits
2381 Hz 3360 steps 11 bits
2391 Hz 3346 steps 11 bits
2401 Hz 3332 steps 11 bits
2411 Hz 3319 steps 11 bits
2421 Hz 3305 steps 11 bits
2431 Hz 3291 steps 11 bits
2441 Hz 3278 steps 11 bits
2451 Hz 3264 steps 11 bits
2461 Hz 3251 steps 11 bits
2471 Hz 3238 steps 11 bits
2481 Hz 3225 steps 11 bits
2491 Hz 3212 steps 11 bits
2501 Hz 3199 steps 11 bits
2511 Hz 3186 steps 11 bits
2521 Hz 3174 steps 11 bits
2531 Hz 3161 steps 11 bits
2541 Hz 3149 steps 11 bits
2551 Hz 3137 steps 11 bits
2561 Hz 3124 steps 11 bits
2571 Hz 3112 steps 11 bits
2581 Hz 3100 steps 11 bits
2591 Hz 3088 steps 11 bits
2601 Hz 3076 steps 11 bits
2611 Hz 3064 steps 11 bits
2621 Hz 3053 steps 11 bits
2631 Hz 3041 steps 11 bits
2641 Hz 3030 steps 11 bits
2651 Hz 3018 steps 11 bits
2661 Hz 3007 steps 11 bits
2671 Hz 2996 steps 11 bits
2681 Hz 2984 steps 11 bits
2691 Hz 2973 steps 11 bits
2701 Hz 2962 steps 11 bits
2711 Hz 2951 steps 11 bits
2721 Hz 2941 steps 11 bits
2731 Hz 2930 steps 11 bits
2741 Hz 2919 steps 11 bits
2751 Hz 2909 steps 11 bits
2761 Hz 2898 steps 11 bits
2771 Hz 2888 steps 11 bits
2781 Hz 2877 steps 11 bits
2791 Hz 2867 steps 11 bits
2801 Hz 2857 steps 11 bits
2811 Hz 2846 steps 11 bits
2821 Hz 2836 steps 11 bits
2831 Hz 2826 steps 11 bits
2841 Hz 2816 steps 11 bits
2851 Hz 2807 steps 11 bits
2861 Hz 2797 steps 11 bits
2871 Hz 2787 steps 11 bits
2881 Hz 2777 steps 11 bits
2891 Hz 2768 steps 11 bits
2901 Hz 2758 steps 11 bits
2911 Hz 2749 steps 11 bits
2921 Hz 2739 steps 11 bits
2931 Hz 2730 steps 11 bits
2941 Hz 2721 steps 11 bits
2952 Hz 2711 steps 11 bits
2961 Hz 2702 steps 11 bits
2971 Hz 2693 steps 11 bits
2981 Hz 2684 steps 11 bits
2991 Hz 2675 steps 11 bits
3001 Hz 2666 steps 11 bits
3012 Hz 2657 steps 11 bits
3021 Hz 2649 steps 11 bits
3031 Hz 2640 steps 11 bits
3041 Hz 2631 steps 11 bits
3051 Hz 2623 steps 11 bits
3061 Hz 2614 steps 11 bits
3071 Hz 2606 steps 11 bits
3081 Hz 2597 steps 11 bits
3091 Hz 2589 steps 11 bits
3101 Hz 2580 steps 11 bits
3111 Hz 2572 steps 11 bits
3121 Hz 2564 steps 11 bits
3131 Hz 2556 steps 11 bits
3142 Hz 2547 steps 11 bits
3152 Hz 2539 steps 11 bits
3162 Hz 2531 steps 11 bits
3172 Hz 2523 steps 11 bits
3182 Hz 2515 steps 11 bits
3191 Hz 2508 steps 11 bits
3201 Hz 2500 steps 11 bits
3211 Hz 2492 steps 11 bits
3221 Hz 2484 steps 11 bits
3231 Hz 2477 steps 11 bits
3241 Hz 2469 steps 11 bits
3252 Hz 2461 steps 11 bits
3261 Hz 2454 steps 11 bits
3271 Hz 2446 steps 11 bits
3281 Hz 2439 steps 11 bits
3292 Hz 2431 steps 11 bits
3301 Hz 2424 steps 11 bits
3311 Hz 2417 steps 11 bits
3322 Hz 2409 steps 11 bits
3331 Hz 2402 steps 11 bits
3341 Hz 2395 steps 11 bits
3351 Hz 2388 steps 11 bits
3361 Hz 2381 steps 11 bits
3371 Hz 2374 steps 11 bits
3381 Hz 2367 steps 11 bits
3391 Hz 2360 steps 11 bits
3401 Hz 2353 steps 11 bits
3411 Hz 2346 steps 11 bits
3421 Hz 2339 steps 11 bits
3432 Hz 2332 steps 11 bits
3442 Hz 2325 steps 11 bits
3451 Hz 2319 steps 11 bits
3461 Hz 2312 steps 11 bits
3472 Hz 2305 steps 11 bits
3481 Hz 2299 steps 11 bits
3491 Hz 2292 steps 11 bits
3501 Hz 2286 steps 11 bits
3511 Hz 2279 steps 11 bits
3521 Hz 2273 steps 11 bits
3532 Hz 2266 steps 11 bits
3541 Hz 2260 steps 11 bits
3552 Hz 2253 steps 11 bits
3561 Hz 2247 steps 11 bits
3571 Hz 2241 steps 11 bits
3581 Hz 2235 steps 11 bits
3592 Hz 2228 steps 11 bits
3601 Hz 2222 steps 11 bits
3611 Hz 2216 steps 11 bits
3621 Hz 2210 steps 11 bits
3631 Hz 2204 steps 11 bits
3641 Hz 2198 steps 11 bits
3651 Hz 2192 steps 11 bits
3661 Hz 2186 steps 11 bits
3671 Hz 2180 steps 11 bits
3681 Hz 2174 steps 11 bits
3691 Hz 2168 steps 11 bits
3701 Hz 2162 steps 11 bits
3712 Hz 2156 steps 11 bits
3722 Hz 2150 steps 11 bits
3731 Hz 2145 steps 11 bits
3741 Hz 2139 steps 11 bits
3752 Hz 2133 steps 11 bits
3761 Hz 2128 steps 11 bits
3771 Hz 2122 steps 11 bits
3782 Hz 2116 steps 11 bits
3791 Hz 2111 steps 11 bits
3802 Hz 2105 steps 11 bits
3811 Hz 2100 steps 11 bits
3822 Hz 2094 steps 11 bits
3831 Hz 2089 steps 11 bits
3842 Hz 2083 steps 11 bits
3851 Hz 2078 steps 11 bits
3861 Hz 2073 steps 11 bits
3872 Hz 2067 steps 11 bits
3881 Hz 2062 steps 11 bits
3891 Hz 2057 steps 11 bits
3902 Hz 2051 steps 11 bits
3911 Hz 2046 steps 10 bits
3921 Hz 2041 steps 10 bits
3931 Hz 2036 steps 10 bits
Comments are closed.