How to use Arduino Analog Input

In this tutorial, you will learn about How to use Arduino Analog Input along with the practice example. Analog input is to get data from analog sensors into our Arduino. Since most of the sensors are analog, it is quite important to get our basics clear.

Let’s begin with theory: –

First of all, we need to understand what an analog signal is. This is probably something which you might have studied in school.

RE
Digital and Analog Signal.jpg

A digital signal is one which, at any point of time is either HIGH or LOW. An analog signal is one which varies continuously with time, as you can see in above picture. Now, let’s talk about analog sensors

Analog Phenomenon.jpg

As you can see in the above infographic, a sensor (or technically, a transducer) is a device that converts any phenomenon (like temperature, light, moisture etc.) into electrical signals. In case of Arduino, this analog signal has to be fed through analog pins, into an ADC, to get the sensor values for processing. I will explain it in a moment. Arduino, as you already know is a microcontroller, but actually, Atmega328p, an IC is the brain of Arduino Uno. Atmega328p has a 10 bit ADC, which processes the analog signals. Let us try to understand ADC first. Analog to Digital converters, or commonly known as ADCs, are circuits, which convert analog signals to digital signals. Since ADCs are very broad topic, we will only see ADC of Arduino. Atmega328p has a 5v 10 bit ADC, which simply means that it generates a number between 0 – 1024 depending on input voltage 0-5 volts.

Analog Phenomenon.jpg

The above infographic is quite self-explanatory. The ADC generates a value, based on the input signal that reflects the data from the sensor. One should note that the value from ADC is not the actual sensor value. To get the actual sensor value, one has to calibrate the sensor. Calibration of sensor is sensor specific, there is not general calibration, and is usually given by the manufacturer in the datasheet. Note that Arduino Uno has 6 analog pins, meaning you can use up to 6 sensors. That was all in theory, let us see how it works in practice.

Time to get our hands dirty: –

We will use LM35 temperature sensor by Texas Instruments, it is the most commonly used temperature sensor that measures temperature in the range   -55 C to 150 C. It is a three terminal device. You can easily find it in any electronics store and is usually included in most of the Arduino educational kits. Here’s how it looks-

LM35 Pinouts.jpg

It is an analog sensor. It is a three-legged device, as one can see:- Vs, GND and Vout. Vs and GND pins are to power the circuit. You can connect to supply of up to 30 volts. Arduino has 5v pins to do this job. Vout is the output pin and has to be connected with A0 pin of Arduino (or any other analog pin). The signal generated by LM35 varies linearly with temperature. It is 10mV per degree rise/fall in temperature. Let’s see the circuit, it is a very simple one, connect the pins as described.

Circuit Diagram

Arduino Interface LM35.jpg

Code

float vout;  //temporary variable to hold sensor reading

void setup() {

pinMode(A0,INPUT); //A0 is pin number, INPUT is self-explanatory

Serial.begin(9600);

}

void loop() {

vout=analogRead(A0); //This function reads the value of ADC at pin A0. 

vout=(vout*500)/1023;

Serial.print(“Temperature =  “);

Serial.print(vout);

Serial.println();

delay(1000); //Getting temperature every second is a good idea!

}

You can copy paste the code in your Arduino IDE and upload the sketch.

Code Explanation

If you are more curious about the code (which you should be), I will explain the critical parts.

  • pinMode(A0, INPUT) :- Defines that pin A0 is going to be used for input.
  • vout=analogRead(A0) :- This function returns the value of ADC at pin A0. As discussed before, it returns a value between 0 and 1024 depending on temperature range of sensor (0C to 155C).
  • vout = (vout*500)/1023 :- Don’t worry about this, here temperature is calculated out of ADC values based on calibration factor (10mV/C here).

You can usually find this by a simple search. Once you have uploaded the sketch, therefore you can open serial monitor (ctrl+shft+M) to see the output.

Conclusion

Pro Tip: To verify if the sensor is working, blow on the sensor, you’ll observe the change in temperature. I hope that you are clear with the tutorial. If you have any queries, please feel free to ask in the comment section below.

Spread knowledge

Leave a Comment

Your email address will not be published. Required fields are marked *