analogRead() - Arduino Reference
Last Updated :
13 Aug, 2024
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.
Similar Reads
Reference variable in programming Reference Variable is a reference to an existing variable, which is defined with the help of the & operator. In other words, reference variables are aliases of some existing variables, and then either of the two variables can be used. Updating the reference variable is same as updating the origi
2 min read
Analog to Digital conversion in Arduino In this article, we will learn about the working of analog input in Arduino Uno R3. Also, learn how to read and write analog data using Arduino Uno R3. Arduino: It is an open-source electronics platform. It consists ATmega328P 8-bit Microcontroller. It can be able to read inputs from different senso
5 min read
Mixed Reference in Excel A cell reference or a cell address is a way to define a cell on a worksheet, it is formed by combining a column letter and a row number. To refer any if the cell on a worksheet (in any formula/ to copy the cell) we use cell reference. Example: To refer to the cell present in row 2 & column D, ce
3 min read
Pass By Reference In C Passing by reference is a technique for passing parameters to a function. It is also known as call by reference, call by pointers, and pass by pointers. In this article, we will discuss this technique and how to implement it in our C program. Pass By Reference in C In this method, the address of an
4 min read
java.lang.ref.Reference Class in Java java.lang.ref.Reference Class is an abstract base class for reference object. This class contains methods used to get information about the reference objects. This class is not a direct subclass because the operations on the reference objects are in close co-operation with the garbage collector. Cla
3 min read
Perl | Pass By Reference When a variable is passed by reference function operates on original data in the function. Passing by reference allows the function to change the original value of a variable. When the values of the elements in the argument arrays @_ are changed, the values of the corresponding arguments will also c
2 min read
Difference between Citation and Reference Citation and Reference are often used interchangeably but they have distinct meanings in academic writing. A citation is a brief notation within the body of a text that indicates the source of information, ideas, or quoted material; whereas, a reference is a detailed entry at the end of a document t
5 min read
Function Call by Reference in Objective-C Just like other languages in objective-C also a function is used to perform some specific task. In objective-C, we can call a function by value or by reference. So, in this article, we will talk about the call by reference in Objective-C. The call-by-reference is a process of passing arguments to a
3 min read
C++ Functions - Pass By Reference In C++, there are different ways to pass data (or variables) to a function, with two common methods being Passing by Value and Passing by Reference. Passing by Reference lets a function modify a variable directly, without creating a copy. The variable and parameter share the same memory location, so
3 min read
Mixed Cell References in MS Excel Cell: The row and column in Excel together make a cell. We input the value/data(s) in cells of a spreadsheet. There are three types of values that can be entered into the cell. NumbersAlphabeticFormulas Cell reference ââââââis the address or name of a cell or a range of cell is known as Cell referen
3 min read