|
| 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 |
0 commit comments