MPU6050 Arduino Uno Simulink
Show older comments
I have a code to get the MPU6050 working using Matlab, but now I would like to run it on Simulink, but I keep hitting dead ends. Here is my Matlab code if anyone out there can help me:
%%setup
a=arduino;
mpu=i2cdev(a,'0x68');
writeRegister(mpu, hex2dec('B6'), hex2dec('00'), 'int16'); %reset
data=zeros(10000,14,'int8'); %prelocating
j=1;
%%loop
while(true)
x=1;
for i=59:72 % 14 Data Registers for Accel,Temp,Gyro
data(j,x)= readRegister(mpu, i, 'int8');
x=x+1;
end
y=swapbytes(typecast(data(j,:), 'int16'));
acc_x(j)=double(y(1));
acc_y(j)=double(y(2));
acc_z(j)=double(y(3));
j=j+1;
end
How can I write this in SIMULINK? The loop is the easy part, I can just put it into a Matlab function block (except for readRegister)... I just can't figure out the setup part in Simulink at all. I have tryed using the I2C Read block, but to no avail. Please help. And before you ask, I do have both the Arduino packages installed (for Matlab, and Simulink).
Again Please HELP!!!
3 Comments
fatima zohra soussi
on 23 Sep 2020
@Alexandros are you do it that if yes can you help me 🙏🙏🙏🙏🙏
Jose Hernandez
on 12 Dec 2020
Edited: Jose Hernandez
on 12 Dec 2020
@fatima zohra soussi Install and use Matlab 2020b, then install Arduino support package,then go to Simulink and go to Arduino blocks, go to sensors blocks and check which sensors are available ;)
Donny WARINT
on 30 Sep 2023
Hi,
I had got the same problem.
I solved by used Stateflow block as describe at path below.
Best Regards.
Answers (3)
Gayatri Menon
on 4 Jan 2022
1 vote
1 Comment
Ana Luz Gómez Plata
on 6 Jun 2022
Sorry, do you know how to do it, I need help:(
Nathan Hardenberg
on 2 Feb 2023
Edited: Nathan Hardenberg
on 2 Feb 2023
1 vote
- You need the "Simulink Support Package for Arduino Hardware" (Can be installed as Add-On)
- Use the "MPU6050 IMU Sensor"-Block from the Simulink library (or other sensorblock)
- Build a model like the one below. Here it just shows the values.

- Set the Model- and Hardware-Settings: Solver > Type > Fixed-step and Hardware Implementation > Hardware board > {choose_the_board_you_are_using}
- Wire your Ardunio (or other hardwareboard) correctly: Sensor connected to the I2C-Bus and VCC and GND to the Sensor
- Connect your Arduino to your PC with a USB-Cable
- Now Klick Monitor & Tune in Simulink
The code will be generated and deployed on the hardware. Since this is in monitoring mode you can see the Simulink-model showing the sensor values. This allows easy debugging.
!WARNING!
The "MPU6050 IMU Sensor"-Block seems to have an issue at the moment (see here). It reports the error status 32, even though the sensor works. Hopefully this will be fixed in a future update. Another IMU sensor that works is the MPU9250. But the given method is the same with this and other sensors as well.
6 Comments
Gurram
on 22 Jun 2023
First of all thankyou for the explaination and it got worked out for me.
But i want the roll, pitch and yaw values invidually in 3 seperate display's
Similarly, also the x,y,z values invidually in 3 seperate display's
Is it possible by chaning any of the display properties , kindly help me with this.
Nathan Hardenberg
on 23 Jun 2023
The question you asked, tells me that you do not really know what the IMU does. But that's no problem. Still, I would recommend that you google a bit and/or watch a video on how they work. In short, they just measure the acceleration and angular acceleration. This does not give you an angle!
To get an angle there are multiple ways. The easiest would be to only use the accelerometer and measure the direction of the acceleration due to gravity. From this you can calculate the current angles. One possible implementation of this is shown here: https://engineering.stackexchange.com/a/3350/42151
If your object has a high acceleration and generally moves fast (a drone for example) just using the accelerometer is not sufficient. In this case you must also take the gyro measurements and integrate them along each axis. This gives you (with the right calculation) relative movement (the math is not trivial though). In this case you have to use a method called "sensor fusion" (general name), where you fuse both sensor data.
Hope this small overview helps

I would say thank you so much for the detailed explaination.
As you said I'm really not very much aware about the IMU sensor.
but let me put it in simple manner, what i actually want.
As you can see in the figure i have obtained 3 values of acceleraton in one display and 3 values of angular acceleration in other display. But i actually want them to be individually presented rather than grouping into three three.
ONE(each) VALUE IN ONE(each) DISPLAY.
Nathan Hardenberg
on 26 Jun 2023
Thale
on 26 Jun 2023
Heyy!
How do you connect the MPU6050 to the arduino uno though? I get Status=32 when I monitor and tune the program. Have I connected it wrong to the ardouino maybe?
Thank you :)
Nathan Hardenberg
on 26 Jun 2023
@Thale Eliassen Fink I think you did not read my answer fully! Check out the "!WARNING!" I wrote. This problem has no fix to my knowlege. It seems to work for @Gurram though. @Gurram are you using the MPU6050 Hardware Sensor? If so, this may be an issue with some hardware-boards/chips.
You could try to read out the data using the "I2C Read"-Block. But you have to check the datasheet for the right registers and convert the bytes to a double accordingly
Gurram
on 26 Jun 2023
1 vote
I have used the hardware sensor and got the output .
The output picture is available in thr above comment.
and these are the connections , i have done.

3 Comments
Thale
on 27 Jun 2023
Hi! Thank you for the answers. Yes, I now connected the way Gurram showed, and tried making my own function block for reading from the IMU (To solve the Status=32 issue Nathan was talking about), by changing the IMU-type in the existing matlab function block https://de.mathworks.com/matlabcentral/answers/1461574-sensor_init_error-mpu6050-imu-sensor-for-arduino-mega-2560-in-simulink.
When having made the function block though, I got a new error, similar to this post; https://se.mathworks.com/matlabcentral/answers/837698-mpu6050-pitch-and-roll-simulink-example-not-working-mpu6050-arduino-simulink-interface The solution was to download the add-on in matlab "MATLAB Support for MinGW-w64 C/C++ Compiler". After this it worked, and I was able to read the angles from the IMU.
Abdullah CAKAN
on 12 Mar 2025
I did same with MPU6050, I got no errors but i got only zero in the display ports. I don't know what is wrong.
omar
on 18 Apr 2025
@Abdullah CAKAN I have the same problem did you find a solution??
Categories
Find more on Arduino Hardware 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!