Sensors

Reference

Reference for following capitalized words in this page:

  • SENSOR
    • sensor name being used (as a prefix to functions), e.g. hdc1080 or adc which then translates to hdc1080_open(..) or adc_read(..)
  • DEV
    • struct i2c_device * in case of sensor connected to I2C bus
    • struct spi_device * in case of sensor connected to SPI bus
    • other device structs depending on bus and so on
  • MASTER
    • struct i2c_master * in case of sensor connected to I2C bus
    • struct spi_master * in case of sensor connected to SPI bus
    • other master structs depending on bus and so on

Generic Functions

int8_t SENSOR_open(DEV, MASTER, int32_t ref, int32_t cfg)
Parameters:
  • DEV – See Reference
  • MASTER – See Reference
  • ref (int32_t) –

    Reference information to driver

    • I2C: Optional address if device supports more than one, set to zero to use default
    • SPI: Slave select pin
  • cfg (int32_t) –

    Driver specific configuration option

    • T&H sensors: Device specific measurement resolution or similar setting
    • 0: use default
    • -1: skip configuration and use what ever the chip is using currently
Returns:

  • 0 on success
  • -1 if device not found
  • -2 on other errors, e.g. configuration failed

void SENSOR_close(DEV)
Parameters:

ADC

Analog to digital converters.

Functions

int8_t SENSOR_vref(DEV, float vref)
Parameters:
  • DEV – See Reference
  • vref (float) – Reference voltage to use when reading
Returns:

  • 0 on success
  • -1 if device not found
  • -2 on other errors

Set reference voltage used by ADC. Default is 1.0 volts.

See SENSOR_read().

int8_t SENSOR_zero(DEV, float zero)
Parameters:
  • DEV – See Reference
  • zero (float) – Zero offset to apply when reading
Returns:

0

Set zero offset. Default is zero which means no effect.

See SENSOR_read().

int8_t SENSOR_multiplier(DEV, float multiplier)
Parameters:
  • DEV – See Reference
  • multiplier (float) – Multiplier to apply when reading
Returns:

0

Default is 1.0.

See SENSOR_read().

int8_t SENSOR_read(DEV, int32_t *raw, float *value)
Parameters:
  • DEV – See Reference
  • *raw (int32_t) –

    Save raw reading here if not NULL.

    Note that differential ADC readings can be negative. *raw is a 32 bit signed integer which means that example a 16 bit differential ADC reading is returned between -32768..32767 as a 32 bit signed integer, not 16 bits of raw data saved in 32 bit signed integer.

  • *value (float) – Save converted value here after applying formula shown in description. Can be NULL.
Returns:

  • 0 on success
  • -1 if device not found
  • -2 on other errors

Read ADC value. Formula applied to raw value for converting it to *value:

\((RAW\_VALUE / MAX\_VALUE * vref - zero) * multiplier\)

ADC Drivers

None at the moment. Only documentation written to be used when writing actual drivers.

adc (internal)

Internal device ADC driver, if exists.

Temperature and Humidity

Temperature and humidity sensors are combined together under same API.

Note

Even sensors that provide only one use the same API.

Functions

int8_t SENSOR_heater(DEV, bool on)
Parameters:
  • DEV – See Reference
  • on (bool) – Heater on or off
Returns:

  • 0 on success
  • -1 if device not found
  • -2 on other errors
  • -3 if device has no heater

Enable internal heater if device has such. Usually heater will only apply heat when a measurement is requested so in order to actually heat the device sensor should be read continuously.

int8_t SENSOR_read(DEV, float *t, float *h)
Parameters:
  • DEV – See Reference
  • *t (float) –

    Save temperature here if not NULL

    • Set to -274 if device does not support temperature reading
    • Set to -275.0 on other errors, e.g. reading failed
  • *h (float) –

    Save humidity here if not NULL

    • Set to -1 if device does not support humidity reading
    • Set to -2 on other errors, e.g. reading failed
Returns:

  • 0 on success
  • -1 if device not found
  • -2 on other errors

Read device temperature and humidity values.

T&H Drivers

hdc1080

SENSOR_open()

  • ref is not used

  • cfg is resolution

    • 14 bits (default)
    • 11 bits

SENSOR_heater()

  • Has a heater

sht21

SENSOR_open()

  • ref is not used

  • cfg is resolution

    • 14: T = 14 bits, RH = 12 bits (default)
    • 13: T = 13 bits, RH = 10 bits
    • 12: T = 12 bits, RH = 8 bits
    • 11: T = 11 bits, RH = 11 bits

SENSOR_heater()

  • Has a heater

sht31

SENSOR_open()

  • ref is I2C address

    • 0x44 (default)
    • 0x45
  • cfg is repeatability

    enumerator SHT31_REPEATABILITY_HIGH = 0x00
    enumerator SHT31_REPEATABILITY_MEDIUM = 0x0b
    enumerator SHT31_REPEATABILITY_LOW = 0x16

SENSOR_heater()

  • Has a heater