Open In App

analogRead() - Arduino Reference

Last Updated : 13 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

In the world of Arduino programming, The analogRead() function plays a crucial role. This function used to fetch the analog data from the analog pins in the Arduino bords. Let us think of an example of developing a weather station. This needs to collect the data from sensors. To analyze the produced data primarily we have to collect that data from it. for this, we require the analogRead() Function to collect the data from external sources.

What is AnalogRead()?

The Arduino board has a 10-bit multi-channel ADC (analog-to-digital converter) that reads analog pin values. It converts input voltages (0 to 5V or 3.3V) into integer values between 0 and 1023. For example, the Arduino UNO's resolution is 0.0049V per unit.

  • Analog Pin Reading: The `analogRead()` function reads the value from the specified analog pin.
  • Input Range Adjustment: Use the `analogReference()` function.
  • Resolution Adjustment: Available on Zero, Due, and MKR boards using the `analogReadResolution()` function.

Key Points

Conversion Resolution

- Arduino UNO: 5V/1024 units = 0.0049V per unit

Input Adjustment

- `analogReference()` function

Resolution Adjustment

- Zero, Due, MKR boards: `analogReadResolution()` function

Reading Speed

- AT mega boards (UNO, Nano, Mini, Mega): ~100 microseconds per read

- Maximum reading rate: ~10,000 reads per second

Now, Let us see how different boards take inputs and produces output resolution..

Board Name

Used pins for analog input

Resolution Bits

Voltage operated

UNO R3

A0 to A5

10 bits

5 Volts

UNO R4 (Minima, Wi-Fi)

A0 to A5

10 bits

5 Volts

Mini

A0 to A7

10 bits

5 Volts

Nano, Nano Every

A0 to A7

12 bits**

5 Volts

Nano 33 (IoT, BLE, RP2040, ESP32)

A0 to A7

10 bits

3.3 Volts

Mega, Mega2560, Mega ADK

A0 to A14

10 bits

5 Volts

Micro

A0 to A11*

10 bits

5 Volts

Leonardo

A0 to A11*

12 bits**

5 Volts

Zero

A0 to A5

12 bits**

3.3 Volts

Due

A0 to A11

16 bits**

3.3 Volts

GIGA R1

A0 to A11

12 bits**

3.3 Volts

MKR Family boards

A0 to A6

14 bits**

3.3 Volts

*A0~A5 are mapped on the board, A6~A11 are available on pins 4, 6, 8, 9, 10, and 12.**The default resolution of analogRead() is based on the board is 10 bits for compatibility. To convert to higher resolution use AnalogReadResolution().

Now let us see the syntax of the analogRead() function.

Syntax

analogRead(pin No);

Where,

  • Pin no is the Analog pin where the input is needed to read.
  • The analog reading on the pin. Although it is limited to the resolution of the analog to digital converter (0-1023 for 10 bits or 0-4095 for 12 bits). Data type: int.

Let us see some example to understand clearly,

Example

Let us example for how to use the analogRead() function to read the value from an analog input pin and print it to the serial monitor,

Note: Use the following code only in the Arduino IDE, when the required hardware is connected....Don't Run the code here. It is only for reference..

// Define the analog pin

const int analogInPin = A0;


void setup() {

// Initializing serial communication

Serial.begin(9600);

}


void loop() {

// Read the value from the analog input pin

int sensorValue = analogRead(analogInPin);


// Print the sensor value to the serial monitor

Serial.print("Analog value: ");

Serial.println(sensorValue);


// Wait for a short delay before reading again

delay(1000);

}

Reference Output

Explanation of Code

  • We first defined the analog input pin in which we'll be using (A0).
  • In the setup() function, we initialized serial communication with a baud rate of 9600.
  • In the loop() function, we used analogRead() to read the value from the analog input pin (analogInPin) and store it in the variable sensor Value.
  • We then print this value to the serial monitor using Serial.print() function and Serial.println() function.
  • Finally, we added a short delay of 1000 milliseconds which is 1 second using the delay() function to give some time before reading the next analog input again.

Upload this code into your Arduino board, open the serial monitor in the Arduino Ide, and you should see the analog values being printed in real-time as the Arduino reads them from the analog pin.

Advantages

  • Reads Numbers from Sensors: This functions helps us to read the numbers from sensors.
  • Easy to Use: The process of using this function is very easy
  • Helps Control Things: With the numbers from analogRead(), we can control other things. like, we can make a light brighter based on the number you read.
  • Smooth Changes: analogRead() can give us smooth changes.

Disadvantages

  • Not Super Fast: Compared to other functions this is not that much fast.
  • Limited Accuracy: The Accuracy that is provided by this function is less and limited.
  • Noise: In some ways it produce some noise while taking numbers from analogRead().

Applications

  • Light Sensors: We can use this analogRead() to read how much light is in a room.
  • Temperature Monitors: With analogRead(), we can read the temperature that came from a sensor.
  • Flex Sensors: analogRead() can read the bending of a flex sensor, which can be used in wearable technology.

Conclusion

The analogRead() function is an crucial function in Arduino programming. It is used to fetch the analog input from analog pins and converts it into digital signals . If that pins are connected to any secondary devices than this function is used to fetch the analog data from secondary devices that may be sensors, actuators and etc.


Next Article

Similar Reads