A component library for MiCS-6814 Multichannel Gas Sensor (seeed)
Dependents: MiCS6814_GasSensor_Hello grove_multichannel_GasSensor
Revision 3:93b7f86b72e1, committed 2017-05-24
- Comitter:
- MACRUM
- Date:
- Wed May 24 10:03:01 2017 +0000
- Parent:
- 2:8d61377f2e2a
- Child:
- 4:a15eee9bb0e6
- Commit message:
- Update docs, add wait between i2c::write and read, correct example code.
Changed in this revision
| MiCS6814_GasSensor.cpp | Show annotated file Show diff for this revision Revisions of this file |
| MiCS6814_GasSensor.h | Show annotated file Show diff for this revision Revisions of this file |
--- a/MiCS6814_GasSensor.cpp Wed May 24 13:38:46 2017 +0900
+++ b/MiCS6814_GasSensor.cpp Wed May 24 10:03:01 2017 +0000
@@ -61,9 +61,6 @@
char cmd[2], val[4]; // write buf, read buf
uint8_t checksum=0;
- if (ret_val==NULL) {
- return NULL_RETURN_BUFFER_PTR; // ret_val is a NULL pointer
- }
if (write_len>2 || write_len<1) {
return WRITE_LENGTH_ERROR; // write_len is wrong
}
@@ -77,6 +74,7 @@
cmd[1] = __dta;
}
_i2c.write(_address, cmd, write_len);
+ wait(0.1f);
_i2c.read(_address, val, read_len);
if (read_len==2) {
*ret_val = (uint16_t) (val[0]<<8 | val[1]) ; // set return value to caller
@@ -110,88 +108,81 @@
tmp[0] = CMD_CONTROL_PWR;
tmp[1] = Switch_On;
_i2c.write(_address, tmp, 2);
+ wait(0.1f);
}
-uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char _indix)
+uint16_t MiCS6814_GasSensor::readR0_A0(unsigned char index)
{
uint16_t readBuf= 0;
const unsigned char A0_table[3]= {ADDR_USER_ADC_HN3, ADDR_USER_ADC_CO, ADDR_USER_ADC_NO2};
unsigned char ret_val;
- ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[_indix], 2, 2, &readBuf);
- DEBUG_PRINT("A0_[%d]: %d, ", _indix, readBuf);
+ ret_val= read_Multibytes(CMD_READ_EEPROM, A0_table[index], 2, 2, &readBuf);
+ DEBUG_PRINT("A0_[%d]: %d, ", index, readBuf);
if (ret_val == READ_OK) {
return readBuf;
}
- // read failed, A0_[_indix]
- DEBUG_PRINT("A0_[%d] read error: %d\r\n", _indix, ret_val);
+ // read failed, A0_[index]
+ DEBUG_PRINT("A0_[%d] read error: %d\r\n", index, ret_val);
return 0;
}
-uint16_t MiCS6814_GasSensor::readRs_An(unsigned char _indix)
+uint16_t MiCS6814_GasSensor::readRs_An(unsigned char index)
{
uint16_t readBuf= 0;
const unsigned char An_table[3]= {CH_VALUE_NH3, CH_VALUE_CO, CH_VALUE_NO2};
unsigned char ret_val;
- ret_val= read_Multibytes(An_table[_indix], 0, 1, 2, &readBuf);
- DEBUG_PRINT("An_[%d]: %d, ", _indix, readBuf);
+ ret_val= read_Multibytes(An_table[index], 0, 1, 2, &readBuf);
+ DEBUG_PRINT("An_[%d]: %d, ", index, readBuf);
if (ret_val == READ_OK) {
return readBuf;
}
- // read failed, An_[_indix]
- DEBUG_PRINT("An_[%d] read error: %d\r\n", _indix, ret_val);
+ // read failed, An_[index]
+ DEBUG_PRINT("An_[%d] read error: %d\r\n", index, ret_val);
return 0;
}
-float MiCS6814_GasSensor::calcGas(const GAS_TYPE gas_type)
+float MiCS6814_GasSensor::getGas(const GAS_TYPE gas_type)
{
int A0_[3], An_[3];
const unsigned char GasType_2index[8] = {0, 1, 2, 0, 0, 1, 1, 1};
- unsigned char _index = GasType_2index[gas_type];
+ unsigned char index = GasType_2index[gas_type];
float Ratio[3]; //will be calculated. Ratio 0,1,2
// prepare necessary Ratio[x] according to gas_type
- An_[_index] = readRs_An(_index); // read An_[x]
- A0_[_index] = readR0_A0(_index); // read R0[x]
- Ratio[_index] = (float)An_[_index]/(float)A0_[_index]*(1023.0-A0_[_index])/(1023.0-An_[_index]);
- DEBUG_PRINT("Ratio[%d]: %.3f, ", _index, Ratio[_index]);
+ An_[index] = readRs_An(index); // read An_[x]
+ A0_[index] = readR0_A0(index); // read R0[x]
+ Ratio[index] = (float)An_[index]/(float)A0_[index]*(1023.0-A0_[index])/(1023.0-An_[index]);
+ DEBUG_PRINT("Ratio[%d]: %.3f, ", index, Ratio[index]);
float calcu_val = 0.0f;
// calculate concentration value of specified gas_type
switch(gas_type) {
- case CO: {
+ case CO:
calcu_val = pow(Ratio[1], -1.179f)*4.385f;
break;
- }
- case NO2: {
+ case NO2:
calcu_val = pow(Ratio[2], 1.007f)/6.855f;
break;
- }
- case NH3: {
+ case NH3:
calcu_val = pow(Ratio[0], -1.67f)/1.47f;
break;
- }
- case C3H8: {
+ case C3H8:
calcu_val = pow(Ratio[0], -2.518f)*570.164f;
break;
- }
- case C4H10: {
+ case C4H10:
calcu_val = pow(Ratio[0], -2.138f)*398.107f;
break;
- }
- case CH4: {
+ case CH4:
calcu_val = pow(Ratio[1], -4.363f)*630.957f;
break;
- }
- case H2: {
+ case H2:
calcu_val = pow(Ratio[1], -1.8f)*0.73f;
break;
- }
- case C2H5OH: {
+ case C2H5OH:
calcu_val = pow(Ratio[1], -1.552f)*1.622f;
break;
- }
default:
break;
}
--- a/MiCS6814_GasSensor.h Wed May 24 13:38:46 2017 +0900
+++ b/MiCS6814_GasSensor.h Wed May 24 10:03:01 2017 +0000
@@ -41,10 +41,9 @@
#include "mbed.h"
-//#define _DEBUG
#define SLAVE_ADDRESS_MiCS6814 (0x04<<1)
-#define ADDR_IS_SET 0 // This is read at initialization time, if 1126, set
+#define ADDR_IS_SET 0
#define ADDR_FACTORY_ADC_NH3 2
#define ADDR_FACTORY_ADC_CO 4
#define ADDR_FACTORY_ADC_NO2 6
@@ -52,9 +51,7 @@
#define ADDR_USER_ADC_HN3 8
#define ADDR_USER_ADC_CO 10
#define ADDR_USER_ADC_NO2 12
-#define ADDR_IF_CALI 14 // IF USER HAD CALI
-
-#define ADDR_I2C_ADDRESS 20
+#define ADDR_IF_CALI 14
#define CH_VALUE_NH3 1
#define CH_VALUE_CO 2
@@ -75,7 +72,6 @@
#define HEATER_ON 1
#define HEATER_OFF 0
-
#ifdef _DEBUG
extern Serial pc;
#define DEBUG_PRINT(...) pc.printf(__VA_ARGS__)
@@ -95,16 +91,16 @@
* Serial pc(USBTX, USBRX);
*
* #if defined(TARGET_LPC1768)
- * MiCS6814_GasSensor _gases(p28, p27);
+ * MiCS6814_GasSensor sensor(p28, p27);
* #else
- * MiCS6814_GasSensor _gases(I2C_SDA, I2C_SCL);
+ * MiCS6814_GasSensor sensor(I2C_SDA, I2C_SCL);
* #endif
*
* int main() {
*
* while(1) {
- * pc.printf("NH3: %.2f ppm, CO: %.2f ppm, NO2: %.2f ppm, C3H8: %.2f ppm \r\n", _gases.calcGas(NH3), _gases.calcGas(CO), _gases.calcGas(NO2), _gases.calcGas(C3H8));
- * pc.printf("C4H10: %.2f ppm, CH4: %.2f ppm, H2: %.2f ppm, C2H5OH: %.2f ppm \r\n", _gases.calcGas(C4H10), _gases.calcGas(CH4), _gases.calcGas(H2), _gases.calcGas(C2H5OH));
+ * 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));
+ * 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));
* wait(1);
* }
* }
@@ -123,19 +119,15 @@
public:
/** Create a MiCS6814_GasSensor instance
* the sensor is connected to specified I2C pins with specified address
- * 1.Create an I2C instance
- * 2.Initialize private variables.
*
* @param[in] sda I2C-bus SDA pin
* @param[in] scl I2C-bus SCL pin
* @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
*/
- MiCS6814_GasSensor(PinName sda, PinName sck, char slave_adr = SLAVE_ADDRESS_MiCS6814);
+ MiCS6814_GasSensor(PinName sda, PinName scl, char slave_adr = SLAVE_ADDRESS_MiCS6814);
/** Create a MiCS6814_GasSensor instance
* the sensor is connected to specified I2C pins with specified address
- * 1.Pass an I2C instance,
- * 2.Initialize private variables.
*
* @param[in] i2c_obj I2C object (instance)
* @param[in] slave_adr (option) I2C-bus address (default: 0x04<<1)
@@ -143,15 +135,11 @@
MiCS6814_GasSensor(I2C &i2c_obj, char slave_adr = SLAVE_ADDRESS_MiCS6814);
/** Destructor of MiCS6814_GasSensor
- * 1.Power off heater
- * 2.Release allocated heap memory.
*/
virtual ~MiCS6814_GasSensor();
/** Initialize MiCS6814_GasSensor
- * 1.Read firmware version from sensor
- * 2.Power on heater
- * 3.Clear private variables
+ * Read firmware version from sensor and power on heater
*/
void initialize(void);
@@ -160,13 +148,14 @@
* @param[in] gas_type one of gas type defined at enum GAS_TYPE
* @return the measured concentration of specific gas type (ppm)
*/
- float calcGas(const enum GAS_TYPE gas_type);
+ float getGas(const enum GAS_TYPE gas_type);
private:
I2C *_i2c_p;
I2C &_i2c;
char _address; //I2C address of this MCU
+
/** Check firmware version of sensor
* only support version 2
*/
@@ -194,14 +183,14 @@
* @param[in] index {0,1,2}
* @return return A0_[x]
*/
- uint16_t readR0_A0(unsigned char _indix);
+ uint16_t readR0_A0(unsigned char index);
/** Read An_[x] of sensor
*
* @param[in] index {0,1,2}
* @return return An_[x]
*/
- uint16_t readRs_An(unsigned char _indix);
+ uint16_t readRs_An(unsigned char index);
};
#endif // MBED_MULTIGAS_SENSOR_H
MiCS6814 MultiChannel Gas Sensor