![]() |
VOOZH | about |
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.
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.
- Arduino UNO: 5V/1024 units = 0.0049V per unit
- `analogReference()` function
- Zero, Due, MKR boards: `analogReadResolution()` function
- 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.
analogRead(pin No);Where,
Let us see some example to understand clearly,
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,
// 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);
}
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.
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.