Arduino

Reference Language | Libraries | Comparison | Changes

## analogWriteResolution()

#### Description

analogWriteResolution() is an extension of the Analog API for the Arduino Due and Zero.

analogWriteResolution() sets the resolution of the analogWrite() function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards.

The Due has the following hardare capabilities:

- 12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution.
- 2 pins with 12-bit DAC (Digital-to-Analog Converter)

By setting the write resolution to 12, you can use analogWrite() with values between 0 and 4095 to exploit the full DAC resolution or to set the PWM signal without rolling over.

The Zero has the following hardare capabilities:

- 10 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution.
- 1 pin with 10-bit DAC (Digital-to-Analog Converter).

By setting the write resolution to 10, you can use analogWrite() with values between 0 and 1023 to exploit the full DAC resolution

#### Syntax

analogWriteResolution(bits)

#### Parameters

bits: determines the resolution (in bits) of the values used in the analogWrite() function. The value can range from 1 to 32. If you choose a resolution higher or lower than your board's hardware capabilities, the value used in analogWrite() will be either truncated if it's too high or padded with zeros if it's too low. See the note below for details.

#### Returns

None.

#### Note

If you set the analogWriteResolution() value to a value higher than your board's capabilities, the Arduino will discard the extra bits. For example: using the Due with analogWriteResolution(16) on a 12-bit DAC pin, only the first 12 bits of the values passed to analogWrite() will be used and the last 4 bits will be discarded.

If you set the analogWriteResolution() value to a value lower than your board's capabilities, the missing bits will be padded with zeros to fill the hardware required size. For example: using the Due with analogWriteResolution(8) on a 12-bit DAC pin, the Arduino will add 4 zero bits to the 8-bit value used in analogWrite() to obtain the 12 bits required.

#### Example

void setup(){
// open a serial connection
Serial.begin(9600);
// make our digital pin an output
pinMode(11, OUTPUT);
pinMode(12, OUTPUT);
pinMode(13, OUTPUT);
}
void loop(){
// read the input on A0 and map it to a PWM pin
// with an attached LED
int sensorVal = analogRead(A0);
Serial.print("Analog Read) : ");
Serial.print(sensorVal);
// the default PWM resolution
analogWriteResolution(8);
analogWrite(11, map(sensorVal, 0, 1023, 0 ,255));
Serial.print(" , 8-bit PWM value : ");
Serial.print(map(sensorVal, 0, 1023, 0 ,255));
// change the PWM resolution to 12 bits
// the full 12 bit resolution is only supported
// on the Due
analogWriteResolution(12);
analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
Serial.print(" , 12-bit PWM value : ");
Serial.print(map(sensorVal, 0, 1023, 0, 4095));
// change the PWM resolution to 4 bits
analogWriteResolution(4);
analogWrite(13, map(sensorVal, 0, 1023, 0, 127));
Serial.print(", 4-bit PWM value : ");
Serial.println(map(sensorVal, 0, 1023, 0, 127));
delay(5);
}
[Get Code]

#### See also

- Description of the analog input pins
- analogWrite()
- analogRead()
- map()

Reference Home

Corrections, suggestions, and new documentation should be posted to the Forum.

The text of the Arduino reference is licensed under a Creative Commons Attribution-ShareAlike 3.0 License. Code samples in the reference are released into the public domain.

### Share

### This link has expired.
Please re-subscribe to our Newsletters.

### Subscribe to our Newsletters

Email

Please enter a valid email to subscribe

Arduino Newsletter
Arduino Store Newsletter

### Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy

###### Newsletter

- ©2015 Arduino
- Copyright Notice
- Contact us
- About us
- Careers