Main Content


Read one sample of temperature from sensor

Since R2021a

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


[tempReadings,timestamp] = readtemperature(sensorobj) returns one sample of the temperature measured in degree Celsius along with the timestamp. Timestamp is optional.


The readTemperature function is available for the LSM6DS3, LSM6DS3H, LSM6DSL, LSM6DSM, LSM6DSR, LSM6DSO, LPS22HB, LSM303C, HTS221, ICM20948, ADXL345, BMP280, LIS3DH, and ADIS16505 sensors.


Read temperature Data from Sensor


Required libraries must be flashed to arduino object before using the sensor. For example, to read data from MPU6050 sensor using mpu6050 object, I2C library is required. Similarly, for the ADIS16505 sensor, SPI library is required. To flash the arduino with required libraries use the Libraries Name-Value Pair arguments during arduino object creation.

Create an Arduino object and include the I2C library.

a = arduino();

Or, you can explicitly specify it in the Libraries Name-Value pair while creating the Arduino object.

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

Create the sensor object for the sensor in use.


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

sensorobj = hts221(a);

Return one sample of temperature data.

temperatureReadings = readTemperature(sensorobj)
temperatureReadings =

Input Arguments

collapse all

The sensor object with the default or specified properties.

Output Arguments

collapse all

The temperature from the sensor.

The time at which MATLAB® receives temperature data from the sensor, specified as a datetime.

More About

collapse all

Code Generation Using MATLAB Function Block

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

  • Timestamp returned is always in seconds.

Version History

Introduced in R2021a