matlabarduino-unoanalog-digital-converter

how to plot the input analog signals from arduino in matlab


I have been trying to read analog signals through arduino uno and plotting them in MATLAB. When i gave my input wave from function generator and tried to plot it, the plotted signals were not correct, e.g. an input sine wave didn't give a sine wave. What could be the possible reason? Do I need any external circuit for smooth reading?

ARDUINO CODE

void setup() 
{ Serial.begin(9600); } 
void loop() 
{ 
int a=analogRead(A0); 
Serial.println(a); 
delay(1); 
}

MATLAB CODE

s=serial('COM3','BaudRate',9600); 
fopen(s) 
x=1:100; 
n=1; 
while n==1 
   for i=1:length(x) 
       tmp=fscanf(s,'%d'); 
       if length(tmp)>1 
          continue; 
       end 
       y(i)=tmp; 
   end 
   y=y/1024*5; 
   ylim([0 5]); 
   plot(x,y) 
   drawnow 
   end 
fclose(s) 

Solution

  • Just one question: did you ever study signal theory? Nyquist theorem?

    You are aquiring 1 point (100 us), then you send it through a serial connection (so 10 bits every byte) with a PRINTLN! (so you send, for instance, 6 bytes, i.e. "1023" + CR + LF). So 60 bytes, at 9600 bps it's more than 6 ms. Let's assume it's 7 ms for every point.

    Now you have a 1 kHz wave. So you are taking one sample from a wave, then wait for 6 of them to pass and then take another point. So you will never be able to see anything.

    If you want to see your wave try to have AT LEAST 10 points for every wave (i.e. don't go above 15 Hz). If you want to see a better wave, try to use a sinc interpolation instead of a plain interpolation.