Main Content

read

Read real-time sensor data at a specified rate

Add-On Required: This feature requires the MATLAB Support Package for Arduino Hardware add-on.

Description

example

[sensorReadings,overrun] = read(sensorobj) returns the sensor readings in timetable format. The timetable contains data read from the sensor associated with time data. The number of rows in the timetable depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to timetable. This syntax is applicable for all sensors.

example

[accelReadings, gyroReadings, magReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, angular velocity, magnetic field, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the MPU9250 and LSM9DS1 sensors.

[accelReadings, gyroReadings, magReadings, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, angular velocity, magnetic field, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the ICM20948 sensor.

example

[accelReadings, gyroReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, angular velocity, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the MPU6050 sensor.

[accelReadings, gyroReadings, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, angular velocity, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix This function signature is available only for the LSM6DS3, LSM6DS3H, LSM6DSL, LSM6DSM, LSM6DSR, LSM6DSO, , and ADIS16505 sensors.

[accelReadings, magReadings, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, magnetic field, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the LSM303C sensor.

[accelReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the ADXL34x family of sensors connected using the adxl345 object.

[humidityReading, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of humidity, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available only for the HTS221 sensor.

[pressureReading, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of pressure, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix. This function signature is available for LPS22HB and BMP280 sensors.

[accelReadings, tempReadings, timeStamps, overrun] = read(sensorobj) returns matrices of acceleration, temperature, time stamps, and overrun. The number of samples depends on the SamplesPerRead value specified while creating the sensor object. These output arguments are returned only when the output format is set to matrix This function signature is available only for the LIS3DH,

Examples

Read Data from Sensor as a Timetable

Create an Arduino object and include the I2C library.

a = arduino('COM4', 'Uno', 'Libraries', 'I2C');

Create a sensor object with additional properties.

Note

The sample code and output in this example is for mpu9250 object. If you are using another sensor that supports read function, use the corresponding sensor object.

sensorobj = mpu9250(a,'SampleRate',50,'SamplesPerRead',5,'ReadMode','Latest');

Read five samples of sensor data in datetime format.

[sensorReadings,overrun] = read(sensorobj)

sensorReadings =

 5×3 timetable

              Time                        Acceleration                         AngularVelocity                       MagneticField        
    ________________________    ________________________________    ______________________________________    ____________________________

    14-Dec-2018 15:01:34.832    -0.28261     0.30836      10.395    0.018968     -0.0050405     -0.0026529    8.4938     10.582    -17.051
    14-Dec-2018 15:01:34.852    -0.28261     0.30836      10.395    0.018968     -0.0050405     -0.0026529    8.4938     10.582    -15.687
    14-Dec-2018 15:01:34.872     -0.2874     0.31375      10.432    0.021356       0.001857     0.00026529    8.4938     9.8766    -17.051
    14-Dec-2018 15:01:34.892    -0.29339     0.30896      10.327    0.020427     -0.0013265    -0.00013265    8.4938     10.582    -17.733
    14-Dec-2018 15:01:34.912    -0.29339     0.30896      10.327    0.020427     -0.0013265    -0.00013265    7.0781     9.1711    -17.733



overrun =

     0

Read Accel, Gyro, and Mag Data from Sensor as a Matrix

Create an Arduino object and include the I2C library.

a = arduino('COM4', 'Uno', 'Libraries', 'I2C');

Create a sensor object with additional properties.

Note

The sample code and output in this example is for mpu9250 object. If you are using another sensor that supports this syntax of read function, use the corresponding sensor object.

sensorobj = mpu9250(a,'OutputFormat', 'matrix', 'SamplesPerRead', 2);

Read two samples of sensor data in matrix format.

[accel, gyro, mag, timeStamps, overrun] = sensorobj.read

accel =

   -0.6239    1.2747    9.5986
   -0.6263    1.2730    9.5861

gyro =

    0.0114   -0.0397    0.0155
    0.0106   -0.0382    0.0147

mag = 

    41.0133  101.0625  -13.1813
    40.2937  100.3406  -13.8750


timestamps =

   2×1 datetime array

   4-Feb-2021 15:53:18.790
   4-Feb-2021 15:53:18.800

overrun =

     5

Read Accel and Gyro Data from Sensor as a Matrix

Create an Arduino object and include the I2C library.

a = arduino('COM4', 'Uno', 'Libraries', 'I2C');

Create a sensor object with additional properties.

Note

The sample code and output in this example is for mpu6050 object. If you are using another sensor that supports this syntax of read function, use the corresponding sensor object.

sensorobj = mpu6050(a,'OutputFormat', 'matrix', 'SamplesPerRead', 2);

Read two samples of sensor data in matrix format.

[accel, gyro, timeStamps, overrun] = sensorobj.read

accel =

   -0.6239    1.2747    9.5986
   -0.6263    1.2730    9.5861

gyro =

    0.0114   -0.0397    0.0155
    0.0106   -0.0382    0.0147

timestamps =

   2×1 datetime array

   4-Feb-2021 15:53:18.790
   4-Feb-2021 15:53:18.800

overrun =

     5

Input Arguments

collapse all

The sensor object with the default or specified properties.

Output Arguments

collapse all

Data read from the sensor when the output format is set to timetable. The timetable returned has the following fields:

  • Time — Time stamps in datetime or duration format

  • Acceleration — N-by-3 array in units of m/s2

  • AngularVelocity — N-by-3 array in units of rad/s

  • MagneticField — N-by-3 array in units of µT (microtesla)

  • Temperature — N-by-1 array in units of celsius

  • Humidity — N-by-1 array specified in %

  • Pressure — N-by-1 array in units of Pascals

Note

The fields of timetable depends on the type of sensor. For example, in case of MPU6050, the output fields returned are Acceleration and AngularVelocity.

Data Types: timetable

The discarded number of data samples between the previous read and the current read is the overrun. The overrun is zero when ReadMode is set to oldest.

Data Types: single | double

Acceleration read from the sensor on x, y and z axis. This value is specified as a real, finite N-by-3 matrix in m/s2. N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Angular velocity read from the sensor on x, y and z axis. This value is specified as a real, finite N-by-3 matrix in rad/s. N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Magnetic field read from the sensor on x, y and z axis. This value is specified as a real, finite N-by-3 matrix in µT (microtesla). N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Temperature read by the sensor. This value is specified as a real, finite N-by-1 matrix in Celsius. N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Humidity value measured by the sensor. This value is specified as a real, finite N-by-1 matrix in %. N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Barometric pressure read from the sensor. This value is specified as a real, finite N-by-1 matrix in Pa (Pascals). N is the number of samples in the current frame, which is equal to the SamplesPerRead value.

Data Types: double

Time displayed when the sensor data is read.

  • datetime — Displays the date and time at which the data is read.

  • duration — Displays the time elapsed in seconds after the first call of the read function or the last execution of the release function.

More About

collapse all

Code Generation Using MATLAB Function Block

  • Use read in a MATLAB® Function block with the Simulink® Support Package for Arduino® Hardware to generate code that can be deployed on Arduino Hardware.

  • read does not return overrun.

  • read always returns matrices.

Version History

Introduced in R2019a