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.
- 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.
- analogRead returns value anywhere between 0 to 1023 depending on the voltage it gets in return.
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).
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. Hi Friends! Welcome you onboard. I have been writing these