MATLAB Answers

Read data from Arduino Nano 33 IoT IMU sensor

47 views (last 30 days)
Hey everyone!
I would like to read the data from the integrated LSM6DS3 IMU sensor in the Arduino Nano 33 IoT.
When using "port1", I only get an array filled with zeros with the read function or a single zero with the readRegister function.
When using "port2", I get [4 17 51 67 255 255 255 ... ] with the read function an 4356 with the readRegister function, independent from movement or tilt.
I've already read the documentation, but it seems I don't really understand what is going on here.
And what does the error message "I2C address '96 (0x60)' already in use." mean when I try to run the script again without clearing the arduino object first?
I would be glad if someone could help me!
Kind regards,
a = arduino('COM8','Nano33IoT','Libraries','I2C');
%% Scan for device adress
ports = scanI2CBus(a,0);
% Avaliable ports
port1 = '0x60';
port2 = '0x6A';
IMU = device(a,'I2CAddress',port2);
out = read(IMU,10);
value = readRegister(IMU,port2,'uint16')


Sign in to comment.

Accepted Answer

MathWorks MATLAB Hardware Team
Hi Matthias,
I see that you are using a correct subset of I2C APIs documented to read out the sensor register. However, the data must be read from registers specified in the datasheet. For intsance, if you wish to read linear acceleration values along all the X,Y, and Z directions, values at 0x28 must be accessed. The sensor can also be configured with some non-zero default settings beforehand.
Use the slave address 0x6A to construct the I2C device object.
>> IMU = device(a, 'I2CAddress', 0x6A);
Before reading any data, you can verify that the sensor is readable by reading the WHO_AM_I register with address 0x0F.
>> readRegister(IMU, 0x0F, 1)
ans =
Here is an example of reading acceleration with default 2g setting:
% Read three 'int16' type data values starting from address 0x28.
% Normalize and scale it to 2g (chosen by default)
>> dataRead = (2/32768)*readRegister(IMU, 0x28, 3, 'int16')
ans =
0.4904 0.0054 0.2997
NOTE : The values will differ as per the IMU position
Similarly, you can try other settings present in the datasheet. Moreover, you may use the writeRegister API to configure the registers.
Our support doesn’t allow multiple connections to same device for ease of use.
If you want to use the existing connection in workspace without clearing it; you can bypass the device creation and proceed with read
Hope this helps. Please contact MathWorks Technical Support for further queries.


Jeramy Miller
Jeramy Miller on 7 Sep 2020 at 1:00
If you don't mind me asking, my Nano IoT with a LSM6DS3 spits out zeros when I follow your process, even for the registers that provide gyroscopic and temperature data (while moving it around of course). Is it possible this thing requires being initialized first through matlab by writing to some address?
I'm a bit over my head trying to read through the datasheet.
MathWorks MATLAB Hardware Team
MathWorks MATLAB Hardware Team on 7 Sep 2020 at 10:53
Hi Jeramy,
Can you please confirm if you have created an arduino object using the
MATLAB Support Package for Arduino Hardware for the same?
Let us know.
Jeramy Miller
Jeramy Miller on 7 Sep 2020 at 22:15
Thanks for the response. I had ran everything listed here as a script and it put out zeros the first time;
clear all;clc
a = arduino('COM7','Nano33IoT','Libraries','I2C')
IMU = device(a, 'I2CAddress', 0x6A)
readRegister(IMU, 0x0F, 1)
dataRead = (2/32768)*readRegister(IMU, 0x28, 3, 'int16')
However, I just ran it again and it is displaying correct values. Also, my I2C Master Read block in Simulink is also putting out numbers now. I'm not really sure what happened.

Sign in to comment.

More Answers (0)


Community Treasure Hunt

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

Start Hunting!

Translated by