In this arduino tutorial we show how to read analog signal with Arduino. Reading analog signal is routine in electronics devices. As such learning how to use the inbuilt arduino analogWrite() function is essential if you want to interface analog sensor and read their values with arduino.
There are two aspect in doing this. One is the connection of the analog sensor to one of the ADC pin of arduino and the other is the programming.
Interfacing with Analog Sensor
Analog sensor such as temperature sensor, light sensors, pressure, magnetometer etc. should be connected to one of the ADC pins of arduino. Arduino UNO has six ADC pins labelled A0 to A5 on the board. Internally these pins goes to a 10-bit ADC(Analog to Digital Converter) which converts the analog signal into digital signal. The range of the ADC is thus 0 to 2¹⁰, including the 0 it means the range is from 0 to 1023(not 2¹⁰=1024).
In this example we will use 1k potentiometer. A potentiometer is not a true analog sensor in that it does not automatically change the analog signal magnitude according to the environment it is supposed to read. However, by changing manually the resistance value with the potentiometer we will get the changing analog signal magnitude. Analog sensor connection to the Arduino is same as connecting potentiometer shown here. So you can just replace the potentiometer.
The following figure shows how to connect the potentiometer to the arduino. This just shows the connection. Down below is video showing the actual reading of analog signal using potentiometer.
The figure below was drawn and simulated in proteus. You can download arduino for proteus following the link below.
Program for reading analog signal
The programming code for reading analog signal has three important functions. One is related to setup of arduino microcontroller serial communication which is Serial.begin(). The second is the analog signal reading function analogWrite(). And the third is the function to display the read in digital signal values on the PC monitor which is Serial.println().
The first step is to tell the Arduino that we will be transferring the analog converted digital values into the PC using the serial communication protocol. This is done using the Serial.begin() function. Serial is an object in C++ programming language and begin() is the object function. This function requires the baud rate we will be using which in this case is just 9600. So the line of code would be:
which must be inserted in the setup() function of the arduino program.
The next step is to read in the analog signal. This is done using the analogWrite() function. It accepts the pin as argument. Thus if we want to read the analog pin A0 we have to write analogRead(A0). Since we want to display this values we will store the read in signal values into a variable, lets say var. Then the code required is,
int var = analogWrite(A0);
The third step is then to display the digital values stored in variable var. For this we will use the Serial.println() function and pass var as parameter.
Now the complete program code/sketch is below:
int var = analogRead(A0);
Demonstration(how to read analog signal with Arduino)
The following video shows how to read analog signal with Arduino. It shows how to compile and upload the above program code or sketch into arduino’s microcontroller. Then it shows how to initiate the serial monitor that is inbuilt into arduino IDE. By varying the potentiometer we will see different values in the serial monitor.
This tutorial showed you how to read analog signal with Arduino with video. Check out other arduino tutorials on this blog.