What is analogRead() function??
analogRead is used to read analog voltages from 0 to 5v and convert them into digital integer values from 0 to 1023. The analogRead function is similar to the digitalRead function as it takes only one argument inside the function, the analogRead function reads analog values as input. analogRead function returns integer value from 0 to 1023, unlike in digitalRead it return only values 0 or 1 means HIGH or LOW (5v or 0v).analogRead reads a scale between value between 0 and 1023.
Analog reads values are used to read analog values from
sensors like temperature sensors, resistance, voltage, and current. analogRead
function reads the value from analog input pins A0-A5 of Arduino and feds to
onboard analog to digital (ADC) converter to give digital values range
between 0 to 1023. ADC can measure 1024 (2^10) as ADC is of 10-bit.
digitalRead() reads current status of digital pin whether its HIGH or LOW in form of 0 & 1 means 0v & 5v(1 - HIGH & 0 – LOW). digitalRead() returns one values whether 0 or 1 which represents 0v and 5v. digitalRead() is used by digital pin (0-13) of Arduino.
analogRead() reads Analog voltage from 0v to 5v. analogRead() returns integer values from 0 to 1023. analogRead() returns values from 0 to 1023 which represents 0v to 5v. analogRead() uses an analog pin from A0-A5 as input and fed it to ADC to convert it to give integer values.
pinMode(A0, INPUT);
Reading analog voltages through the analog pin A1 and store
into variables.
int readvalue = analogRead(A0);
That value is stored in variable name readvalue is the value between 0 to 1023.
Serial.print(“Analog value :“);
Serial.println(readvalue);
Simple Example of analogRead using potentiometer and Arduino
How to read voltages in Arduino using analogRead()??
To understand how the analogRead function works first we need to
know the basics of ohm’s law.
In the above circuit, we know that current flows from higher
potential toward a lower potential mean from positive terminal to negative
terminal. But electron flows on the opposite side from the negative side toward the positive side. Opposite charges electrons are repelled by negative charge
batteries hence it moves towards the positive charge of batteries.
According to ohm’s law
V=IR
Where V is a voltage in volts, I is the current in Amp
flowing in a circuit & R is the resistance in Ohms offered by the circuit.
So I = V/R and R= V/I
For example, consider a voltage of 5v and resistor of 330Ω
I=
V/R = 5/330 = 15.15 mA
So
we got the current flowing in a circuit as I = 15.15 mA
What
if you have more than one resistor?
From
above we have current I = 15.15 mA, Resistor R1 = 330Ω & Resistor R2 =
100Ω
So
total Resistor
Rs = R1+R2 = 330Ω + 100Ω = 430Ω
So
total Current
I
= V/Rs = 5v/430Ω = 11.63 mA
V1
= IR1 = 11.63 x 330 = 3.4v
V2
= IR2 = 11.63 x 100 = 1.16v
By Reading voltage by Arduino
Slope
(m) = (y2 – y1/ x2 - x1)
Slope
(m) = (5 – 0 / 1023 – 0)
(m)
= 5 / 1023
y
– y1 = m(x - x1)
y
= (5 / 1023) * (x)
y
= voltage & x = value from sensor
Voltage (v) = 5.0/1023.0 * value from sensor
X represents
values read by Arduino read pin from the potentiometer
Y represents the output voltage
Arduino with LDR to read voltage using analogRead()
This image is created using fritzing |
Connect the ground pin of the LDR sensor to the ground pin of Arduino, the VCC pin to 5v, and the Signal pin to A1. Also, connect the positive lead of LED to Arduino pin 6.
Code -
int Sensor_value;
int LED_pin = 6;
float voltage;
void setup()
{
pinMode(LDR_pin,INPUT);
pinMode(LED_pin,OUTPUT);
Serial.begin(115200);
}
void loop()
{
Sensor_value = analogRead(LDR_pin);
voltage = (5.0/1023.0)* Sensor_value;
Serial.print(voltage);
Serial.println("v");
if(Sensor_value<4)
{
digitalWrite(LED_pin,HIGH);
}
else{
digitalWrite(LED_pin,LOW);
}
delay(500);
}
Arduino with a potentiometer to read Analog voltage using analogRead()
Potentiometers
are variable resistors having three pins terminal. The first pin is where we connect
Vcc, the last pin is where we connect to the ground and at the middle pin we connect the signal
pin.
To
understand potentiometers we will look at how potentiometers work.
When
we move the slider (B) upside towards point A, then the voltage at the reading pin
(Analog pin) will be 5v. When we move the slider (B) towards the downside the read
pin which is the analog pin will read 0v and when the slider is between point A and
point C, the read pin will read 2.5v.
How does Potentiometer work??
R Total = 10k
R Total will change according to pot value in my case
its 10K
If we move the slider left side in an anti-clockwise direction the value of R2 will be 0, R2=0
R Total = R1 + 0
R Total = R1 = 10k
If we move the slider right side in the clockwise direction the value of R1 will be 0, R1=0
R Total = 0 + R2
R Total = R2 = 10k
If
we move the slider in middle between R1 resistor and R2 resistor then.
R Total = R1 + R2 = 5k + 5k = 10k
V
= IR, I = V/R
I
= 5/10k = .5mA
Current
I = .5mA
Voltage
(V) = I x R2
If
R2 = 0, then Voltage (V) = .5 x 0 = 0
If
R2 = 10k, then Voltage (V) = .5 x 10k = 5v
int potnetiometer_value;
float voltage;
void setup() {
Serial.begin(115200);
pinMode(potentiometer_pin,INPUT);
}
void loop()
{
potnetiometer_value = analogRead(potentiometer_pin);
voltage = (5.0/1023.0)* potnetiometer_value;
Serial.print(voltage);
Serial.println("v");
delay(1000);
}
Now we used the serial monitor to observe the output. To open up the serial monitor, click on Tools > Serial Monitor or use the shortcut (SHIFT + CONTROL + M). You can see when there is the value of voltage increases and decreases according to the rotation of knob of potentiometer”. the rotation of the knob can be the clockwise or anti-clockwise direction.
Conclusion –
Today we learn about the analogRead() function in Arduino, where to use the analogRead function, how to use the analogRead function, the syntax and parameters of the analogRead function, the difference between digital read and analogRead, read voltages in Arduino & Arduino with the potentiometer to read Analog voltage.
"I hope you find this IoT blog very helpful to you. In the upcoming lesson, we will see more about IoT sensors till then bye. See you all in my next blog."