0

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) 
am304
  • 13,758
  • 2
  • 22
  • 40
sherin thomas
  • 15
  • 1
  • 1
  • 3
  • 1
    Please post your code. – am304 Oct 05 '14 at 17:37
  • 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) – sherin thomas Oct 06 '14 at 05:25
  • Next time, please add the code to the question rather than the comments as it makes it difficult to read. So you are acquiring 100 data points through a serial connection at 9,600 bauds. What frequency is your input sine wave? What output do you get in MATLAB? BTW, it looks like your `while` loop will run forever... – am304 Oct 06 '14 at 08:28
  • well i tried frequencies upto 1khz.the output was very distorted which resembled a triangle shape irrespectve of the function given as input.yes,while is an infinite loop because i wanted real time plotting as in an orginal oscilloscope – sherin thomas Oct 06 '14 at 15:35
  • It might be worth for debugging purposes to just acquire one period worth of data and trying to plot it after the data acquisition is finished. Once you get this working, you can then move on to "real-time" plotting and acquiring data continuously. It sounds like you are aliasing, i.e. not acquiring data at a fast enough sample rate. – am304 Oct 06 '14 at 15:56
  • if i increase my baudrate,can it help?? – sherin thomas Oct 06 '14 at 16:52
  • It might do... I am not sure what else to suggest, sorry. – am304 Oct 06 '14 at 20:24
  • when i connected my input to arduino and opened the serial monitor,what i saw was that the signals where sampled at different points due to which i am getting such a plot.if i connect a filter circuit,can it help?? – sherin thomas Oct 07 '14 at 04:45
  • Not sure, it doesn't sound to be a MATLAB-related issue to be honest, but more a hardware issue on the Arduino side, so this forum is probably not the best place for this. You need to fix the hardware and sampling issues before attempting to get the data into MATLAB. – am304 Oct 07 '14 at 08:01

1 Answers1

1

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.

frarugi87
  • 2,826
  • 1
  • 20
  • 41
  • if i increase my baud and decrease my frequency,can it help for a better plot? – sherin thomas Oct 07 '14 at 13:29
  • Of course it can.. Since the main problem here is the baud rate you can set it to 115200 bps and acheve a transmission in almost 0.5 ms (so you can go at about 150 Hz). If you want to go faster you can check with your serial port and/or program to see how fast you can go (arduino can go also to 1Mbps). I suggest you to change the way you transmit your data: switch from text representation (so 6 bytes per transmission) to binary; since you have 10 bits, you will have to send just 2 bytes (so x3 speed). Anyway you won't be able to go above 1 kHz because of the ADC times... – frarugi87 Oct 07 '14 at 13:53