AVRly - AVR Development Resources
ccs811.h
Go to the documentation of this file.
1/******************************************************************************
2 @copyright Copyright © 2022 by Jason Duffy.
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in all
12 copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
20 SOFTWARE.
21******************************************************************************/
22
23/**
24 * @file ccs811.h
25 * @ingroup ccs811
26 * @author Jason Duffy
27 * @date 4th March 2022
28 * @brief Driver for the CCS811 gas sensor .
29 * @bug No known bugs.
30 *
31 * This file provides the basic low-level functionality for the CCS81 gas
32 * sensor / air quality sensor. It was tested with the CJMCU-811 module which
33 * provides pin breakout to THT for breadboard prototyping, and some peripheral
34 * circuitry. The CCS811 eatures an on-board MCU to interface with, greatly
35 * reducing the load on the host MCU.
36 *
37 * When the sensor is new, a burn-in period of 48hr is necessary for the
38 * resistive elements to level out and make readings more accurate. The CCS81
39 * controls the burn-in period, allowing eCO2 and eTVOC readings to be used
40 * from first power-on after 60 minutes of operation in modes 1-3.
41 *
42 * After early-life (Burn-In) use the conditioning period is the time required
43 * to achieve good sensor stability before measuring VOCs after a long idle
44 * period. After starting the application and calling init_ccs811(), run the
45 * sensor for 20 minutes before accurate readings are generated. The
46 * conditioning period must also be observed before writing to the BASELINE
47 * register.
48 *
49 * This driver was written using the datasheet for the ams CCS81 gas sensor,
50 * which can be found at the link below.
51 * @see
52 * https://pdf1.alldatasheet.com/datasheet-pdf/view/1047395/AMSCO/CCS811.html
53 */
54
55
56#ifndef CCS_EIGHT_ONE_ONE_DOT_H
57#define CCS_EIGHT_ONE_ONE_DOT_H
58
59#include <stdbool.h>
60#include <stdint.h>
61
62/**
63 * Drive modes 0 to 3. Mode 4 has been omitted as it does not appear to be
64 * particularly useful (only RAW_DATA may be used in mode 4).
65 */
66typedef enum
67{
68 drive_mode_idle = 0U,
69 drive_mode_1sec = 1U,
70 drive_mode_10sec = 2U,
71 drive_mode_60sec = 3U,
73
74
75/**
76 * Config object, to be instantiated and values assigned to members before
77 * passing the object address into the function init_ccs811().
78 * If wake_pin_enabled is set to true, WAKE must be connected to a GPIO pin and
79 * defined in pin_defines.h. Otherwise, tie the WAKE pin on the CCS811 to
80 * ground. If interrupt_dataready is set to true, INT pin should be connected
81 * to a GPIO and defined in pindefines.h. That pin will be driven low when new
82 * data is ready. If both interrupt_dataready AND interrupt_threshold are set
83 * to true, the INT pin is driven low when ALG_RESULT_DATA crosses one of the
84 * thresholds set in the THRESHOLDS register.
85 */
86typedef struct
87{
88 ccs811_drive_mode_t drive_mode; // Sets mode in which readings will be taken.
89 bool wake_pin_enabled; // true = enabled, false = disabled.
90 bool interrupt_dataready; // true = enabled, false = disabled.
91 bool interrupt_threshold; // true = enabled, false = disabled.
93
94
95/**
96 * Initialisation routine (run once at startup).
97 * This function is to be called before any other functions in this file can be
98 * called. Instantiate the ccs811_config_t object first, then pass it's address
99 * into init_ccs811() to make config data available at file scope.
100 * @param p_config is a pointer to the ccs811_config_t object.
101 * @return An error code is returned as an unsigned 8 bit integer.
102 */
103uint8_t init_ccs811(ccs811_config_t *p_config);
104
105
106/**
107 * Read eCO2 (equivalent carbon dioxide) level from sensor. Once the data_ready
108 * flag is set, this function can be called.
109 * @return eCO2 level in ppm is returned, ranging from (400 - 32768ppm).
110 */
111uint16_t ccs811_get_eco2_level(void);
112
113
114/**
115 * Read eTVOC (equivalent total volatile organic compounds) level from sensor.
116 * Once the data_ready flag is set, this function can be called.
117 * @return eTVOC level in ppb is returned, ranging from (0 - 32768ppb).
118 */
119uint16_t ccs811_get_etvoc_level(void);
120
121
122/**
123 * Perform a read operation on all 8 bytes of ALG_RESULT_REGISTER. The data is
124 * stored in the read_buffer[] array, and can be extracted using the register
125 * size definitions in the source file.
126 */
128
129
130/**
131 * Read the status register and check if the data ready flag is set. Use this
132 * function to poll the data ready flag and call the getter functions above
133 * only when a new value is ready.
134 * @return A boolean value of true is returned if flag is set, otherwise false
135 * is returned.
136 */
137bool ccs811_data_ready_check(void);
138
139
140/**
141 * (Optional) Write environmental data from another sensor to the CCS811. The
142 * sensor has the capability to use humidity and temperature data in a
143 * compensation formula to imporve the accuracy of it's readings.
144 * @param uint8_t humidity is the Relative Humidity % passed in as an integer.
145 * @param uint8_t temp is the temperature in degrees Celcius.
146 */
147void ccs811_update_env_data(uint8_t humidity, uint8_t temp);
148
149
150/**
151 * For error handling/logging. If the error flag is set on the status register,
152 * call this function to turn the error code into a string message.
153 * @param char* pointer to the first element of the string is returned.
154 */
155char* ccs811_error_to_string(void);
156
157
158
159#endif // CCS_EIGHT_ONE_ONE_DOT_H
160
161
162/*** end of file ***/
void ccs811_update_env_data(uint8_t humidity, uint8_t temp)
(Optional) Write environmental data from another sensor to the CCS811.
Definition: ccs811.c:216
uint16_t ccs811_get_etvoc_level(void)
Read eTVOC (equivalent total volatile organic compounds) level from sensor.
Definition: ccs811.c:189
uint16_t ccs811_get_eco2_level(void)
Read eCO2 (equivalent carbon dioxide) level from sensor.
Definition: ccs811.c:182
char * ccs811_error_to_string(void)
For error handling/logging.
Definition: ccs811.c:231
ccs811_drive_mode_t
Drive modes 0 to 3.
Definition: ccs811.h:67
uint8_t init_ccs811(ccs811_config_t *p_config)
Initialisation routine (run once at startup).
Definition: ccs811.c:133
void ccs811_get_alg_result_data(void)
Perform a read operation on all 8 bytes of ALG_RESULT_REGISTER.
Definition: ccs811.c:211
bool ccs811_data_ready_check(void)
Read the status register and check if the data ready flag is set.
Definition: ccs811.c:200
Config object, to be instantiated and values assigned to members before passing the object address in...
Definition: ccs811.h:87