### Sensor Capabilities Structure (sensor_t) Example Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Shows how to retrieve and display sensor capabilities using the sensor_t structure. This example uses a TSL2561 light sensor. It requires the Adafruit_Sensor and Adafruit_TSL2561 libraries. The output details the sensor's name, driver version, ID, measurement range, resolution, and minimum delay. ```cpp #include #include Adafruit_TSL2561 tsl = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 12345); void displayCapabilities() { sensor_t sensor; tsl.getSensor(&sensor); // Display sensor capabilities 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.print("Min Delay: "); Serial.print(sensor.min_delay); Serial.println(" microseconds"); Serial.println("------------------------------------ "); } void setup() { Serial.begin(9600); if(!tsl.begin()) { Serial.println("No TSL2561 detected!"); while(1); } displayCapabilities(); } void loop() { sensors_event_t event; tsl.getEvent(&event); if (event.light) { Serial.print(event.light); Serial.println(" lux"); } else { // Sensor saturated Serial.println("Sensor overload"); } delay(1000); } ``` -------------------------------- ### Read Multiple Sensors with Unified Interface (C++) Source: https://context7.com/adafruit/adafruit_sensor/llms.txt This example demonstrates how to read data from multiple different sensor types (temperature, pressure, humidity, light, accelerometer) using the Adafruit Unified Sensor library. It initializes the sensors, reads their data, and outputs it in CSV format. Ensure all required Adafruit sensor libraries are included. ```cpp #include #include #include #include #include // Create multiple sensors with unique IDs Adafruit_BME280 bme; Adafruit_TSL2561 light = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 1001); Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(1002); struct SensorData { float temperature; float pressure; float humidity; float light; float accelX, accelY, accelZ; uint32_t timestamp; bool valid; }; SensorData readAllSensors() { SensorData data = {0}; data.timestamp = millis(); data.valid = true; sensors_event_t event; // Read temperature/pressure/humidity if (bme.getEvent(&event)) { data.temperature = event.temperature; data.pressure = event.pressure; data.relative_humidity = event.relative_humidity; } else { data.valid = false; } // Read light sensor if (light.getEvent(&event)) { data.light = event.light; } else { data.valid = false; } // Read accelerometer if (accel.getEvent(&event)) { data.accelX = event.acceleration.x; data.accelY = event.acceleration.y; data.accelZ = event.acceleration.z; } else { data.valid = false; } return data; } void setup() { Serial.begin(9600); while (!Serial) delay(10); Serial.println("Multi-Sensor System Starting..."); // Initialize all sensors if (!bme.begin()) { Serial.println("BME280 init failed!"); } if (!light.begin()) { Serial.println("TSL2561 init failed!"); } if (!accel.begin()) { Serial.println("LSM303 init failed!"); } light.enableAutoRange(true); Serial.println("All sensors initialized\n"); } void loop() { SensorData data = readAllSensors(); if (data.valid) { // CSV format for data logging Serial.print(data.timestamp); Serial.print(","); Serial.print(data.temperature, 2); Serial.print(","); Serial.print(data.pressure, 2); Serial.print(","); Serial.print(data.humidity, 2); Serial.print(","); Serial.print(data.light, 2); Serial.print(","); Serial.print(data.accelX, 3); Serial.print(","); Serial.print(data.accelY, 3); Serial.print(","); Serial.print(data.accelZ, 3); Serial.println(); } else { Serial.println("ERROR: Sensor read failed"); } delay(1000); } ``` -------------------------------- ### Implement Custom Sensor Driver (C++) Source: https://context7.com/adafruit/adafruit_sensor/llms.txt This example shows how to create a custom sensor driver by implementing the Adafruit_Sensor interface. It defines a 'CustomTempSensor' class that can be used to integrate a new sensor into the Adafruit Unified Sensor ecosystem. The driver handles sensor initialization, event retrieval, and sensor property reporting. ```cpp #include // Custom temperature sensor example class CustomTempSensor : public Adafruit_Sensor { private: int32_t _sensorID; uint8_t _pin; public: CustomTempSensor(int32_t sensorID, uint8_t pin) { _sensorID = sensorID; _pin = pin; } bool begin() { pinMode(_pin, INPUT); return true; } // Required: Implement getEvent() bool getEvent(sensors_event_t* event) { if (!event) return false; // Clear the event memset(event, 0, sizeof(sensors_event_t)); // Populate event structure event->version = sizeof(sensors_event_t); event->sensor_id = _sensorID; event->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; event->timestamp = millis(); // Read sensor (example: analog thermistor) int rawValue = analogRead(_pin); float voltage = rawValue * (5.0 / 1023.0); event->temperature = (voltage - 0.5) * 100.0; // Example conversion return true; } // Required: Implement getSensor() void getSensor(sensor_t* sensor) { if (!sensor) return; // Clear the sensor_t structure memset(sensor, 0, sizeof(sensor_t)); // Populate sensor details strncpy(sensor->name, "CustomTemp", sizeof(sensor->name) - 1); sensor->version = 1; sensor->sensor_id = _sensorID; sensor->type = SENSOR_TYPE_AMBIENT_TEMPERATURE; sensor->max_value = 125.0; // Max temp in °C sensor->min_value = -40.0; // Min temp in °C sensor->resolution = 0.5; // Resolution in °C sensor->min_delay = 100000; // Min delay: 100ms (10 Hz max) } }; // Usage example CustomTempSensor tempSensor(99999, A0); void setup() { Serial.begin(9600); tempSensor.begin(); // Display sensor capabilities sensor_t sensor; tempSensor.getSensor(&sensor); Serial.print("Sensor: "); Serial.println(sensor.name); Serial.print("Range: "); Serial.print(sensor.min_value); Serial.print(" to "); Serial.print(sensor.max_value); Serial.println(" °C"); } void loop() { sensors_event_t event; if (tempSensor.getEvent(&event)) { Serial.print("Temperature: "); Serial.print(event.temperature); Serial.println(" °C"); } delay(1000); } ``` -------------------------------- ### Unified Sensor Event Structure (sensors_event_t) Example Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Demonstrates how to use the sensors_event_t structure to retrieve and access accelerometer data. This structure is compatible with any Adafruit_Sensor device. It requires the Adafruit_Sensor and Adafruit_ADXL343 libraries. The output includes acceleration in m/s^2 along with sensor metadata like ID, timestamp, and type. ```cpp #include #include // Create sensor instance with unique ID Adafruit_ADXL343 accel = Adafruit_ADXL343(12345); void setup() { Serial.begin(9600); if(!accel.begin()) { Serial.println("Sensor not detected!"); while(1); } } void loop() { // Get sensor event - works with any Adafruit_Sensor-compatible device sensors_event_t event; accel.getEvent(&event); // Access accelerometer data (m/s^2) Serial.print("X: "); Serial.print(event.acceleration.x); Serial.print(" Y: "); Serial.print(event.acceleration.y); Serial.print(" Z: "); Serial.print(event.acceleration.z); Serial.println(" m/s^2"); // Event metadata Serial.print("Sensor ID: "); Serial.println(event.sensor_id); Serial.print("Timestamp: "); Serial.println(event.timestamp); Serial.print("Type: "); Serial.println(event.type); delay(500); } ``` -------------------------------- ### Get Sensor Information (C++) Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md This function, required for all Adafruit_Sensor drivers, provides basic sensor information (name, version, ranges, etc.) and populates a `sensor_t` structure. ```c++ void getSensor(sensor_t*); ``` -------------------------------- ### Get Sensor Event (C++) Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md This function, required for all Adafruit_Sensor drivers, retrieves the latest sensor data and populates a `sensors_event_t` structure. It is called repeatedly to update the sensor data. ```c++ bool getEvent(sensors_event_t*); ``` -------------------------------- ### Query BME280 Sensor Capabilities and Information Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Initializes a BME280 sensor and retrieves its metadata using `getSensor()`. The code prints the sensor's name, version, ID, type, measurement range, and resolution. It requires `Adafruit_Sensor.h` and `Adafruit_BME280.h`. The `sensor_t` structure holds the sensor's capabilities. ```cpp #include #include Adafruit_BME280 bme; void printSensorInfo() { sensor_t sensor; bme.getSensor(&sensor); Serial.println("\n=== Sensor Information ==="); Serial.print("Name: "); Serial.println(sensor.name); Serial.print("Version: "); Serial.println(sensor.version); Serial.print("Sensor ID: "); Serial.println(sensor.sensor_id); // Determine sensor type Serial.print("Type: "); switch((sensors_type_t)sensor.type) { case SENSOR_TYPE_AMBIENT_TEMPERATURE: Serial.println("Temperature"); Serial.print("Range: "); Serial.print(sensor.min_value); Serial.print(" to "); Serial.print(sensor.max_value); Serial.println(" °C"); Serial.print("Resolution: "); Serial.print(sensor.resolution); Serial.println(" °C"); break; case SENSOR_TYPE_PRESSURE: Serial.println("Pressure"); Serial.print("Range: "); Serial.print(sensor.min_value); Serial.print(" to "); Serial.print(sensor.max_value); Serial.println(" hPa"); break; case SENSOR_TYPE_RELATIVE_HUMIDITY: Serial.println("Humidity"); Serial.print("Range: "); Serial.print(sensor.min_value); Serial.print(" to "); Serial.print(sensor.max_value); Serial.println(" %"); break; } if (sensor.min_delay > 0) { Serial.print("Max Rate: "); Serial.print(1000000.0 / sensor.min_delay); Serial.println(" Hz"); } else { Serial.println("Max Rate: Variable"); } Serial.println("=========================\n"); } void setup() { Serial.begin(9600); if (!bme.begin()) { Serial.println("BME280 not found!"); while(1); } printSensorInfo(); } void loop() { // Main sensor reading loop delay(1000); } ``` -------------------------------- ### Utility Constants - C++ Physical Conversions Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Provides standardized physical constants for sensor calculations including gravity, magnetic field, and pressure values for Earth, Moon, and Sun. Includes unit conversion functions for degrees to radians and gauss to microtesla conversions, plus altitude calculation from pressure readings. Requires Adafruit_Sensor library. ```cpp #include void setup() { Serial.begin(9600); // Gravity constants Serial.println("=== Gravity Constants ==="); Serial.print("Earth: "); Serial.print(SENSORS_GRAVITY_EARTH); Serial.println(" m/s^2"); Serial.print("Moon: "); Serial.print(SENSORS_GRAVITY_MOON); Serial.println(" m/s^2"); Serial.print("Sun: "); Serial.print(SENSORS_GRAVITY_SUN); Serial.println(" m/s^2"); // Magnetic field constants Serial.println("\n=== Magnetic Field ==="); Serial.print("Earth Max: "); Serial.print(SENSORS_MAGFIELD_EARTH_MAX); Serial.println(" uT"); Serial.print("Earth Min: "); Serial.print(SENSORS_MAGFIELD_EARTH_MIN); Serial.println(" uT"); // Pressure constants Serial.println("\n=== Pressure ==="); Serial.print("Sea Level: "); Serial.print(SENSORS_PRESSURE_SEALEVELHPA); Serial.println(" hPa"); // Unit conversion examples Serial.println("\n=== Unit Conversions ==="); float degreesPerSec = 90.0; float radiansPerSec = degreesPerSec * SENSORS_DPS_TO_RADS; Serial.print(degreesPerSec); Serial.print(" deg/s = "); Serial.print(radiansPerSec); Serial.println(" rad/s"); float gauss = 0.5; float microTesla = gauss * SENSORS_GAUSS_TO_MICROTESLA; Serial.print(gauss); Serial.print(" G = "); Serial.print(microTesla); Serial.println(" uT"); } void loop() { // Calculate altitude from pressure float seaLevelPressure = SENSORS_PRESSURE_SEALEVELHPA; float currentPressure = 950.0; // Example reading float altitude = 44330.0 * (1.0 - pow(currentPressure / seaLevelPressure, 0.1903)); Serial.print("Pressure: "); Serial.print(currentPressure); Serial.print(" hPa -> Altitude: "); Serial.print(altitude); Serial.println(" m"); delay(2000); } ``` -------------------------------- ### Query Sensor Metadata via Unified Driver Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md Shows how to query sensor capabilities using the unified driver interface. Retrieves sensor metadata including name, version, unique ID, value ranges, and resolution into a sensor_t struct. Displays technical specifications for the TSL2561 light sensor. ```c++ sensor_t sensor; 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(""); ``` -------------------------------- ### Read TSL2561 Light Sensor Data via Unified Driver Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md Demonstrates reading sensor data using the unified driver abstraction layer. Creates a TSL2561 light sensor instance, retrieves a sensor event, and displays light levels in lux. Handles sensor saturation by checking if event.light returns zero, indicating unreliable data. ```c++ 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"); } ``` -------------------------------- ### Retrieve and Process Light Sensor Readings Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Initializes a TSL2561 light sensor and continuously reads its illuminance. The code checks for valid readings, prints the lux value, and classifies the light level (Dark, Dim, Normal, Bright). It requires `Wire.h`, `Adafruit_Sensor.h`, and `Adafruit_TSL2561.h`. The `getEvent()` function populates a `sensors_event_t` structure. ```cpp #include #include #include Adafruit_TSL2561 lightSensor = Adafruit_TSL2561(TSL2561_ADDR_FLOAT, 54321); void setup() { Serial.begin(9600); if (!lightSensor.begin()) { Serial.println("Light sensor initialization failed!"); while(1); } // Configure sensor settings lightSensor.enableAutoRange(true); } void loop() { sensors_event_t event; // Get current sensor reading bool success = lightSensor.getEvent(&event); if (success) { // Check for valid reading if (event.light > 0) { Serial.print("Illuminance: "); Serial.print(event.light); Serial.println(" lux"); // Classify light levels if (event.light < 10) { Serial.println("Dark"); } else if (event.light < 100) { Serial.println("Dim"); } else if (event.light < 1000) { Serial.println("Normal"); } else { Serial.println("Bright"); } } else { Serial.println("Sensor saturated - too bright!"); } } else { Serial.println("Failed to read sensor"); } delay(1000); } ``` -------------------------------- ### Print Sensor Details - C++ Debug Helper Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Prints formatted sensor information to serial console for debugging and diagnostics. Uses built-in printSensorDetails() method to display comprehensive sensor specifications and real-time acceleration readings with magnitude calculations. Requires Adafruit_Sensor and Adafruit_LSM303_Accel libraries. ```cpp #include #include #include Adafruit_LSM303_Accel_Unified accel = Adafruit_LSM303_Accel_Unified(30301); void setup() { Serial.begin(9600); while (!Serial) delay(10); Serial.println("Accelerometer Diagnostic Test\n"); if (!accel.begin()) { Serial.println("Failed to find LSM303 chip"); while (1) delay(10); } // Print detailed sensor information (built-in method) accel.printSensorDetails(); Serial.println("Starting measurements...\n"); } void loop() { sensors_event_t event; accel.getEvent(&event); // Calculate magnitude float magnitude = sqrt( event.acceleration.x * event.acceleration.x + event.acceleration.y * event.acceleration.y + event.acceleration.z * event.acceleration.z ); Serial.print("X: "); Serial.print(event.acceleration.x, 2); Serial.print("\tY: "); Serial.print(event.acceleration.y, 2); Serial.print("\tZ: "); Serial.print(event.acceleration.z, 2); Serial.print("\t|A|: "); Serial.print(magnitude, 2); Serial.println(" m/s^2"); delay(250); } ``` -------------------------------- ### Process Sensor Events Based on Type Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Handles different sensor types by casting the event type to `sensors_type_t` and using a switch statement. It displays readings for common sensor types like accelerometer, light, pressure, temperature, humidity, TVOC, and CO2. This function requires the `Adafruit_Sensor.h` library. ```cpp #include // Function to handle different sensor types appropriately void processSensorEvent(sensors_event_t* event) { switch((sensors_type_t)event->type) { case SENSOR_TYPE_ACCELEROMETER: Serial.print("Acceleration X: "); Serial.print(event->acceleration.x); Serial.println(" m/s^2"); break; case SENSOR_TYPE_LIGHT: Serial.print("Light: "); Serial.print(event->light); Serial.println(" lux"); break; case SENSOR_TYPE_PRESSURE: Serial.print("Pressure: "); Serial.print(event->pressure); Serial.println(" hPa"); break; case SENSOR_TYPE_AMBIENT_TEMPERATURE: Serial.print("Temperature: "); Serial.print(event->temperature); Serial.println(" °C"); break; case SENSOR_TYPE_RELATIVE_HUMIDITY: Serial.print("Humidity: "); Serial.print(event->relative_humidity); Serial.println(" %"); break; case SENSOR_TYPE_TVOC: Serial.print("TVOC: "); Serial.print(event->tvoc); Serial.println(" ppb"); break; case SENSOR_TYPE_CO2: Serial.print("CO2: "); Serial.print(event->CO2); Serial.println(" ppm"); break; default: Serial.println("Unknown sensor type"); break; } } ``` -------------------------------- ### Define Sensor Details (C++) Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md Defines the `sensor_t` structure, which encapsulates basic information about a sensor. This structure includes fields for the sensor name, version, ID, type, and value ranges. It's used to abstract sensor-specific details. ```c++ /* 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; ``` -------------------------------- ### Enable Auto Range - C++ Dynamic Adjustment Source: https://context7.com/adafruit/adafruit_sensor/llms.txt Enables automatic range adjustment for sensors supporting multiple measurement ranges, optimizing precision based on current readings. Uses built-in enableAutoRange() method to automatically adjust sensor gain for optimal light measurement accuracy. Requires Adafruit_Sensor and Adafruit_TSL2591 libraries. ```cpp #include #include Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591); void setup() { Serial.begin(9600); if (!tsl.begin()) { Serial.println("TSL2591 not found!"); while(1); } // Enable automatic gain adjustment for optimal precision tsl.enableAutoRange(true); Serial.println("Auto-ranging enabled - sensor will adjust gain automatically"); } void loop() { sensors_event_t event; tsl.getEvent(&event); if (event.light > 0) { Serial.print("Light: "); Serial.print(event.light); Serial.println(" lux"); } else { Serial.println("No light detected or sensor saturated"); } delay(1000); } ``` -------------------------------- ### Define Sensor Event Data (C++) Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md Defines the `sensors_event_t` structure, which is used to return sensor data. It includes a timestamp, sensor ID, type, and a union to accommodate different sensor data types like acceleration, magnetic field, temperature, etc. This provides a common format for sensor events. ```c++ /* 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; ``` -------------------------------- ### Define Sensor Types (C++) Source: https://github.com/adafruit/adafruit_sensor/blob/master/README.md This C++ enum defines various sensor types supported by the Adafruit Unified Sensor Driver. These types are used to abstract sensor details and units, enabling consistent data handling across different sensor families. ```c++ /** 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; ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.