Main Content


Connect HTS221 sensor on Raspberry Pi hardware I2C bus

Since R2021a


The hts221 object reads ambient temperature and relative humidity using the STMicroelectronics® I2C-based HTS221 sensor interfaced with the Raspberry Pi® hardware. This object represents a connection from MATLAB® to the HTS221 sensor connected on the Raspberry Pi hardware board I2C pins I2C1_SDA (GPIO 2) and I2C1_SCL (GPIO 3). Before you use the hts221 object, ensure that the I2C interface is enabled on your Raspberry Pi kernel. You can then create a connection to the Raspberry Pi board using the raspi object and set its properties. For more information, see enableI2C and raspi.




sensor = hts221(mypi) creates a HTS221 sensor object with default property values. The object represents a connection to the sensor on the Raspberry Pi hardware mypi.


sensor = hts221(mypi,Name,Value) creates a HTS221 sensor object with properties using one or more Name,Value arguments.

Input Arguments

expand all

Raspberry Pi hardware board connection specified as a raspi object.

Example: sensor = hts221(mypi) creates a connection to the HTS221 sensor on the Raspberry Pi object mypi.

Name-Value Arguments

Specify optional pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

I2C bus address of the Raspberry Pi hardware board.

Example: sensor = hts221(mypi, 'Bus', 'i2c-1') creates the HTS221 sensor object on 'i2c-1' bus of the Raspberry Pi board.

Data Types: char

Object Functions

readTemperatureRead one sample of temperature from sensor
readHumidityRead one relative humidity data sample from HTS221 sensor


collapse all

Ensure that the I2C interface on the Raspberry Pi kernel is enabled and then create an hts221 sensor object.

You can enable the I2C interface using:

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)

GPIO header pins I2C_SDA (GPIO 2) and I2C_SCL (GPIO 3) are used for I2C functionality. Display pin mapping of the Raspberry Pi hardware board.

Raspberry Pi 3 Model B+ GPIO Pin Map diagram

Get the address of the HTS221 sensor connected to the I2C bus 'i2c-1'.

sensor = scanI2CBus(mypi,'i2c-1');
ans =

  1×1 cell array


Create an hts221 sensor object on the I2C bus 'i2c-1' on address '0x5F'.

sensor = hts221(mypi,'Bus','i2c-1');
sensor = 

  hts221 with properties:

                         I2CAddress: 95 ("0x5F")
                                Bus: i2c-1

Extended Capabilities

Version History

Introduced in R2021a