Javatpoint Logo
Javatpoint Logo

Arduino analogRead ( )

The analogRead( ) function reads the value from the specified analog pin present on the particular Arduino board.

The ADC (Analog to Digital Converter) on the Arduino board is a multichannel converter. It maps the input voltage and the operating voltage between the values 0 and 1023. The operating voltage can be 5V or 3.3V.

The values from 0 to 1023 are the integer values. It can also be written as 0 to (2^10) -1.

The time duration to read an analog input signal on the boards (UNO, Mega, Mini, and Nano) is about 100 microseconds or 0.0001 seconds.

Hence, the maximum reading rate of analog input is about 10000 times per second.

Let's discuss operating voltage and resolution of some Arduino boards.

  • The Operating voltage of Arduino UNO, Mini, Mega, Nano, Leonardo, and Micro is 5V, and resolution is 10 bits.
  • The Operating voltage of MKR family boards, Arduino Due, and Zero is 3 V, and resolution is 12 bits.

Changing the input voltage and resolution

  • On a particular board, we can change the input voltage using the analogReference( ) function.

analogReference( )

The reference voltage used for the analog input is configured by the analogReference( ).

The syntax is:

Note: The external reference voltage on the AREF pin should not exceed greater than 5V or less than 0V. We need to set the analog Reference as External while working on the AREF pin.
Otherwise, it can damage the microcontroller present on the Arduino board.

  • We can change the resolution only for the MKR Family, Zero, and Due board using the analogReadResolution( ) function.

analogReadResolution( )

The MKR Family, Zero, and Due have the ADC resolution of 12 bits.

The analogReadResolution( ) will return the integer values between 0 and 4095 from the analogRead( ) function.

The syntax is:

where,

bits: It signifies the resolution bits returned by the analogRead( ) function. We can set the value of bits between 1 and 32.

Note: If the resolution bits higher than the board capabilities are specified, the Arduino will pad the extra bits with zeroes.
For example, analogReadResolution (14)
The above function will give a 14-bit approximated number with the first two 12 bits (that includes ADC reading) and the last two padded bits.

Syntax of analogRead ( )

The syntax is:

where,

pin: It includes the name of the particular analog pin to read from.

The data type is int.

A not-connected analog pin can also return some value. Such value depends on the position of our hand on the board and other nearby analog inputs.

Code Example

Let's understand the concept of analogRead with an example.

Consider the below code:

The above code reads the voltage from the specified analog pin (AnaPin) and displays it.


Next TopicArduino Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA