How to read analog signal into Arduino | applied electronics engineering

Latest

How to read analog signal into Arduino

By Applied Electronics - Tuesday, January 24, 2017 No Comments
In this tutorial we show how to read analog signal into Arduino. Arduino UNO has 10 bit ADC. Analog signal from outside outside world can be read into the arduino using the analogRead function. The converted analog signal is stored as digital values ranging from 0 to 2^10(1023). The digital value stored in Arduino can then be transferred to PC via the USB or serial port. The values on PC can be displayed via the built in Serial Monitor. This serial monitor can be invoked by clicking on a button on the arduino IDE toolbar. Alternatively we can also view the values using the PC hyperterminal or similar program like putty.

Here we show how to read varying potentiometer values on both the Arduino IDE built-in serial monitor and putty. We will show this using proteus program. After successful simulation you can go ahead and built the hardware which is easy to do. We will show the hardware in the next blog post.

The potentiometer will act as our analog sensor. Reading other analog sensor like temperature, pressure sensors, IR sensors etc are the same. Because the concept is easy using potentiometer and because it is readily found we will use potentiometer. Afterwards we will show how to connect other analog sensors.

Since in this simulation we will not be using actual hardware, we will not connect the Arduino USB. That means we will not use the inbuilt serial monitor. Instead we will use the virtual port and hyperterminal putty to view the data. This is important part to learn because often you want to simulate or prototype the final hardware before you proceed with actually building the hardware.

Arduino and RS232 communication theory

This means we will be using the TxD and RxD pins on the Arduino board. These two pins are serial pins and together they form the serial port. Arduino UNO has one serial port. Arduino UNO outputs voltage values 0 and 5V. But the actual RS232 protocol uses 12V. Therefore we need to convert the 0 to 5V range(TTL logic) to 0 to 12V range(RS232 protocol). This is done using line transceiver such as MAX232. 8031 Microcontroller UART RS232 signal analysis using Proteus describes this in details.

This type of communication is called RS232 serial communication and there are two types of serial communication- asynchronous and synchronous. RS232 communication is asynchronous type, see serial communication protocols for the difference between these two protocols. Internally, the analog converted digital values are first sent to parallel to serial converter then parity bits and stop bits(at least) are added. The data, stop bits and one or more parity bits makes a frame. Each frame is sent over the serial (either USB or RS232 serial port) line to the PC. To see this process of RS232 frame encoding see vhdl tutorial on RS-232 with parity bit.

After having learned the theoretical minimum to understand the ADC pins and serial communication we now turn to the circuit diagram. For purpose of simulation we will use proteus professional which is electronics design software.

Circuit Schematic

The basic circuit connection for reading analog values via ADC pin A0 is shown below. Also we have connected a RS232 DB9 connector. As you can see we have also connected a virtual terminal which is used to display data coming out of the TXD pin of the Arduino serial port.

How to read analog signal into Arduino

If you don't already have arduino proteus simulation model you can download it below:

http://noteable.site/2Zr/arduinoUNO.zip

As you can see we have 1k potentiometer connected to the A0 pin of arduino. By varying potentiometer resistance we can change the analog signal passing into the ADC pin A0. Then internally the arduino microcontroller converts the analog signal to digital signal. The digital data signal is itself 8 bit but the range of values we can have is from 0 to 2^10(1023).

As mentioned before, the converted data can be sent to PC via USB or we can read it using the serial port(TX and RX) on the arduino board. Normally people would read the values on the PC which are sent by the microcontroller via the USB. Here we will display the value on the virtual terminal and also on serial hyperterminal. We first show how you can read it via virtual terminal and then using serial hyperterminal program such as Putty.

Using Virtual Terminal

The virtual terminal is already connected as shown in the schematic above. If we open the virtual terminal it will show the analog signal converted digital signal. This is as shown below.

How to read analog signal into Arduino
 It shows value of 512 because the potentiometer pointer is at the middle of the POT. If we slide it up or down it show corresponding values. For example if we slide the POT upwards, that is increasing the voltage into the ADC pin we will get corresponding value displayed on the virtual terminal display as shown.

How to read analog signal into Arduino


Using hyperterminal program Putty

Now we can also read the same ADC converted analog signal values on hyperterminal window. For this you have to download two pieces of program. Both are free to download in their respective websites. The first is the Virtual Serial Port Emulator. You can download it here http://www.eterlogic.com/Products.VSPE.html. After you have downloaded it and installed, you should first "create a device" as shown below. Use one of your computer COM port, for example COM1. Then click on the RUN button.

How to read analog signal into Arduino

Once you have done that it will emulate the computer com port 1, which means, it checks for any program that request com port 1 and connects to it. That is, although there is no physical RS232 connection with your computer RS232 port, it creates the virtualization that RS232 connection is being made from outside to your PC RS232 port. The request for connection is made via the program which in this case is proteus.

Until now we have set up and made RS232 virtual connection. The next step is to view what is being received by the PC although virtual. To display what is received at your computer com port you need hypterterminal program(or similar programs). Windows XP have inbuilt hyperterminal. But if you are on windows 7 or other OS which does not have the inbuilt hyper terminal you can download program such as Putty. The link is provided below.

http://www.putty.org

See the download section and download it then install it. Now run the putty program. You have to make two changes. First set the session to serial by selecting Serial in the Session section and set up the baud rate to 9600 and also make sure the com port is right. Here it is com1.

How to read analog signal into Arduino

 Then  go the connection then serial and set up the serial connection properties as shown below.

How to read analog signal into Arduino

Finally click open. Now at this point putty is running and it should display any values read by the virtual terminal as shown in the figure below.

How to read analog signal into Arduino

Program Code

The program code is simple. The main thing to remember is to first set the baud rate using Serial.begin(). This should be set to 9600. After that we read the analog valaue using the analogRead() function. This function requires the analog pin which will be used to read in the analog value which in this case is A0. So you should write analogWrite(A0). Then you should save it in a variable using var = analogWrite(A0). Then using the Serial.println(var) print out the value of var. The program is as under:

void setup()
 {
 Serial.begin(9600);
 }

void loop()
 {
   int var = analogRead(A0);
   Serial.println(var);
   delay(500);
 }

Compile this program and load into the arduino and you will get the reading in virtual terminal or in putty hyperterminal as was shown earlier.

The video demonstration is here:

No Comment to " How to read analog signal into Arduino "