10-min tutorial: What happens when PWM registers overflow? 

 May 8, 2018

By  Peter

As you may know, in the Atmega328 that powers the Arduino Uno, several of the pins are capable of Pulse Width Modulation (PWM). These are pins 3, 5, 6, 9, 10, and 11. With PWM, we can approximate analog output programmatically and do things like fade an LED on and off or control the speed of a motor.

In the Atmega328, the register that is used by the PWM function has a resolution of 8 bits. This gives us a total of 255 possible “analog” output levels. If we attach an LED to a PWM-capable pin, we can drive it to 255 different brightness levels. And if we attach a motor, we can drive it to 255 different speed levels.

We can set a PWM value by using the analogWrite(pin, value) instruction.

So, analogWrite(3, 125) would set pin 3 to value 125. The maximum value is 255 since the PWM register is 8 bits wide (2 in the power of 8 is 255).

What happens if we set analogWrite to a value bigger than 255? Say, 256?

Let’s think about this for a minute.

If the PWM value is 255, the binary version is 11111111 is stored in the PWM register. A connected LED would light up in maximum brightness.

Let’s add 1 to the register, and make the PWM value 256. The binary version of 256 is 0000000100000000 since now we need two bytes to represent this value. But, the Arduino (Atmega) can only fit the first byte in its PWM register, the one in bold. The second byte will overflow and “disappear”.

So, what you have stored in the PWM register is 00000000. This is decimal “0”, which means that your LED is practically turned off.

In other words, analogWrite(3, 0) and analogWrite(3, 256) would have the exact same effect on an LED or a motor.

Add another “1” to the register, and the PWM value now is 257. The binary version of 257 is 0000000100000001. The byte in bold is stored in the PWM register, and the rest disappears. In the register now the decimal value “1” is stored.

The lesson to take home is that although you can set the PWM value in analogWrite to any decimal you like, only the first byte of this number will fit in the PWM register. The rest will overflow and disappear.


10, Min, Overflow, PWM, Registers, Tip, Tutorial

You may also like

{"email":"Email address invalid","url":"Website address invalid","required":"Required field missing"}

I got my first Raspberry Pi in 2012 when this single-board computer made its debut. In the following years, I purchased all models: Raspberry Pi 2, 3, 4, Zero and Zero W. In most cases,

Read More
A smart enclosure for your Raspberry Pi: Pironman

Our new course, Raspberry Pi Full Stack Upgrade Project, is now published! See our Black Friday offer at the bottom of this post. This course is perfect for original Raspberry Pi Full Stack graduates and

Read More
New Course: Raspberry Pi Full Stack Upgrade Project (PLUS our Black Friday offer)