MATLAB Answers

How to synchronize Arduino Due and Arduino Uno without data acquisition delay in Matlab??

17 views (last 30 days)
Kasun Samarawickrama
Kasun Samarawickrama on 22 Mar 2019
Commented: Mark Sherstan on 22 Mar 2019
I have interfaced Arduino due board and Arduino Uno boards with Matlab GUI to acquire data streams real time. Arduino due board is operated at 3.3V and sensor is connected via SDA/SCL and Arduino Uno is operated at 5V and analog sensors are connected to the device. When I run the GUI program, I get real time data from arduino due but data coming from Arduino Uno will get delayed and the delay time is increased with the progress.
How can I synchronize the 2 devices to get rid of delay???


Sign in to comment.

Answers (1)

Mark Sherstan
Mark Sherstan on 22 Mar 2019
Could you just add your analog sensors to the due or the I2C device to the Uno so that all information is sent as one package?
Another solution is to introduce a "handshake". Basically you do a 1 for 1 data exchange. MATLAB sends some character and the Ardiuno will send a data value back and wont continue until both sets of data are acquired. This can slow down your data rate but is a good way to gaurntee you arent loosing data and that everything is synced.
Heres a quick example of what I am talking about:
% s is the serial port object...
fprintf(s, 'a'); % Send character to ardiuno
out = fscanf(s, '%40s\n'); % Read data from ardiuno
split = strsplit(out, ','); % Seperate data based off commas
% Continue to process...
void setup(){
... set up
void loop(){
... data acquistion and processing
if (Serial.available() > 0) {
incomingString = Serial.readString();
if (incomingString == "a\n") {
Serial.print(data1); Serial.print(","); Serial.println(data2);


Kasun Samarawickrama
Kasun Samarawickrama on 22 Mar 2019
Dear Sir,
Thank you for the reply. I have connected impedance sensor to Arduino due because its only worked in 3.3V including the I2C communication pins and rest of my analog sensors are required 5V supply to perform. That's why I have used 2 different arduinos insted of one device.
I have used while loop in the program. when I plot the data real time, impedance sensor values comes fine but rest of the analog readings get delayed. There is no data loss in the process but due to the delay I won't be able to capture all analog data.
Mark Sherstan
Mark Sherstan on 22 Mar 2019
Consider using a logic level converter for the I2C device so that you can work with 5V or going the other way you could make a voltage divider to ensure 3.3V is not exceeded.
Can you please post your code? How do you know there is no data loss? Are you sampling at the same rate on both devices? Can you use a handshake? A couple things you can implment if you havent already.
ARDUINO Sample Rate Stabilization:
// Variable Definition
... Other variables
long loopTimeMicroSec = 5000;
void setup() {
... Startup
// Reset the timer
timer = micros();
void loop() {
// Stabilize sampling rate
... Rest of code
void timeSync(unsigned long deltaT){
// Calculate required delay to run at 200 Hz
unsigned long currTime = micros();
long timeToDelay = deltaT - (currTime - timer);
if (timeToDelay > 5000){
delay(timeToDelay / 1000);
delayMicroseconds(timeToDelay % 1000);
} else if (timeToDelay > 0){
} else {}
timer = currTime + timeToDelay;
MATLAB Real Time Plotter:
function [] = realTimePlotter()
% Set up figure, get properties, and label
h = animatedline;
ax = gca;
ax.YLim = [0 5];
xlabel('Time (s)')
ylabel('Output Y [units]')
% Start a timer
startTime = toc;
% Loop for 20 secounds
while toc < 20
% Random data
v = randi(4);
% Get current time and add to animation
t = toc - startTime;
% Update axes
if t < 5
ax.XLim = [0 10];
ax.XLim = [t-5 t+5];

Sign in to comment.

Sign in to answer this question.