Lecture26_Serial_Comm_II

In electronics, an analog-to-digital converter (ADC, A/D, or A-to-D) is a system that converts an analog signal, such as a sound picked up by a microphone or light entering a digital camera, into a digital signal. An ADC may also provide an isolated measurement such as an electronic device that converts an input analog voltage or current to a digital number representing the magnitude of the voltage or current.

The concept of an ADC:

An analog sine wave is being digitized into a digital signal:

Your Arduino board has 6 ADC inputs, these inputs can be used at the same time, the internal digital circuit (Ananlog input multiplexer) can manage the operation. Look at the figure below, the internal ADC is a 10-bit ADC, which means the 0-5 V is divided by 2^10 steps, which means the ADC can resolve the minimum voltage of 5/2^10=5/1024, which is around 5 mV. In this case, tiny input variations smaller than 5 mV are unlikely to be detected. You can also say the sensitivy of the ADC is 5 mV.

2. Connect your circuit:
We are going to use a potentiometer to vary the input voltage to the Arduino ADC. Please use A0 (analog input 0) as the input voltage.

The analog voltage is converted into digital signal and sent to your PC by the USB cable. The communication protocol between the Arduino board and your PC is called 'UART', which means Universal Asynchronous Receiver-Transmitter. The UART is a type of Serial Communication.

As soon as you complete the circuit and power up the Arduino, the data starts streaming, we need Matlab to receive the data.

3. Matlab script for Serial Data receiving:
clear all
close all
delete(instrfind(('Port'),('COM3')));  % This command will close the previous port you opened. Figure out your COM number on your computer by 'Device Manager':

s=serial('COM3','Baudrate',9600,'DataBits',8); % Open the serial port on your PC, define the transmittion rate to be 9600 bit per second, the length of each data is 8 bits.

fopen(s); % Open 's', which represents '
serial('COM3','Baudrate',9600,'DataBits',8);'
voltage=fscanf(s); % Use 'fscanf' function to acquire the data being stored in the memory of your PC.
y=str2double(voltage); % Convert the data into a 'double' type. The data is being sent as string characters, you need convert them into numbers for display.

Run the program, you will see the following result:

The data is be streamed very quickly, when you run the program in Matlab, Matlab will acquire ONE 8-bit data from the streamed digital signal. It is very likely the data is not ready or the you sampled the middle of an 8-bit data chain. So the first data can be invalid. In this case, Matlab just give up the first 8-bit data, and show you NaN as the frist data.

So the question is, how can we sample more data and store them in a vector?

A 'For Loop' will solve this issue. Think about it in this way: if you put the data aquisition part in a loop, Matlab will keep sampling the streamed data.

Task 1: Complete the script below and print out the current voltage.
clear all
close all
delete(instrfind(('Port'),('COM3'))); % Close previous port
s=serial('COM3','Baudrate',9600,'DataBits',8); % Configure the port
fopen(s); % Open the port
for i=1:100 % continuously sample 100 data points
??
end
fprintf(??)

Task 2: Use the stopwatch timer in Matlab to get the time spent by the 'For Loop':
In Matlab, you can use the following to report the time elapse:

tic
toc

Task 3: Plot the data.
Use the following command to remove the first bad data in your vector:
data(1)=[];

Convert the digital voltage value to real voltage values. Create an X axis as the Time axis. Do not use the tic/toc time. The sampling rate is 9600 bits/s, you have 100 data points, and 8 bits for every point, so you have 800 bits collected, which consumes ???? time..... Complete this calculation and generate a linear X space for your plot.

Plot the data use the plot() function.

Label the X and Y axis properly.

Task 4: Try different voltages

Turn the knob of the potentiometer, and try two other different voltages and plot them in the same way.

***** Note that your serial communication and the plotting cannot be done in the same run. You must run the serial communicationa nd collect the data, then plot the data.

This homework must be done in a .doc or PDF file, like a report with narrative contents. Add figure captions to your figures. Have your code as the 'Appendix' section in your report. By email only. Due Monday