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

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?

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.

## What happens if you give a negative PWM value to analogWrite()?

To understand what happens when you give a negative PWM value to analogWrite we need to look into how the Arduino deals with negative numbers,…

## Arduino basics – How to use the potentiometer

In this post I’ll show you how to use a potentiometer. A potentiometer is a device that allows you to change the value of a…

## What is “baud”?

The unit “baud” is the speed of communication between the two devices. Your computer and the Arduino talk via the USB/TTL (serial) interface. There are…

## Arduino – A dive into the fundamentals of the blinking LED

The majority of Arduino buyers purchase an Arduino kit that contains an Arduino circuit board and a few hardware parts to test and examine. The…

## Arduino basics – How to use the passive buzzer

A buzzer is a device that can generate simple tones, typically used to provide audible feedback to the user. Buzzers are used in alarm clocks,…