Description
This function is used to read the value from the analog input pin of Arduino. The Arduino controller can map a voltage input signal of 0-5 volts to a value of 0-1023.
In other words, we can divide 5 volts into 1024 equal parts. An input signal of 0 volts corresponds to the value 0, and an input signal of 5 volts corresponds to 1023.
example:
When the input voltage of the analog input pin is 2.5 volts, the value of this pin is 512.
(2.5 Volts / 5 Volts = 0.5, 1024 X 0.5 = 512)
Syntax
analogRead(pin)
pin: the analog pin number being read
Example code
int analogPin = A0; // define the analog input pin
int val = 0; // create a variable named val, assigned value 0.void setup() {
Serial.begin(9600); // setup serial communication
}void loop() {
val = analogRead(analogPin); // read the input pin
Serial.println(val); // display value in the serial window
}