Main Content


Read one sample of temperature from sensor

Since R2021a

Add-On Required: This feature requires the MATLAB Support Package for Raspberry Pi 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, and LIS3DH sensors.


collapse all

Create a connection from MATLAB® to the Raspberry Pi® hardware board.

mypi = raspi('raspberrypi-hysdu8X38o','rocky','bullwinkle')
mypi = 

  Raspi with Properties:

           DeviceAddress: 'raspberrypi-hysdu8X38o'
                    Port: 18734
               BoardName: 'Raspberry Pi 3 Model B+'
           AvailableLEDs: {'led0'}
    AvailableDigitalPins: [4,5,6,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27]
    AvailableSPIChannels: {‘CE0’,’CE1’}
       AvailableI2CBuses: {'i2c-1'}
             I2CBusSpeed: 100000
        AvailabelWebCams: mmal service 16.1 (platform:bcm2835-v4l2)

Create a sensor object on the I2C bus 'i2c-1' on addresses '0x6B'.


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

sensorobj = lsm6dsl(mypi,'Bus','i2c-1','I2CAddress','0x6B');
sensorobj = 

  lsm6dsl with properties:

                         I2CAddress: 107 ("0x6B")
                                Bus: i2c-1

Return one sample of ambient temperature data read from the sensor.

[tempReading,timestamp] = readTemperature(sensor);
tempReading =



timestamp = 


   5-Jan-2021 11:35:17.279

Since the timestamp output is optional, you can obtain only the ambient temperature data from the sensor.

[tempReading] = readTemperature(sensor);
tempReading =



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.

Extended Capabilities

Version History

Introduced in R2021a