-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrove_MultichannelGasSensor.h
More file actions
94 lines (77 loc) · 3.8 KB
/
Grove_MultichannelGasSensor.h
File metadata and controls
94 lines (77 loc) · 3.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
/******************************************************************************
* Arduino-Grove-MultichannelGasSensor-driver *
* -------------------- *
* Arduino driver for Grove Multichannel Gas sensor (CO, NO2, C2H5OH, H2, NH3 *
* CH4, C3H8 and C4H10) *
* Author: Olivier Staquet *
* Last version available on *
* https://github.com/ostaquet/Arduino-Grove-MultichannelGasSensor-driver/ *
******************************************************************************/
#ifndef _GROVE_MULTICHANNELGASSENSOR_H_
#define _GROVE_MULTICHANNELGASSENSOR_H_
#include <Arduino.h>
#include <Wire.h>
// Default parameters
#define MICS6814_DEFAULT_I2C_ADDR 0x04 // I2C slave address to use if not set
#define MICS6814_DEFAULT_STABLE_CYCLE 20 // Number of cycles with low deviation to consider
// the sample as stable and reliable
// I2C commands
#define MICS6814_CMD_READ_ADC_CH0 0x01
#define MICS6814_CMD_READ_ADC_CH1 0x02
#define MICS6814_CMD_READ_ADC_CH2 0x03
#define MICS6814_CMD_CHANGE_I2C_ADDR 0x05
#define MICS6814_CMD_READ_EEPROM 0x06
#define MICS6814_CMD_SET_R0 0x07
#define MICS6814_CMD_CONTROL_LED 0x0A
#define MICS6814_CMD_CONTROL_PWR 0x0B
// EEPROM addresses (configuration stored on the Grove Multichannel Gas Sensor)
#define MICS6814_EEPROM_ADDR_SET 0x00
#define MICS6814_EEPROM_ADDR_FACTORY_ADC_CH0 0x02 // Factory setting for R0
#define MICS6814_EEPROM_ADDR_FACTORY_ADC_CH1 0x04 // Factory setting for R0
#define MICS6814_EEPROM_ADDR_FACTORY_ADC_CH2 0x06 // Factory setting for R0
#define MICS6814_EEPROM_ADDR_USER_ADC_CH0 0x08 // User setting for R0 (after calibration)
#define MICS6814_EEPROM_ADDR_USER_ADC_CH1 0x0A // User setting for R0 (after calibration)
#define MICS6814_EEPROM_ADDR_USER_ADC_CH2 0x0C // User setting for R0 (after calibration)
#define MICS6814_EEPROM_ADDR_I2C_ADDRESS 0x14 // I2C address stored on the system
enum GasType {CO, NO2, NH3, C3H8, C4H10, CH4, H2, C2H5OH};
class MiCS6814Class {
public:
MiCS6814Class(TwoWire& wire);
virtual ~MiCS6814Class();
// Start the driver for I2C address slaveAddress
// By default: use the MICS6814_DEFAULT_I2C_ADDR (0x04)
uint8_t begin(uint8_t slaveAddress = MICS6814_DEFAULT_I2C_ADDR);
void end();
// Obtain the firmware version of the Grove Multichannel Gas Sensor
// Return 0 if unknown, 1 = v1 and 2 = v2
uint8_t getVersion();
// Output the configuration on Serial
void displayConfig();
// Change the I2C slave address to the newSlaveAddress
void changeSlaveAddress(uint8_t newSlaveAddress);
// Sample the data from the sensor
void sample();
// Get the gas concentration for a specific gasType
// (data coming from the last call of sample())
float get(GasType gasType);
// Calibrate the sensor
void warmup();
void calibrate();
private:
// Internal helpers
uint16_t readOnCommand(uint8_t command);
uint16_t readOnCommand(uint8_t command, uint8_t parameter);
uint8_t command(uint8_t command, uint8_t parameter);
void readRsValues();
uint8_t ledOn();
uint8_t ledOff();
uint8_t heaterOn();
uint8_t heaterOff();
// Internal variables
TwoWire* _wire;
uint8_t _slaveAddress;
uint8_t r0ByChannel[3] = {0, 0, 0};
uint8_t rSByChannel[3] = {0, 0, 0};
};
extern MiCS6814Class MiCS6814;
#endif // _GROVE_MULTICHANNELGASSENSOR_H_