Clear Filters
Clear Filters

Hello everyone! Need to SPEED UP sampling rate / Baudrate of i2c connection with Arduino! NO Serial command

3 views (last 30 days)
I am using the following code, with the support of i2C library, due to its flexibility to set the registers of the sensors ( MPU6050 ); it looks like the command 'arduino' does not have options to set sampling rate, since with this code I almost reach 6 Hz.. very bad. I need to speed up at minimum at 50-60 Hz. Thanks everyone!
>>>>>>>><<<<<<<<<
a = arduino('/dev/tty.usbmodem1421','uno','libraries','I2C');
addrs = scanI2CBus(a);
addrs = addrs(1,1);
dev = i2cdev(a,addrs{1,1});
% set arduino register
writeRegister(dev,107,128); % reset MPU
writeRegister(dev,27,0); % set gyro reg
writeRegister(dev,28,0); % set acc reg
writeRegister(dev,107,0); % set power reg
% define the scale factor of acceleration and gyro
fls_acc = 4;
fls_gyro = 500;
k_acc = fls_acc/(2^16); %scale factor of acc (LSB)
k_gyro = fls_gyro/(2^16); %scale factor of gyro (LSB)
% register map
register = map_register(dev);
% read acceleration on 3 axis
x_acc = read_acceleration_x(dev,k_acc,fls_acc);
y_acc = read_acceleration_y(dev,k_acc,fls_acc);
z_acc = read_acceleration_z(dev,k_acc,fls_acc);
figure(1)
grid on
tic;
while toc<10
drawnow;
% Accel. >> X << plotting
x_acc = [x_acc;read_acceleration_x(dev,k_acc,fls_acc)];
acc(1) = subplot(3,1,1);
plot(x_acc, 'r', 'linewidth', 3);
title(acc(1), 'x axis acceleration');
% Accel. >> Y << plotting
y_acc = [y_acc;read_acceleration_y(dev,k_acc,fls_acc)];
acc(2) = subplot(3,1,2);
plot(y_acc, 'g', 'linewidth', 3);
title(acc(2), 'y axis acceleration');
% Accel. >> Z << plotting
z_acc = [z_acc;read_acceleration_z(dev,k_acc,fls_acc)];
acc(3) = subplot(3,1,3);
plot(z_acc, 'b', 'linewidth', 3);
title(acc(3), 'z axis acceleration');
ResultAcc = [x_acc, y_acc, z_acc];
time = toc;
pause(.05);
end
hold off
>>>>>>>>>><<<<<<<<<<<
  2 Comments
Madhu Govindarajan
Madhu Govindarajan on 13 Nov 2017
Have you tried using Simulink support package for Arduino and the I2C block? That might be faster than the MATLAB version as there are lot of back and forths in the MATLAB support package for Arduino.
Francesco campagna
Francesco campagna on 14 Nov 2017
Thanks Madhu for your kind reply, actually I did not use Simulink since I never used that, is there really any command to speed up sampling rate simply using matlab code? Looks weird to me :/ Thank you!

Sign in to comment.

Answers (0)

Community Treasure Hunt

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

Start Hunting!