Skip to main content
Arduino

You are not logged in. Your edit will be placed in a queue until it is peer reviewed.

We welcome edits that make the post easier to understand and more valuable for readers. Because community members review edits, please try to make the post substantially better than how you found it, for example, by fixing grammar or adding additional resources and hyperlinks.

Required fields*

Increase PWM bit resolution

I would like to increase the PWM bit resolution of the Arduino Uno. At this moment it is 8-bit which I consider too low. Is this possible without losing the ability of interrupts and delays?

Koen

EDIT This setup delivers a 16-bit resultion

void setupPWM16() {
 DDRB |= _BV(PB1) | _BV(PB2); /* set pins as outputs */
 TCCR1A = _BV(COM1A1) | _BV(COM1B1) /* non-inverting PWM */
 | _BV(WGM11); /* mode 14: fast PWM, TOP=ICR1 */
 TCCR1B = _BV(WGM13) | _BV(WGM12)
 | _BV(CS11); /* prescaler: clock / 8 */
 ICR1 = 0xffff; /* TOP counter value (freeing OCR1A*/
}
/* Comments about the setup
Changing ICR1 will effect the amount of bits of resolution.
ICR1 = 0xffff; (65535) 16-bit resolution
ICR1 = 0x7FFF; (32767) 15-bit resolution
ICR1 = 0x3FFF; (16383) 14-bit resolution etc....
Changing the prescaler will effect the frequency of the PWM signal.
Frequency[Hz}=CPU/(ICR1+1) where in this case CPU=16 MHz
16-bit PWM will be>>> (16000000/8)/(65535+1)=30.5175Hz
*/
/* 16-bit version of analogWrite(). Works only on pins 9 and 10. */
void analogWrite16(uint8_t pin, uint16_t val)
{
 switch (pin) {
 case 9: OCR1A = val; break;
 case 10: OCR1B = val; break;
 }
}

Answer*

Draft saved
Draft discarded
Cancel
5
  • This is clever! Seems that the Mozzi sound synthesis library uses this trick for it's so called "HIFI" mode. Commented Jun 17, 2015 at 19:40
  • That is a great us of the PWM. But wouldn't this smooth out the waveform? I am asking this since you are using a RC filter. Haven't mentioned this in my question but I'm driving a DC motor with it <feeling ashamed>. Thank you for the input! Commented Jun 18, 2015 at 6:14
  • @KoenR (fwiw: I don't see anything to be ashamed of.) I don't know what frequency response/rate of change you want in your ADC output. Or why you want N bits or how big is enough. Motors will usually not be usefully controlled by more than 8 bits - depends on how precision an application you have. Motor acts as part of a smoothing filter due to inductance. You need to say what sort of motor and how driven. And a circuit diagram is about essential. Unless motor is tiny you have a driver. A brushed motor fed PWM must have a a catch diode to pass motor current when PWM is off. Adding two ... Commented Jun 18, 2015 at 9:01
  • ... PWMs here is entiely doable but circuit details need to be known. Commented Jun 18, 2015 at 9:01
  • Beware! In some cases, smoothing out PWM with a low pass RC is not desirable. E.g., if you plug the Arduino output into the gate of a MOSFET, the MOSFET will stay cold as long as it's driven by clean PWM. But if you smooth it out, the MOSFET will start dissipating much more heat. Sometimes that's not a good thing. Commented Jul 13, 2015 at 6:46

lang-cpp

AltStyle によって変換されたページ (->オリジナル) /