Sometimes we have to take input of the analog values from the sensors such as to find out the temperature of the room, the input values are in analog. These values can be read by the Arduino from its specific pins and these values can be used for further use. But before understanding the analogRead(), we have to understand what is the analog value? The analog value always varies from negative infinity to positive infinity and it is not restricted to only 0 and 1 like the digital values.

In the above discussion, we gave an example of room temperature, the room temperature can be 35 degrees or 10 degrees. It is not restricted that the room temperature should be 0 or 1. These types of values are known as analog values. In this write-up, the analogRead() function is explained in detail with the help of which we can take analog input values in Arduino.

What is the analogRead() function in Arduino

In Arduino, there is a built-in function that takes the analog values as input from its pins that are dedicated to the analog values. In Arduino Uno, the pins A0 to A5 are used to take the input values of analog signals and these pins have a pre-installed ADC (analog to digital converter) which converts the analog signal to digital so it can be read by the machine. The ADC reads the changing voltage and converts the analog signal from 0 to 1023 and communicates this value to the machine.

Example : analogRead() in Arduino

Consider the following Arduino code:

void setup() {


Serial.begin(9600);

}


void loop() {


int in=analogRead(A0);


Serial.println(in);


delay(5000);

}

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/Analog-Read-1.png" data-lazy- height="219" src="data:image/svg xml,” width=”712″>

Explanation: We have declared an integer variable “in” and using the analogRead() function, store the value from analog input from pin A0, then display the value on the serial monitor using the serial communication at a baud rate of 9600.

In the last statement, we use the function of delay to generate a delay of 5 seconds so that the output can be readable. We are taking the input of a variable resistor on pin A0 and displaying its output.

Hardware assembling

The circuit for this contains the following components:

  • Variable resistor of 1KΩ
  • Jumper wires – male to male
  • Arduino Uno
  • Breadboard

Connect one end of the potentiometer with a 5V pin of Arduino and the other end terminal to the ground pin with the help of jumper wires. Connect the middle terminal of the potentiometer with pin A0 of Arduino.

The circuit diagram will be like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/Analog-Read-2.png" data-lazy- height="260" src="data:image/svg xml,” width=”380″>

The circuit will look like this:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/Analog-Read-3.jpg" data-lazy- height="900" src="data:image/svg xml,” width=”1600″>

The serial monitor output of the above circuit is:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/Analog-Read-4.png" data-lazy- height="99" src="data:image/svg xml,” width=”1022″>

We can also visualize the output on the serial plotter as well:

<img alt="" data-lazy- data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/Analog-Read-5.png" data-lazy- height="359" src="data:image/svg xml,” width=”780″>

As we change the value of the variable resistor, the value on pin A0 changed which can be visualized on both; serial monitor and serial plotter.

Conclusion

The analogRead() is a function that is used to read the input from analog pins which is a continuous value and then converts it into digital by using the ADC circuits which are already present in the circuit. In this write-up, an example is demonstrated to explain the use of the analogRead() function in Arduino.

About the author

<img data-del="avatar" data-lazy-src="https://kirelos.com/wp-content/uploads/2022/01/echo/hammad–150×150.jpg61f7a3943d6b8.jpg" height="112" src="data:image/svg xml,” width=”112″>

Hammad Zahid

I’m an Engineering graduate and my passion for IT has brought me to Linux. Now here I’m learning and sharing my knowledge with the world.