Skip to content

Commit 7dccfa9

Browse files
author
Akshay Sharma
committed
Add content Functions/Analog IO
1 parent 2232e7c commit 7dccfa9

File tree

2 files changed

+156
-0
lines changed

2 files changed

+156
-0
lines changed
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
:ext-relative: adoc
4+
5+
6+
= analogRead()
7+
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Reads the value from the specified analog pin. The Arduino board contains a 6 channel (8 channels on the Mini and Nano, 16 on the Mega), 10-bit analog to digital converter. This means that it will map input voltages between 0 and 5 volts into integer values between 0 and 1023. This yields a resolution between readings of: 5 volts / 1024 units or, .0049 volts (4.9 mV) per unit. The input range and resolution can be changed using link:analogReference{ext-relative}[analogReference()]`.
16+
17+
It takes about 100 microseconds (0.0001 s) to read an analog input, so the maximum reading rate is about 10,000 times a second.
18+
[%hardbreaks]
19+
20+
21+
[float]
22+
=== Syntax
23+
24+
`analogRead(pin)`
25+
26+
[float]
27+
=== Parameters
28+
`pin`: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)
29+
30+
[float]
31+
=== Returns
32+
int(0 to 1023)
33+
34+
--
35+
// OVERVIEW SECTION ENDS
36+
37+
38+
39+
40+
// HOW TO USE SECTION STARTS
41+
[#howtouse]
42+
--
43+
44+
[float]
45+
=== Example Code
46+
// Describe what the example code is all about and add relevant code ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
47+
The code reads the voltage on analogPin and displays it.
48+
49+
[source,arduino]
50+
----
51+
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
52+
// outside leads to ground and +5V
53+
int val = 0; // variable to store the value read
54+
55+
void setup()
56+
{
57+
Serial.begin(9600); // setup serial
58+
}
59+
60+
void loop()
61+
{
62+
val = analogRead(analogPin); // read the input pin
63+
Serial.println(val); // debug value
64+
}
65+
----
66+
[%hardbreaks]
67+
68+
[float]
69+
=== Notes and Warnings
70+
If the analog input pin is not connected to anything, the value returned by analogRead() will fluctuate based on a number of factors (e.g. the values of the other analog inputs, how close your hand is to the board, etc.).
71+
[%hardbreaks]
72+
73+
[float]
74+
=== See also
75+
// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#),
76+
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
77+
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTION IS MANDATORY ◄◄◄◄◄
78+
79+
[role="language"]
80+
* #LANGUAGE# link:analogReference{ext-relative}[analogreference()] +
81+
* #LANGUAGE# link:analogReadResolution{ext-relative}[analogReadResolution()] +
82+
* #LANGUAGE# (http://arduino.cc/en/Tutorial/AnalogInputPins[Tutorial: Analog Input Pins])
83+
84+
85+
--
86+
// HOW TO USE SECTION ENDS
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
:source-highlighter: pygments
2+
:pygments-style: arduino
3+
//:ext-relative: adoc
4+
5+
6+
= analogReference()
7+
8+
9+
// OVERVIEW SECTION STARTS
10+
[#overview]
11+
--
12+
13+
[float]
14+
=== Description
15+
Configures the reference voltage used for analog input (i.e. the value used as the top of the input range). The options are:
16+
17+
* DEFAULT: the default analog reference of 5 volts (on 5V Arduino boards) or 3.3 volts (on 3.3V Arduino boards)
18+
* INTERNAL: an built-in reference, equal to 1.1 volts on the ATmega168 or ATmega328 and 2.56 volts on the ATmega8 (not available on the Arduino Mega)
19+
* INTERNAL1V1: a built-in 1.1V reference (Arduino Mega only)
20+
* INTERNAL2V56: a built-in 2.56V reference (Arduino Mega only)
21+
* EXTERNAL: the voltage applied to the AREF pin (0 to 5V only) is used as the reference.
22+
[%hardbreaks]
23+
24+
25+
[float]
26+
=== Syntax
27+
`analogReference(type)`
28+
29+
30+
[float]
31+
=== Parameters
32+
`type`: which type of reference to use (DEFAULT, INTERNAL, INTERNAL1V1, INTERNAL2V56, or EXTERNAL).
33+
34+
[float]
35+
=== Returns
36+
Nothing
37+
38+
--
39+
// OVERVIEW SECTION ENDS
40+
41+
42+
43+
44+
// HOW TO USE SECTION STARTS
45+
[#howtouse]
46+
--
47+
48+
[float]
49+
=== Notes and Warnings
50+
After changing the analog reference, the first few readings from `analogRead()` may not be accurate.
51+
52+
*Don't use anything less than 0V or more than 5V for external reference voltage on the AREF pin! If you're using an external reference on the AREF pin, you must set the analog reference to EXTERNAL before calling `analogRead()`.* Otherwise, you will short together the active reference voltage (internally generated) and the AREF pin, possibly damaging the microcontroller on your Arduino board.
53+
54+
Alternatively, you can connect the external reference voltage to the AREF pin through a 5K resistor, allowing you to switch between external and internal reference voltages. Note that the resistor will alter the voltage that gets used as the reference because there is an internal 32K resistor on the AREF pin. The two act as a voltage divider, so, for example, 2.5V applied through the resistor will yield 2.5 * 32 / (32 + 5) = ~2.2V at the AREF pin.
55+
[%hardbreaks]
56+
57+
[float]
58+
=== See also
59+
// Link relevant content by category, such as other Reference terms (please add the tag #LANGUAGE#),
60+
// definitions (please add the tag #DEFINITION#), and examples of Projects and Tutorials
61+
// (please add the tag #EXAMPLE#) ►►►►► THIS SECTON IS MANDATORY ◄◄◄◄◄
62+
63+
[role="example"]
64+
* #EXAMPLE# http://arduino.cc/en/Tutorial/AnalogInputPins[Description of analog input pins]
65+
66+
[role="language"]
67+
* #LANGUAGE# link:analogRead{ext-relative}[analogRead()]
68+
69+
--
70+
// HOW TO USE SECTION ENDS

0 commit comments

Comments
 (0)