Arduino Programming Language: AnalogWriteResolution

Name

analogWriteResolution()

Description

analogWriteResolution() is an extension of the Analog API for the Arduino Due/M0/M0 Pro and Primo. 
This function sets the resolution of the analogWrite() function. 
It defaults to 8 bits (values between 0-255)but in Arduino Due /M0/M0 Pro is possible to set the write resolution to 12, with values between 0 and 4095 to exploit the full DAC resolution or to set the PWM signal without rolling over.
Also for the Arduino Primo, it is set to default to 8 bits, but it is possible to change it to 10 or 12 bits.

 

Syntax

analogWriteResolution(bit)

Parameters

  • bit: the resolution (in bits) of the values used in the analogWrite()function.

Returns

none 

Usage

Application

Note

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.  

Examples

void setup(){
Serial.begin(9600);
pinMode(11, OUTPUT);
}

void loop(){
// read the input on A0 and map it to a PWM pin
// with an attached LED
int sensorValue = analogRead(A0);
Serial.print("Analog Read) : ");
Serial.print(sensorValue);

// change the PWM resolution to 12 bits
// the full 12 bit resolution is only supported
// on the Due
analogWriteResolution(12);
analogWrite(11, map(sensorValue, 0, 1023, 0, 4095));
Serial.print(" , 12-bit PWM value : ");
Serial.print(map(sensorValue, 0, 1023, 0, 4095));
}


Based on the reference originally written by Arduino Community,  wiring.org.co and  arduino.cc