Skip to content

Commit 3693e29

Browse files
author
Akshay Sharma
committed
Add content Functions/Arduino Due only
1 parent 7dccfa9 commit 3693e29

File tree

2 files changed

+230
-0
lines changed

2 files changed

+230
-0
lines changed
Lines changed: 110 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
= analogReadResolution()
6+
7+
8+
// OVERVIEW SECTION STARTS
9+
[#overview]
10+
--
11+
12+
[float]
13+
=== Description
14+
analogReadResolution() is an extension of the Analog API for the Arduino Due.
15+
16+
Sets the size (in bits) of the value returned by analogRead(). It defaults to 10 bits (returns values between 0-1023) for backward compatibility with AVR based boards.
17+
18+
The *Due has 12-bit ADC capabilities* that can be accessed by changing the resolution to 12. This will return values from analogRead() between 0 and 4095.
19+
[%hardbreaks]
20+
21+
22+
[float]
23+
=== Syntax
24+
`analogReadResolution(bits)`
25+
26+
27+
[float]
28+
=== Parameters
29+
`bits`: determines the resolution (in bits) of the value returned by `analogRead()` function. You can set this 1 and 32. You can set resolutions higher than 12 but values returned by `analogRead()` will suffer approximation. See the note below for details.
30+
31+
[float]
32+
=== Returns
33+
Nothing
34+
35+
--
36+
// OVERVIEW SECTION ENDS
37+
38+
39+
40+
41+
// HOW TO USE SECTION STARTS
42+
[#howtouse]
43+
--
44+
45+
[float]
46+
=== Example Code
47+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
48+
The code shows how to use ADC with different resolutions.
49+
50+
[source,arduino]
51+
----
52+
void setup() {
53+
// open a serial connection
54+
Serial.begin(9600);
55+
}
56+
57+
void loop() {
58+
// read the input on A0 at default resolution (10 bits)
59+
// and send it out the serial connection
60+
analogReadResolution(10);
61+
Serial.print("ADC 10-bit (default) : ");
62+
Serial.print(analogRead(A0));
63+
64+
// change the resolution to 12 bits and read A0
65+
analogReadResolution(12);
66+
Serial.print(", 12-bit : ");
67+
Serial.print(analogRead(A0));
68+
69+
// change the resolution to 16 bits and read A0
70+
analogReadResolution(16);
71+
Serial.print(", 16-bit : ");
72+
Serial.print(analogRead(A0));
73+
74+
// change the resolution to 8 bits and read A0
75+
analogReadResolution(8);
76+
Serial.print(", 8-bit : ");
77+
Serial.println(analogRead(A0));
78+
79+
// a little delay to not hog serial monitor
80+
delay(100);
81+
}
82+
----
83+
[%hardbreaks]
84+
85+
[float]
86+
=== Notes and Warnings
87+
If you set the `analogReadResolution()` value to a value higher than your board's capabilities, the Arduino will only report back at its highest resolution padding the extra bits with zeros.
88+
89+
For example: using the Due with `analogReadResolution(16)` will give you an approximated 16-bit number with the first 12 bits containing the real ADC reading and the last 4 bits *padded with zeros*.
90+
91+
If you set the `analogReadResolution()` value to a value lower than your board's capabilities, the extra least significant bits read from the ADC will be *discarded*.
92+
93+
Using a 16 bit resolution (or any resolution *higher* than actual hardware capabilities) allows you to write sketches that automatically handle devices with a higher resolution ADC when these become available on future boards without changing a line of code.
94+
[%hardbreaks]
95+
96+
[float]
97+
=== See also
98+
// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#),
99+
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
100+
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
101+
102+
[role="example"]
103+
* #EXAMPLE# http://arduino.cc/en/Tutorial/AnalogInputPins[Description of the analog input pins]
104+
105+
[role="language"]
106+
* #LANGUAGE# link:analofRead{ext-relative}[analogRead()]
107+
108+
109+
--
110+
// HOW TO USE SECTION ENDS
Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
= analogWriteResolution()
6+
7+
8+
// OVERVIEW SECTION STARTS
9+
[#overview]
10+
--
11+
12+
[float]
13+
=== Description
14+
`analogWriteResolution()` is an extension of the Analog API for the Arduino Due.
15+
16+
`analogWriteResolution()` sets the resolution of the `analogWrite()` function. It defaults to 8 bits (values between 0-255) for backward compatibility with AVR based boards.
17+
18+
The Due has the following hardare capabilities
19+
* 12 pins which default to 8-bit PWM, like the AVR-based boards. These can be changed to 12-bit resolution.
20+
* Pns with 12-bit DAC (Digital-to-Analog Converter).
21+
22+
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.
23+
[%hardbreaks]
24+
25+
26+
[float]
27+
=== Syntax
28+
`analogWriteResolution(bits)`
29+
30+
31+
[float]
32+
=== Parameters
33+
`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.
34+
35+
[float]
36+
=== Returns
37+
Nothing
38+
39+
--
40+
// OVERVIEW SECTION ENDS
41+
42+
43+
44+
45+
// HOW TO USE SECTION STARTS
46+
[#howtouse]
47+
--
48+
49+
[float]
50+
=== Example Code
51+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
52+
Explain Code
53+
54+
[source,arduino]
55+
----
56+
void setup(){
57+
// open a serial connection
58+
Serial.begin(9600);
59+
// make our digital pin an output
60+
pinMode(11, OUTPUT);
61+
pinMode(12, OUTPUT);
62+
pinMode(13, OUTPUT);
63+
}
64+
65+
void loop(){
66+
// read the input on A0 and map it to a PWM pin
67+
// with an attached LED
68+
int sensorVal = analogRead(A0);
69+
Serial.print("Analog Read) : ");
70+
Serial.print(sensorVal);
71+
72+
// the default PWM resolution
73+
analogWriteResolution(8);
74+
analogWrite(11, map(sensorVal, 0, 1023, 0 ,255));
75+
Serial.print(" , 8-bit PWM value : ");
76+
Serial.print(map(sensorVal, 0, 1023, 0 ,255));
77+
78+
// change the PWM resolution to 12 bits
79+
// the full 12 bit resolution is only supported
80+
// on the Due
81+
analogWriteResolution(12);
82+
analogWrite(12, map(sensorVal, 0, 1023, 0, 4095));
83+
Serial.print(" , 12-bit PWM value : ");
84+
Serial.print(map(sensorVal, 0, 1023, 0, 4095));
85+
86+
// change the PWM resolution to 4 bits
87+
analogWriteResolution(4);
88+
analogWrite(13, map(sensorVal, 0, 1023, 0, 127));
89+
Serial.print(", 4-bit PWM value : ");
90+
Serial.println(map(sensorVal, 0, 1023, 0, 127));
91+
92+
delay(5);
93+
}
94+
----
95+
[%hardbreaks]
96+
97+
[float]
98+
=== Notes and Warnings
99+
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.
100+
101+
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.
102+
[%hardbreaks]
103+
104+
[float]
105+
=== See also
106+
// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#),
107+
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
108+
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
109+
110+
[role="example"]
111+
* #EXAMPLE# http://arduino.cc/en/Tutorial/AnalogInputPins[Description of the analog input pins]
112+
113+
[role="language"]
114+
* #LANGUAGE# link:analogWrite{ext-relative}[analogWrite()] +
115+
* #LANGUAGE# link:analogRead{ext-relative}[analogRead()] +
116+
* #LANGUAGE# link:map{ext-relative}[map()]
117+
118+
119+
--
120+
// HOW TO USE SECTION ENDS

0 commit comments

Comments
 (0)