Skip to content

Updated analogRead() to use An style pin names #463

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 21, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 25 additions & 12 deletions Language/Functions/Analog IO/analogRead.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -4,23 +4,37 @@ categories: [ "Functions" ]
subCategories: [ "Analog I/O" ]
---




= analogRead()


// OVERVIEW SECTION STARTS
[#overview]
--

[float]
=== Description
Reads the value from the specified analog pin. The Arduino board contains a 6 channel (7 channels on MKR boards, 8 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[analogReference()].
Reads the value from the specified analog pin. Arduino boards contain a multichannel, 10-bit analog to digital converter. This means that it will map input voltages between 0 and the operating voltage(5V or 3.3V) into integer values between 0 and 1023. On an Arduino UNO, for example, this yields a resolution between readings of: 5 volts / 1024 units or, 0.0049 volts (4.9 mV) per unit. See the table below for the usable pins, operating voltage and maximum resolution for some Arduino boards.

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.
[%hardbreaks]
The input range can be changed using link:../analogreference[analogReference()], while the resolution can be changed (only for Zero, Due and MKR boards) using link:../../zero-due-mkr-family/analogreadresolution[analogReadResolution()].

On ATmega based boards (UNO, Nano, Mini, Mega), 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.

[options="header"]
|===================================================
|Board |Operating voltage |Usable pins |Max resolution
|Uno |5 Volts |A0 to A5 |10 bits
|Mini, Nano |5 Volts |A0 to A7 |10 bits
|Mega, Mega2560, MegaADK |5 Volts |A0 to A14 |10 bits
|Micro |5 Volts |A0 to A11* |10 bits
|Leonardo |5 Volts |A0 to A11* |10 bits
|Zero |3.3 Volts |A0 to A5 |12 bits**
|Due |3.3 Volts |A0 to A11 |12 bits**
|MKR Family boards |3.3 Volts |A0 to A6 |12 bits**
|===================================================

*A0 through A5 are labelled on the board, A6 through A11 are respectively available on pins 4, 6, 8, 9, 10, and 12 +
**The default analogRead() resolution for these boards is 10 bits, for compatibility. You need to use link:../../zero-due-mkr-family/analogreadresolution[analogReadResolution()] to change it to 12 bits.

[%hardbreaks]

[float]
=== Syntax
Expand All @@ -29,18 +43,17 @@ It takes about 100 microseconds (0.0001 s) to read an analog input, so the maxim

[float]
=== Parameters
`pin`: the number of the analog input pin to read from (0 to 5 on most boards, 0 to 6 on MKR boards, 0 to 7 on the Mini and Nano, 0 to 15 on the Mega)
`pin`: the name of the analog input pin to read from (A0 to A5 on most boards, A0 to A6 on MKR boards, A0 to A7 on the Mini and Nano, A0 to A15 on the Mega).

[float]
=== Returns
int(0 to 1023)

The analog reading on the pin (int). Although it is limited to the resolution of the analog to digital converter (0-1023 for 10 bits or 0-4095 for 12 bits).

--
// OVERVIEW SECTION ENDS




// HOW TO USE SECTION STARTS
[#howtouse]
--
Expand All @@ -52,7 +65,7 @@ The code reads the voltage on analogPin and displays it.

[source,arduino]
----
int analogPin = 3; // potentiometer wiper (middle terminal) connected to analog pin 3
int analogPin = A3; // potentiometer wiper (middle terminal) connected to analog pin 3
// outside leads to ground and +5V
int val = 0; // variable to store the value read

Expand Down