MATLAB Answers

Recording sound while acquiring data from pressure sensor that connected with arduino

10 views (last 30 days)
Dziban Naufal
Dziban Naufal on 22 Aug 2019
Commented: Walter Roberson on 28 Aug 2019
I was trying to record sound using soundcard and pressure data from arduino simultaneously. I tried to use audiorecorder and recordblocking function in the same a while loop with pressure data acquisition command and i found that MATLAB will execute the recordblocking function till the set time limit then it continue to execute the acquisition command.
Is there any way to record two data (from soundcard and arduino) simultaneously?
Here are the codes that used:
a = arduino();
% Create a figure window to monitor the live data
tmax = 10; % Total time for data collection in seconds
grid on,
xlabel ('Time (s)'), ylabel('Voltage');
axis([0 tmax+1 -0.5 5]);
% set initial index value, starting value
k = 0; %index
v = 0; %voltage
t = 0; %time
tic % Start timer
while toc <= tmax
k = k + 1;
disp('Start speaking')
disp('End of Recording');
v(k) = readVoltage(a,'A1');
t(k) = toc;
% Now plot the data
if k > 1
line([t(k-1) t(k)],[v(k-1) v(k)]);
for i=1:length(v)


Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson on 22 Aug 2019
Edited: Walter Roberson on 28 Aug 2019
You would use record(), which will permit you to continue running. You would set up a recording duration ahead of time.
However, you are going to have a hard time synchronizing the inputs, unless you deliberately include some kind of synchronization signal on the readings.
You are also not going to be able to poll the arduino with readVoltage at more than 100 Hz or so -- a lot of people report success only down to about 40 Hz or so. In order to do better, you need to write code for the arduino that records a number of readings and sends them in batches as close to 1000 bytes long as you can manage. This is due to the fact that the connection is likely serial over USB, and USB polling rate is no more than 1000 transactions per second, with 1000 data bytes available in a serial packet (the other 4 bytes normally available are used by the serial-over-usb protocol to indicate the current status of the various likes such as DTR.)
When you need to synchronize channels, it is often advisable to use an Ni Compaq Daq chassis, especially configuring it to return sample timestamps.


Dziban Naufal
Dziban Naufal on 28 Aug 2019
Thanks for the answer. I have tried to use record() function with the example that are provided by Matlab and the code seems like this :
s = serial('COM7');
s.RecordDetail = 'verbose';
s.RecordName = 'MySerialFile.txt';
out = fscanf(s);
But when i tried to execute the code, the command window was showing alert like this:
Warning: Unsuccessful read: A timeout occurred before the Terminator was reached..
could you help me about this?
Thanks a lot.
Walter Roberson
Walter Roberson on 28 Aug 2019
record() would be applied to the audiorecorder session. Where you have
you would use
and then later when you knew enough time had elapsed you would getaudiodata(recorder)
Your stuff with fprintf(s,'*IDN?') would be primarily for use with Instrument Control Toolbox for GPIB or SCPI devices, perhaps with a VISA driver. It would never be used with a sound card, and it would rather rarely be used with an arduino

Sign in to comment.


Community Treasure Hunt

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

Start Hunting!

Translated by