Sample a signal from an Arduino using the Serial port

Hi!
Got an issue. So I'm using an Arduino Uno but I'm not able to get sample a signal well. Need some help or advices with this.
This is the code I'm using to sample the signal.
clear all
clc
arduino=serial('/dev/tty.usbmodemfd131','BaudRate',9600,'InputBuffer',512);
fopen(arduino);
k = 0;
c = 0;
x=linspace(1,2,1000);
for i=1:length(x)
y(i)=fscanf(arduino,'%d');
end
fclose(arduino);
disp('making plot..')
plot(x,y);
grid on;
for i=1:length(x)
if(y(i) > 0.5)
k = k +1;
end
end
k/length(y) %duty cycle
From the arduino I'm sending a square wave at approx 500 Hz. I'm getting like 20 samples per period, but I need to be able to sample other signals than 500 Hz ones as well.
How can I make it faster somehow? Any suggestions are welcomed :)

Answers (1)

500 samples per second, N characters per sample, 1 space (or end of line) character per sample to allow the samples to be distinguished from each other. This is 500 * (N+1) characters per second. 10 bits are needed to send each character over the serial port, so that is 10 * 500 * (N+1) = 5000 * (N+1) bits per second. This must fit in to the 9600 bits per second that you have set the serial port to, so we get
5000 * (N+1) <= 9600
and we see from that that you can only sustain the rate if N, the average number of characters per sample, is strictly less than 2 (e.g., 1).

Categories

Find more on MATLAB Support Package for Arduino Hardware in Help Center and File Exchange

Asked:

on 31 Jan 2012

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!