Sunday, November 12

Roll Your Own Arduino PWM

Most projects are built on abstractions. After all, few of us can create our own wire, our own transistors, or our own integrated circuits. A few months ago, [Julian Ilett] found a problem using the Arduino library for PWM. Recently, he revisited the issue and used his own PWM code to fix the problem. You can watch the video below.

Of course, neither the Arduino library nor [Julian’s] code is actually producing PWM. The Atmel CPU’s hardware is doing the work. The Arduino library gives you a wrapper called analogWrite — especially handy if you are not using an Atmel CPU where the same abstraction will do the same work. The issue arose when [Julian] broke the abstraction to invert the PWM output.

The video does a good job of framing the issue. Setting the PWM hardware to zero still causes a one tick output to occur. That is, the actual count is the count you supply plus one. That’s great on the high end where 255 is treated as 256 out of 256. But at the low end, a zero counterintuitively gives you 1/256. The Arduino library authors elected to detect that edge case and just force the output pin to go low in that case. When inverted, however, the pin still goes low when it ought to go high. You can see the source code responsible, below.

pinMode(pin, OUTPUT);
if (val == 0)
  {
  digitalWrite(pin, LOW);
  }
else if (val == 255)
  {
  digitalWrite(pin, HIGH);
  }
else
{ ...

Oddly, the 255 case appears to be superfluous in the normal case but is also backward if you invert the output. In all fairness, the Arduino library doesn’t provide you a way to invert the output, so you’ve already broken the abstraction and that’s why this isn’t technically a bug in the library.

[Julian’s] code is quite simple. There’s initial set up of the TCCR1A and TCCR1B registers along with ICR1. The DDRB register sets the pin as an output. After that, writing to OCR1A and OCR1B set the PWM value. The video explains it all in great detail.

We’ve looked at PWM on FPGAs at least once, and that post gives some background on PWM in any application. We also have our own video from way back in 2011 about PWM.


Filed under: Arduino Hacks

No comments:

Post a Comment