View Source HTS221 (hts221 v1.1.0)

Functions for working with the HTS221

The functionality is useful for use cases where you need complete control over the sensor or to debug the registers. If you just want to get started quickly see HTS221.Server module.

To read registers you will need to provide a HTS221.Transport.t().

{:ok, transport} = HTS221.Transport.init(HTS221.Transport.I2C, bus_name: "i2c-1")

Reading Registers

After opening the transport you can read or write registers.

{:ok, %HTS221.Temperature{} = temp} = HTS221.read_temperature(transport)

While this library provides some common helper functions to read particular registers it does not provide all the registers currently. However, the library does provide the HTS221.Register protocol and HTS221.read_register/2 that will allow you to provide support for any register.

Writing Registers

To write a register you will need to use HTS221.write_register/2.

# this will provide the default register values
ctrl_reg1 = %HTS221.CTRLReg1{}

HTS221.write_register(transport, ctrl_reg1)

Calibration

Each HTS221 is calibrated that the factory and the calibration that is stored in non-volatile memory is specific to each sensor. The calibration contains data about how to calculate the temperature and humidity and thus you will need to the calibration values to make those calculations.

This library provides functionality for reading the calibration and using it to calculate the temperature and humidity.

{:ok, %HTS221.Calibration{} = calibration} = HTS221.read_calibration(transport)

{:ok, %HTS221.Temperature{} = temp} = HTS221.read_temperature(transport)

temp_in_celsius = HTS221.calculate_temperature(temp, calibration)

the same steps are required for calculating humidity

Link to this section Summary

Types

Signed 16-bit integer

The scale in which the temperature is calculated (default :celsius)

Functions

Calculate the humidity from the HTS221.Humidity register values

Calculate the temperature from the HTS221.Temperature register values

Read the AV_CONF register

Read the calibration on the HTS221

Read the CTRL_REG1 register

Read the values of the humidity registers

Read any register that implements the HTS221.Register protocol

Read the values of the temperature registers

Write any register that implements the HTS221.Register protocol

Link to this section Types

Specs

opt() :: {:scale, scale()}

Specs

s16() :: -32768..32767

Signed 16-bit integer

Specs

scale() :: :celsius | :fahrenheit | :kelvin

The scale in which the temperature is calculated (default :celsius)

Link to this section Functions

Link to this function

calculate_humidity(humidity, calibration)

View Source

Specs

calculate_humidity(HTS221.Humidity.t(), HTS221.Calibration.t()) :: float()

Calculate the humidity from the HTS221.Humidity register values

This requires the HTS221.Calibration has the the humidity register values are the raw reading from the ADC. Each HTS221 is calibrated during manufacturing and contains the coefficients to required to convert the ADC values into percent.

Link to this function

calculate_temperature(temperature, calibration, opts \\ [])

View Source

Specs

calculate_temperature(HTS221.Temperature.t(), HTS221.Calibration.t(), [opt()]) ::
  float()

Calculate the temperature from the HTS221.Temperature register values

This requires the HTS221.Calibration has the the temperature register values are the raw reading from the ADC. Each HTS221 is calibrated during manufacturing and contains the coefficients to required to convert the ADC values into degrees celsius (default).

Specs

read_av_conf(HTS221.Transport.t()) :: {:ok, HTS221.AVConf.t()} | {:error, any()}

Read the AV_CONF register

See the HTS221.AVConfig module for more information.

Link to this function

read_calibration(transport)

View Source

Specs

read_calibration(HTS221.Transport.t()) ::
  {:ok, HTS221.Calibration.t()} | {:error, any()}

Read the calibration on the HTS221

This is useful for checking the calibration on the hardware itself or fetch the calibration after any other register initialization and storing it for future calculations.

{:ok, calibration} = HTS221.read_calibration(hts221)

%HTS221{hts221 | calibration: calibration}
Link to this function

read_ctrl_reg1(transport)

View Source

Specs

read_ctrl_reg1(HTS221.Transport.t()) ::
  {:ok, HTS221.CTRLReg1.t()} | {:error, any()}

Read the CTRL_REG1 register

See the HTS221.CTRLReg1 module for more information.

Link to this function

read_humidity(transport)

View Source

Specs

read_humidity(HTS221.Transport.t()) ::
  {:ok, HTS221.Humidity.t()} | {:error, any()}

Read the values of the humidity registers

This function does not provided the final calculations of the humidity but only provides the functionality of reading the raw values in the register.

Link to this function

read_register(transport, register)

View Source

Specs

read_register(HTS221.Transport.t(), HTS221.Register.t()) ::
  {:ok, binary()} | {:error, any()}

Read any register that implements the HTS221.Register protocol

Link to this function

read_temperature(transport)

View Source

Specs

read_temperature(HTS221.Transport.t()) ::
  {:ok, HTS221.Temperature.t()} | {:error, any()}

Read the values of the temperature registers

This function does not provide the final calculations of the temperature but only provides the functionality of reading the raw values in the register.

Link to this function

write_register(transport, register)

View Source

Specs

write_register(HTS221.Transport.t(), HTS221.Register.t()) ::
  :ok | {:error, any()}

Write any register that implements the HTS221.Register protocol