Anyone help me, how can I change this code for me to plot the graph from the data obtained?

38 views (last 30 days)
function [] = i2c_sensor()
a = arduino('COM3', 'UNO');
imu = mpu6050(a,'SampleRate',50,'SamplesPerRead',10,'ReadMode','Latest');
for i=1:10
accelReadings(i,:) = readAcceleration(imu);
display(accelReadings(i,:));
pause(1);
end
end

Answers (1)

Aashray
Aashray on 24 Jun 2025 at 6:01
If you would like to plot the accelerometer data obtained in your “i2c_sensor()” function, you can modify your code to store the readings. After that, you can plot the stored readings as in the code shown below.
For demonstration, I’m using synthetic (random) data in this example, but you can apply the same plotting logic to your real readings from the MPU6050.
function [] = simulate_sensor_plot()
% Generate synthetic acceleration data (10 samples, 3 axes: X, Y, Z)
accelReadings = randn(10, 3); % simulate acceleration in m/s^2
% Time axis (1 second per sample for this example)
time = 1:10;
% Separate the axes
x = accelReadings(:, 1);
y = accelReadings(:, 2);
z = accelReadings(:, 3);
% Plot the simulated data
figure;
plot(time, x, '-r', 'DisplayName', 'X-axis');
hold on;
plot(time, y, '-g', 'DisplayName', 'Y-axis');
plot(time, z, '-b', 'DisplayName', 'Z-axis');
hold off;
xlabel('Sample Number');
ylabel('Acceleration (m/s^2)');
title('Simulated Acceleration Data');
legend show;
grid on;
end
simulate_sensor_plot()
You can use the above logic directly by replacing the synthetic data with the actual data captured by the “MPU6050” sensor.

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!