Adafruit Unified Sensor Driver Documentation

repository·master·Indexed 21 days ago

https://github.com/adafruit/adafruit_sensor

A standardized abstraction layer for embedded sensor drivers that ensures different sensor models return data in consistent formats and SI units. It provides the Adafruit_Sensor base class, sensor_t for metadata, and sensors_event_t for data retrieval via getEvent() and getSensor() functions.

Tokens
3.1K
Snippets
6
Records
8
Agent score
27%

What's inside Adafruit Unified Sensor Driver

  1. Understand the Adafruit Unified Sensor Abstraction

    master

    The Adafruit Unified Sensor Driver provides an abstraction layer for embedded sensor systems. It allows developers to write code that is decoupled from specific sensor hardware by using a standardized set of data types and SI units.

    By using this abstraction, you can swap one sensor model for another (e.g., switching from an ADXL345 to an LSM303DLHC accelerometer) with minimal changes to your application logic, as long as both drivers implement the Adafruit_Sensor base class and return data in the same standardized units.

  2. Query sensor capabilities using `getSensor()`

    master

    You can interrogate a compliant sensor about its technical specifications and capabilities using the getSensor() method. This populates a sensor_t structure containing metadata such as the sensor's name, driver version, unique ID, and measurement ranges (min/max values and resolution).

     sensor_t sensor;
     tsl.getSensor(&sensor);
    
     /* Display the sensor details */
     Serial.println("------------------------------------");
     Serial.print  ("Sensor:       "); Serial.println(sensor.name);
     Serial.print  ("Driver Ver:   "); Serial.println(sensor.version);
     Serial.print  ("Unique ID:    "); Serial.println(sensor.sensor_id);
     Serial.print  ("Max Value:    "); Serial.print(sensor.max_value); Serial.println(" lux");
     Serial.print  ("Min Value:    "); Serial.print(sensor.min_value); Serial.println(" lux");
     Serial.print  ("Resolution:   "); Serial.print(sensor.resolution); Serial.println(" lux");  
     Serial.println("------------------------------------");
     Serial.println("");
  3. Read sensor data using `getEvent()`

    master

    Once a sensor driver is compliant with the Adafruit Unified Sensor abstraction, you can retrieve sensor readings using the getEvent() method. This method populates a sensors_event_t structure with standardized data. For example, a light sensor will populate the event.light field with values in lux. If event.light is 0, it may indicate sensor saturation or an error.

     Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345);
     ...
     /* Get a new sensor event */ 
     sensors_event_t event;
     tsl.getEvent(&event);
     
     /* Display the results (light is measured in lux) */
     if (event.light)
     {
       Serial.print(event.light); Serial.println(" lux");
     }
     else
     {
       /* If event.light = 0 lux the sensor is probably saturated
          and no reliable data could be generated! */
       Serial.println("Sensor overload");
     }
  4. Implement required functions for Unified Sensor drivers

    master

    To implement a driver compatible with the Adafruit Unified Sensor abstraction, you must provide the following two functions:

    1. bool getEvent(sensors_event_t*): Populates the provided sensors_event_t reference with the latest available sensor data. This should be called whenever you need updated readings.
    2. void getSensor(sensor_t*): Populates the provided sensor_t reference with basic sensor information (name, version, range, etc.).
    bool getEvent(sensors_event_t*);
    
    void getSensor(sensor_t*);
  5. Reference standardized SI units for sensor data

    master

    The Unified Sensor Driver standardizes all sensor readings to specific SI units. When consuming sensors_event_t data, use the following scales:

    Sensor TypeUnitScale/Details
    accelerationm/s²meter per second per second
    magneticuTmicro-Tesla
    orientationdegrees
    gyrorad/s
    temperature°Cdegrees centigrade
    distancecmcentimeters
    lightluxSI lux
    pressurehPahectopascal
    relative_humidity%percent
    currentmAmilliamps
    voltageVvolts
    colorRGB0..1.0 luminosity, 32-bit RGBA
    tvocppbparts per billion
    voc_indexindex1-500 (100 is normal)
    nox_indexindex1-500 (100 is normal)
    CO2 / eCO2ppmparts per million
    PM (std/env)ppmparts per million
    gas_resistanceΩohms
    unitless_percent%
    altitudemmeters
  6. Reference the `sensors_type_t` enumeration

    master

    The sensors_type_t enum defines the category of sensor being used. This type is used within both sensor_t (to describe the sensor's capabilities) and sensors_event_t (to identify the type of data contained in the event).

    /** Sensor types */
    typedef enum
    {
      SENSOR_TYPE_ACCELEROMETER         = (1),
      SENSOR_TYPE_MAGNETIC_FIELD        = (2),
      SENSOR_TYPE_ORIENTATION           = (3),
      SENSOR_TYPE_GYROSCOPE             = (4),
      SENSOR_TYPE_LIGHT                 = (5),
      SENSOR_TYPE_PRESSURE              = (6),
      SENSOR_TYPE_PROXIMITY             = (8),
      SENSOR_TYPE_GRAVITY               = (9),
      SENSOR_TYPE_LINEAR_ACCELERATION   = (10),
      SENSOR_TYPE_ROTATION_VECTOR       = (11),
      SENSOR_TYPE_RELATIVE_HUMIDITY     = (12),
      SENSOR_TYPE_AMBIENT_TEMPERATURE   = (13),
      SENSOR_TYPE_VOLTAGE               = (15),
      SENSOR_TYPE_CURRENT               = (16),
      SENSOR_TYPE_COLOR                 = (17),
      SENSOR_TYPE_TVOC                  = (18),
      SENSOR_TYPE_VOC_INDEX             = (19),
      SENSOR_TYPE_NOX_INDEX             = (20),
      SENSOR_TYPE_CO2                   = (21),
      SENSOR_TYPE_ECO2                  = (22),
      SENSOR_TYPE_PM10_STD              = (23),
      SENSOR_TYPE_PM25_STD              = (24),
      SENSOR_TYPE_PM100_STD             = (25),
      SENSOR_TYPE_PM10_ENV              = (26),
      SENSOR_TYPE_PM25_ENV              = (27),
      SENSOR_TYPE_PM100_ENV             = (28),
      SENSOR_TYPE_GAS_RESISTANCE        = (29),
      SENSOR_TYPE_UNITLESS_PERCENT      = (30),
      SENSOR_TYPE_ALTITUDE              = (31),
    } sensors_type_t;
  7. Use `sensors_event_t` to retrieve sensor data

    master

    The sensors_event_t struct provides a common format for sensor data using standardized SI units. It uses a union to encapsulate different types of data based on the type field.

    Fields:

    • version: Contains sizeof(sensors_event_t) to identify the API version.
    • sensor_id: Must match the sensor_id in the corresponding sensor_t.
    • type: The sensors_type_t value.
    • timestamp: Time in milliseconds when the value was read.
    • data[4] (or specific union members): The actual sensor reading.
    /* Sensor event (36 bytes) */
    /** struct sensor_event_s is used to provide a single sensor event in a common format. */
    typedef struct
    {
        int32_t version;
        int32_t sensor_id;
        int32_t type;
        int32_t reserved0;
        int32_t timestamp;
        union
        {
            float           data[4];
            sensors_vec_t   acceleration;
            sensors_vec_t   magnetic;
            sensors_vec_t   orientation;
            sensors_vec_t   gyro;
            float           temperature;
            float           distance;
            float           light;
            float           pressure;
            float           relative_humidity;
            float           current;
            float           voltage;
            float           tvoc;
            float           voc_index;
            float           nox_index;
            float           CO2, 
            float           eCO2,
            float           pm10_std,
            float           pm25_std,
            float           pm100_std,
            float           pm10_env,
            float           pm25_env,
            float           pm100_env,
            float           gas_resistance,
            float           unitless_percent,
            float           altitude,
            sensors_color_t color;
        };
    } sensors_event_t;
  8. Use `sensor_t` to retrieve sensor metadata

    master

    The sensor_t struct describes the specific capabilities and identity of a sensor. It is used to determine the sensor's name, version, range of values, and resolution.

    Fields:

    • name: Sensor name or ID (max 12 chars).
    • version: Hardware and driver version.
    • sensor_id: Unique identifier for the specific sensor instance.
    • type: The sensors_type_t value.
    • max_value: Maximum returnable value in SI units.
    • min_value: Minimum returnable value in SI units.
    • resolution: Smallest reportable difference in SI units.
    • min_delay: Minimum delay between events in microseconds (0 if no constant rate).
    /* Sensor details (40 bytes) */
    /** struct sensor_s is used to describe basic information about a specific sensor. */
    typedef struct
    {
        char     name[12];
        int32_t  version;
        int32_t  sensor_id;
        int32_t  type;
        float    max_value;
        float    min_value;
        float    resolution;
        int32_t  min_delay;
    } sensor_t;