A component library for MiCS-6814 Multichannel Gas Sensor (seeed)

Dependents:   MiCS6814_GasSensor_Hello

Committer:
edamame22
Date:
Thu May 25 03:16:47 2017 +0000
Revision:
4:a15eee9bb0e6
Parent:
3:93b7f86b72e1
added pin name definition for I2C in header file.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
edamame22 0:e0794665caf1 1 /**
edamame22 0:e0794665caf1 2 ******************************************************************************
edamame22 1:5caeba593de4 3 * @file MiCS6814_GasSensor.h
edamame22 0:e0794665caf1 4 * @author Boting Ren
edamame22 0:e0794665caf1 5 * @version V1.0.1
edamame22 0:e0794665caf1 6 * @date 22 May 2017
edamame22 0:e0794665caf1 7 * @brief This file contains the class of a Multichannel Gas Sensor library with I2C interface
edamame22 0:e0794665caf1 8 ******************************************************************************
edamame22 0:e0794665caf1 9 * @attention
edamame22 0:e0794665caf1 10 *
edamame22 0:e0794665caf1 11 * Permission is hereby granted, free of charge, to any person obtaining a copy
edamame22 0:e0794665caf1 12 * of this software and associated documentation files (the "Software"), to deal
edamame22 0:e0794665caf1 13 * in the Software without restriction, including without limitation the rights
edamame22 0:e0794665caf1 14 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
edamame22 0:e0794665caf1 15 * copies of the Software, and to permit persons to whom the Software is
edamame22 0:e0794665caf1 16 * furnished to do so, subject to the following conditions:
edamame22 0:e0794665caf1 17 *
edamame22 0:e0794665caf1 18 * The above copyright notice and this permission notice shall be included in
edamame22 0:e0794665caf1 19 * all copies or substantial portions of the Software.
edamame22 0:e0794665caf1 20 *
edamame22 0:e0794665caf1 21 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
edamame22 0:e0794665caf1 22 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
edamame22 0:e0794665caf1 23 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
edamame22 0:e0794665caf1 24 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
edamame22 0:e0794665caf1 25 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
edamame22 0:e0794665caf1 26 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
edamame22 0:e0794665caf1 27 * THE SOFTWARE.
edamame22 1:5caeba593de4 28 */
edamame22 1:5caeba593de4 29
edamame22 1:5caeba593de4 30 /**
edamame22 1:5caeba593de4 31 * Library for "MiCS-6814 Multichannel Gas Sensor" from Switch Science
edamame22 1:5caeba593de4 32 * https://www.switch-science.com/catalog/2512/
edamame22 0:e0794665caf1 33 *
edamame22 1:5caeba593de4 34 * For more information about the Multichannel Gas Sensor:
edamame22 1:5caeba593de4 35 * http://wiki.seeed.cc/Grove-Multichannel_Gas_Sensor/
edamame22 0:e0794665caf1 36 */
edamame22 1:5caeba593de4 37
edamame22 1:5caeba593de4 38
edamame22 0:e0794665caf1 39 #ifndef MBED_MULTIGAS_SENSOR_H
edamame22 0:e0794665caf1 40 #define MBED_MULTIGAS_SENSOR_H
edamame22 0:e0794665caf1 41
edamame22 0:e0794665caf1 42 #include "mbed.h"
edamame22 0:e0794665caf1 43
edamame22 0:e0794665caf1 44 #define SLAVE_ADDRESS_MiCS6814 (0x04<<1)
edamame22 0:e0794665caf1 45
MACRUM 3:93b7f86b72e1 46 #define ADDR_IS_SET 0
edamame22 0:e0794665caf1 47 #define ADDR_FACTORY_ADC_NH3 2
edamame22 0:e0794665caf1 48 #define ADDR_FACTORY_ADC_CO 4
edamame22 0:e0794665caf1 49 #define ADDR_FACTORY_ADC_NO2 6
edamame22 0:e0794665caf1 50
edamame22 0:e0794665caf1 51 #define ADDR_USER_ADC_HN3 8
edamame22 0:e0794665caf1 52 #define ADDR_USER_ADC_CO 10
edamame22 0:e0794665caf1 53 #define ADDR_USER_ADC_NO2 12
MACRUM 3:93b7f86b72e1 54 #define ADDR_IF_CALI 14
edamame22 0:e0794665caf1 55
edamame22 0:e0794665caf1 56 #define CH_VALUE_NH3 1
edamame22 0:e0794665caf1 57 #define CH_VALUE_CO 2
edamame22 0:e0794665caf1 58 #define CH_VALUE_NO2 3
edamame22 0:e0794665caf1 59
edamame22 0:e0794665caf1 60 #define CMD_ADC_RES0 1 // NH3
edamame22 0:e0794665caf1 61 #define CMD_ADC_RES1 2 // CO
edamame22 0:e0794665caf1 62 #define CMD_ADC_RES2 3 // NO2
edamame22 0:e0794665caf1 63 #define CMD_ADC_RESALL 4 // ALL CHANNEL
edamame22 0:e0794665caf1 64 #define CMD_CHANGE_I2C 5 // CHANGE I2C
edamame22 0:e0794665caf1 65 #define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
edamame22 0:e0794665caf1 66 #define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
edamame22 0:e0794665caf1 67 #define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
edamame22 0:e0794665caf1 68 #define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
edamame22 0:e0794665caf1 69 #define CMD_CONTROL_LED 10
edamame22 0:e0794665caf1 70 #define CMD_CONTROL_PWR 11
edamame22 0:e0794665caf1 71
edamame22 1:5caeba593de4 72 #define HEATER_ON 1
edamame22 1:5caeba593de4 73 #define HEATER_OFF 0
edamame22 1:5caeba593de4 74
edamame22 4:a15eee9bb0e6 75 #if !defined(I2C_SDA) && defined(SDA)
edamame22 4:a15eee9bb0e6 76 #define I2C_SDA SDA
edamame22 4:a15eee9bb0e6 77 #endif
edamame22 4:a15eee9bb0e6 78
edamame22 4:a15eee9bb0e6 79 #if !defined(I2C_SCL) && defined(SCL)
edamame22 4:a15eee9bb0e6 80 #define I2C_SCL SCL
edamame22 4:a15eee9bb0e6 81 #endif
edamame22 4:a15eee9bb0e6 82
edamame22 0:e0794665caf1 83 #ifdef _DEBUG
edamame22 0:e0794665caf1 84 extern Serial pc;
edamame22 0:e0794665caf1 85 #define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
edamame22 0:e0794665caf1 86 #else
edamame22 0:e0794665caf1 87 #define DEBUG_PRINT(...)
edamame22 0:e0794665caf1 88 #endif
edamame22 0:e0794665caf1 89
edamame22 0:e0794665caf1 90 enum GAS_TYPE{NH3, CO, NO2, C3H8, C4H10, CH4, H2, C2H5OH};
edamame22 0:e0794665caf1 91 enum READ_ERROR_TYPE{READ_OK, NULL_RETURN_BUFFER_PTR, WRITE_LENGTH_ERROR, READ_LENGTH_ERROR, CHECKSUM_ERROR};
edamame22 0:e0794665caf1 92
edamame22 1:5caeba593de4 93 /** Interface for controlling MiCS-6814 Multichannel Gas Sensor
edamame22 1:5caeba593de4 94 *
edamame22 1:5caeba593de4 95 * @code
edamame22 1:5caeba593de4 96 * #include "mbed.h"
edamame22 1:5caeba593de4 97 * #include "MiCS6814_GasSensor.h"
edamame22 1:5caeba593de4 98 *
edamame22 1:5caeba593de4 99 * Serial pc(USBTX, USBRX);
edamame22 1:5caeba593de4 100 *
edamame22 1:5caeba593de4 101 * #if defined(TARGET_LPC1768)
MACRUM 3:93b7f86b72e1 102 * MiCS6814_GasSensor sensor(p28, p27);
edamame22 1:5caeba593de4 103 * #else
MACRUM 3:93b7f86b72e1 104 * MiCS6814_GasSensor sensor(I2C_SDA, I2C_SCL);
edamame22 1:5caeba593de4 105 * #endif
edamame22 1:5caeba593de4 106 *
edamame22 1:5caeba593de4 107 * int main() {
edamame22 1:5caeba593de4 108 *
edamame22 1:5caeba593de4 109 * while(1) {
MACRUM 3:93b7f86b72e1 110 * pc.printf("NH3: %.2f ppm, CO: %.2f ppm, NO2: %.2f ppm, C3H8: %.2f ppm \r\n", sensor.getGas(NH3), sensor.getGas(CO), sensor.getGas(NO2), sensor.getGas(C3H8));
MACRUM 3:93b7f86b72e1 111 * pc.printf("C4H10: %.2f ppm, CH4: %.2f ppm, H2: %.2f ppm, C2H5OH: %.2f ppm \r\n", sensor.getGas(C4H10), sensor.getGas(CH4), sensor.getGas(H2), sensor.getGas(C2H5OH));
edamame22 1:5caeba593de4 112 * wait(1);
edamame22 1:5caeba593de4 113 * }
edamame22 1:5caeba593de4 114 * }
edamame22 1:5caeba593de4 115 *
edamame22 1:5caeba593de4 116 * @endcode
edamame22 1:5caeba593de4 117 */
edamame22 1:5caeba593de4 118
edamame22 0:e0794665caf1 119 /**
edamame22 0:e0794665caf1 120 * @class MiCS6814_GasSensor
edamame22 1:5caeba593de4 121 * @brief A mbed component library to measure concentration value for 8 type of gases by using MiCS6814 - Multichannel Gas Sensor (seeed)
edamame22 0:e0794665caf1 122 *
edamame22 0:e0794665caf1 123 */
edamame22 0:e0794665caf1 124
edamame22 0:e0794665caf1 125 class MiCS6814_GasSensor{
edamame22 0:e0794665caf1 126
edamame22 0:e0794665caf1 127 public:
edamame22 0:e0794665caf1 128 /** Create a MiCS6814_GasSensor instance
edamame22 0:e0794665caf1 129 * the sensor is connected to specified I2C pins with specified address
edamame22 0:e0794665caf1 130 *
edamame22 0:e0794665caf1 131 * @param[in] sda I2C-bus SDA pin
edamame22 0:e0794665caf1 132 * @param[in] scl I2C-bus SCL pin
edamame22 0:e0794665caf1 133 * @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
edamame22 0:e0794665caf1 134 */
MACRUM 3:93b7f86b72e1 135 MiCS6814_GasSensor(PinName sda, PinName scl, char slave_adr = SLAVE_ADDRESS_MiCS6814);
edamame22 0:e0794665caf1 136
edamame22 0:e0794665caf1 137 /** Create a MiCS6814_GasSensor instance
edamame22 0:e0794665caf1 138 * the sensor is connected to specified I2C pins with specified address
edamame22 0:e0794665caf1 139 *
edamame22 0:e0794665caf1 140 * @param[in] i2c_obj I2C object (instance)
edamame22 0:e0794665caf1 141 * @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
edamame22 0:e0794665caf1 142 */
edamame22 0:e0794665caf1 143 MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr = SLAVE_ADDRESS_MiCS6814);
edamame22 0:e0794665caf1 144
edamame22 0:e0794665caf1 145 /** Destructor of MiCS6814_GasSensor
edamame22 0:e0794665caf1 146 */
edamame22 0:e0794665caf1 147 virtual ~MiCS6814_GasSensor();
edamame22 0:e0794665caf1 148
edamame22 0:e0794665caf1 149 /** Initialize MiCS6814_GasSensor
MACRUM 3:93b7f86b72e1 150 * Read firmware version from sensor and power on heater
edamame22 0:e0794665caf1 151 */
edamame22 0:e0794665caf1 152 void initialize(void);
edamame22 0:e0794665caf1 153
edamame22 0:e0794665caf1 154 /** Return a specific measured value, unit: ppm
edamame22 0:e0794665caf1 155 *
edamame22 0:e0794665caf1 156 * @param[in] gas_type one of gas type defined at enum GAS_TYPE
edamame22 0:e0794665caf1 157 * @return the measured concentration of specific gas type (ppm)
edamame22 0:e0794665caf1 158 */
MACRUM 3:93b7f86b72e1 159 float getGas(const enum GAS_TYPE gas_type);
edamame22 0:e0794665caf1 160
edamame22 0:e0794665caf1 161 private:
edamame22 0:e0794665caf1 162
edamame22 0:e0794665caf1 163 I2C *_i2c_p;
edamame22 0:e0794665caf1 164 I2C &_i2c;
edamame22 1:5caeba593de4 165 char _address; //I2C address of this MCU
MACRUM 3:93b7f86b72e1 166
edamame22 0:e0794665caf1 167 /** Check firmware version of sensor
edamame22 1:5caeba593de4 168 * only support version 2
edamame22 0:e0794665caf1 169 */
edamame22 0:e0794665caf1 170 void CheckFirmwareVersion(void);
edamame22 0:e0794665caf1 171
edamame22 0:e0794665caf1 172 /** Turn On/Off sensor heater
edamame22 0:e0794665caf1 173 *
edamame22 1:5caeba593de4 174 * @param[in] Switch_On 1: Turn on, 0: Turn off
edamame22 0:e0794665caf1 175 */
edamame22 1:5caeba593de4 176 void HeaterPower(const unsigned char Switch_On);
edamame22 1:5caeba593de4 177
edamame22 0:e0794665caf1 178 /** Read 16-bit data from sensor
edamame22 0:e0794665caf1 179 *
edamame22 0:e0794665caf1 180 * @param[in] reg_addr register adderss
edamame22 0:e0794665caf1 181 * @param[in] data_4reg data for the register, it will be Zero in case no input data for above register.
edamame22 0:e0794665caf1 182 * @param[in] write_length length for I2C writing, should be 1 or 2
edamame22 0:e0794665caf1 183 * @param[in] read_length length for I2C reading, should be 2 or 4
edamame22 0:e0794665caf1 184 * @param[out] pointer for read result buffer 16-bit
edamame22 0:e0794665caf1 185 * @return status
edamame22 0:e0794665caf1 186 */
edamame22 0:e0794665caf1 187 READ_ERROR_TYPE read_Multibytes(unsigned char reg_addr, unsigned char data_4reg ,unsigned char write_length, unsigned char read_length, uint16_t *readBuf);
edamame22 0:e0794665caf1 188
edamame22 0:e0794665caf1 189 /** Read A0_[x] of sensor
edamame22 0:e0794665caf1 190 *
edamame22 0:e0794665caf1 191 * @param[in] index {0,1,2}
edamame22 1:5caeba593de4 192 * @return return A0_[x]
edamame22 0:e0794665caf1 193 */
MACRUM 3:93b7f86b72e1 194 uint16_t readR0_A0(unsigned char index);
edamame22 0:e0794665caf1 195
edamame22 0:e0794665caf1 196 /** Read An_[x] of sensor
edamame22 0:e0794665caf1 197 *
edamame22 0:e0794665caf1 198 * @param[in] index {0,1,2}
edamame22 1:5caeba593de4 199 * @return return An_[x]
edamame22 0:e0794665caf1 200 */
MACRUM 3:93b7f86b72e1 201 uint16_t readRs_An(unsigned char index);
edamame22 0:e0794665caf1 202 };
edamame22 0:e0794665caf1 203
edamame22 0:e0794665caf1 204 #endif // MBED_MULTIGAS_SENSOR_H