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)