Environmental Sensor FeatherWing Sample
Introduction
The sensor sample sets up the sensors available on the Environmental Sensor FeatherWing v1 and reads values from the sensors in a loop.
Project Configuration
In order to use the sensors on the FeatherWing, the following directives are enabled in the prj.conf file:
CONFIG_I2C=y
CONFIG_I2C_NRFX=y
CONFIG_I2C_2=y
CONFIG_I2C_2_NRF_TWIM=y
CONFIG_SENSOR=y
These include and enable the I2C driver and the sensor library.
Additionally, the sample configures the following directives in the prj.conf:
CONFIG_SENSOR=y
CONFIG_BME280=y
CONFIG_CCS811=y
CONFIG_CCS811_GPIO_WAKEUP=y
CONFIG_CCS811_GPIO_DEV_NAME="GPIO_0"
CONFIG_CCS811_GPIO_WAKEUP_PIN_NUM=30
CONFIG_SI7060=y
CONFIG_OPT3001=y
These enable the Zephyr sensor driver and the use of the individual sensor drivers.
Code Explanation
The sample initializes each of the sensors by getting the device bindings from the Icarus device tree (they are added to the device tree by the overlay file):
struct device* bme280 = device_get_binding("BME280");
struct device* ccs881 = device_get_binding("CCS811");
struct device* opt3001 = device_get_binding("OPT3001");
struct device* si7060 = device_get_binding("SI7060");
Then it reads each sensor's values in a loop using the Zephyr sensor library and prints them.
Reading Sensor Values
The following code shows how the measured values are retrieved from the sensors:
BME280
sensor_sample_fetch(bme280);
struct sensor_value temp;
sensor_channel_get(bme280, SENSOR_CHAN_AMBIENT_TEMP, &temp);
struct sensor_value press;
sensor_channel_get(bme280, SENSOR_CHAN_PRESS, &press);
struct sensor_value humidity;
sensor_channel_get(bme280, SENSOR_CHAN_HUMIDITY, &humidity);
CCS811
sensor_sample_fetch(ccs881);
struct sensor_value co2;
sensor_channel_get(ccs881, SENSOR_CHAN_CO2, &co2);
struct sensor_value voc;
sensor_channel_get(ccs881, SENSOR_CHAN_VOC, &voc);
OPT3001
sensor_sample_fetch(opt3001);
struct sensor_value lux;
sensor_channel_get(opt3001, SENSOR_CHAN_LIGHT, &lux);
SI7060
sensor_sample_fetch(si7060);
struct sensor_value temp;
sensor_channel_get(si7060, SENSOR_CHAN_AMBIENT_TEMP, &temp);
Converting Sensor Values
In order to convert the value returned from the sensors to a double, the sensor library provides a helper method:
double value_float = sensor_value_to_double(&sensor_value);