How to use analogRead in Arduino?

Hi Friends! Welcome you onboard. I have been writing these Arduino tutorial for beginners for quite a while now and today we are having the next episode. Today, I'll discuss How to use analogRead in Arduino. The analogRead is mainly used to program and address analog pins on the board. In our previous tutorial, we have seen How to use digitalWrite Arduino Command, which deals with digital pins of Arduino but today's one deals with analog pins. There are many types of boards available in the market ranging from Arduino UNO, Arduino Mega2560, Arduino Micro and many more, which you can use based on your technical requirements. Arduino Programming is made simple by the Arduino.cc - the manufacturer of Arduino Boards, providing an open source software and hardware features and give you the flexibility to modify and tweak the boards as per your requirements. In this post, I'll discuss how you can easily program the Arduino Board using analogRead if you intend to target the analog pins on the board. Let's dive in.

How to use analogRead in Arduino

The analogRead is a command mainly used to program the analog pins on the board. If you are using analogRead functions, it indicates you are making the pins as input i.e. you can connect the Arduino analog pins with any sensor and read its value by making the analog pins as input. Following figure shows the placement of analog pins on the Arduino Uno Board.
  • If you have already got a hold of some features of Arduino Board, you must have known that analog pins are 10-bit pins. It means each pin can store 0 - 1023 values.
Analog pins are different than digital pins as the later can store only two values: HIGH and LOW while the former comes with an ability to store any random value ranging from 0 - 1023 where 0 will indicate the ground signal or zero volts while 1023 will be representing 5 volts. The voltage values are directly proportional to the values stored in the Arduino Pins. For example, if the sensor voltage is around 2.5 V then the value we get on an analog pin will be half the total value it can store in the pin i.e. 512. Syntax:
  • The syntax of analogRead is given as follows:

int data = analogRead(int pin);

where:
  • Pin defines the number of a pin you are targeting. Most of the Arduino Boards come with 6 analog pins and marked as A0 to A5 while Arduino Pro Mini and Arduino Nano come with 8 pins, marked from A0 to A7 and Arduino Mega stands out in terms of having the most number of analog pins, around 16, marking from A0 to A15 on the Mega.
Return:
  • analogRead returns value anywhere between 0 to 1023 depending on the voltage it gets in return.
Example:

data = analogRead (4);

Note: 
  • If you are aiming to read analog pins from digitalRead, you must write A4, instead of simply pointing the required pin number i.e. analogRead(A4).
Here's a sample code for testing the analogRead Arduino command:
int sensorPin = A0;
int sensorValue = 0;  

void setup() {
 
  Serial.begin(9600);
  pinMode(ledPin, OUTPUT);
}

void loop() 
{
  sensorValue = analogRead(sensorPin);
  Serial.println(sensorValue);
}
I have written an Article on Introduction to Arduino IDE - An Official Software used to program the variety of Arduino Boards. In this Article, I have broken down everything in simple steps, detailing how to select the relevant board you are working on and make it compatible with the software. That’s all for today. I hope you have got valuable information out of this read. However, if you are unsure or have any question you can approach me in the comment section below. I’d love to help you according to the best of my knowledge. In the coming tutorial, we will have a look at How to use analogWrite in Arduino, which is used to update the status of analog pins. Thanks for reading the article.
Syed Zain Nasir

I am Syed Zain Nasir, the founder of <a href=https://www.TheEngineeringProjects.com/>The Engineering Projects</a> (TEP). I am a programmer since 2009 before that I just search things, make small projects and now I am sharing my knowledge through this platform.I also work as a freelancer and did many projects related to programming and electrical circuitry. <a href=https://plus.google.com/+SyedZainNasir/>My Google Profile+</a>

Share
Published by
Syed Zain Nasir