MATLAB Answers

Read data from Arduino Nano 33 IoT IMU sensor

66 ビュー (過去 30 日間)
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,
Matthias
clear;
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')

  0 件のコメント

サインインしてコメントする。

採用された回答

MathWorks MATLAB Hardware Team
MathWorks MATLAB Hardware Team 2020 年 4 月 22 日
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 =
105
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.

  6 件のコメント

表示 3 件の古いコメント
Nicholas Motta
Nicholas Motta 2020 年 11 月 24 日
Hi, I have the same problem Jeramy had earlier.
I ran the exact same code as he did here:
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')
And I get a vector of three zeros as a result.
I have the same Arduino and it is connected correctly, I don't know what the problem might be.
MathWorks MATLAB Hardware Team
MathWorks MATLAB Hardware Team 2020 年 11 月 25 日
Hi Nicholas,
Before reading data from the sensor, consider configuring it with the following defualt custom settings:
a) Set the gyroscope control register (addr = 0x11) to work at 104 Hz, 2000 dps and in bypass mode
>> writeRegister(d, 0x11, 0x4C);
b) Set the Accelerometer control register (addr = 0x10) to work at 104 Hz, 4G,and in bypass mode and enable ODR/4
>> writeRegister(d, 0x10, 0x4A);
c) Set the Angular rate control register (addr = 0x16) to have high performance gyroscope power mode and bandwidth equal to 16.32 MHz
>> writeRegister(d, 0x16, 0x00);
d) Set the ODR config register (addr = 0x17) to ODR/4
>> writeRegister(d, 0x17, 0x09);
NOTE: Refer to the sensor's datasheet to configure the registers to other settings.
Read acceleration values X, Y, Z
% normalizing w.r.t int16 and scaling it by 4g full scale
>> dataRead = (4/32768)*readRegister(d, 0x28, 3, 'int16');
dataRead =
0.0662 0.0104 0.9954
NOTE: The acceleration values dataRead are normalized with respect to 'g' i.e. 9.8 m/s2. Consider multiplying these normalized values with the standard value of 'g' to get the results in units of acceleration, m/s2.
Refer to the datasheet to access the appropriate registers for reading gyroscope and magnetometer data.
Hope it helps.
Nicholas Motta
Nicholas Motta 2020 年 11 月 25 日
Thank you. Works great now.

サインインしてコメントする。

その他の回答 (0 件)

タグ

Community Treasure Hunt

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

Start Hunting!

Translated by