Skip to content

Commit

Permalink
Library
Browse files Browse the repository at this point in the history
  • Loading branch information
TaktischerSpeck authored May 14, 2019
1 parent 7b4f6f2 commit cf0da3a
Show file tree
Hide file tree
Showing 13 changed files with 3,358 additions and 0 deletions.
699 changes: 699 additions & 0 deletions MutichannelGasSensor.cpp

Large diffs are not rendered by default.

164 changes: 164 additions & 0 deletions MutichannelGasSensor.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
/*
MutichannelGasSensor.h
2015 Copyright (c) Seeed Technology Inc. All right reserved.
Author: Jacky Zhang
2015-3-17
http://www.seeed.cc/
modi by Jack, 2015-8
V2 by Loovee
2016-11-11
The MIT License (MIT)
Copyright (c) 2015 Seeed Technology Inc.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#ifndef __MUTICHANNELGASSENSOR_H__
#define __MUTICHANNELGASSENSOR_H__

#if defined (ARDUINO_SAMD_ZERO)
#define _SERIAL SerialUSB
#elif defined(ARDUINO_ARCH_SEEED_STM32F4)
#define _SERIAL SerialUSB
#elif defined (ARDUINO_ARCH_SAMD)
#define _SERIAL Serial
#elif defined (ARDUINO_ARCH_AVR)
#define _SERIAL Serial
#else
#error "Architecture not matched"
#endif


#define DEFAULT_I2C_ADDR 0x04

#define ADDR_IS_SET 0 // if this is the first time to run, if 1126, set
#define ADDR_FACTORY_ADC_NH3 2
#define ADDR_FACTORY_ADC_CO 4
#define ADDR_FACTORY_ADC_NO2 6

#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 CH_VALUE_NH3 1
#define CH_VALUE_CO 2
#define CH_VALUE_NO2 3

#define CMD_ADC_RES0 1 // NH3
#define CMD_ADC_RES1 2 // CO
#define CMD_ADC_RES2 3 // NO2
#define CMD_ADC_RESALL 4 // ALL CHANNEL
#define CMD_CHANGE_I2C 5 // CHANGE I2C
#define CMD_READ_EEPROM 6 // READ EEPROM VALUE, RETURN UNSIGNED INT
#define CMD_SET_R0_ADC 7 // SET R0 ADC VALUE
#define CMD_GET_R0_ADC 8 // GET R0 ADC VALUE
#define CMD_GET_R0_ADC_FACTORY 9 // GET FACTORY R0 ADC VALUE
#define CMD_CONTROL_LED 10
#define CMD_CONTROL_PWR 11

enum{CO, NO2, NH3, C3H8, C4H10, CH4, H2, C2H5OH};

class MutichannelGasSensor{

private:

int __version;
unsigned char dta_test[20];

unsigned int readChAdcValue(int ch);
unsigned int adcValueR0_NH3_Buf;
unsigned int adcValueR0_CO_Buf;
unsigned int adcValueR0_NO2_Buf;

public:

uint8_t i2cAddress; //I2C address of this MCU
uint16_t res0[3]; //sensors res0
uint16_t res[3]; //sensors res
bool r0_inited;

MutichannelGasSensor();
inline unsigned int get_addr_dta(unsigned char addr_reg);
inline unsigned int get_addr_dta(unsigned char addr_reg, unsigned char __dta);
inline void write_i2c(unsigned char addr, unsigned char *dta, unsigned char dta_len);

void sendI2C(unsigned char dta);
int16_t readData(uint8_t cmd);
int16_t readR0(void);
int16_t readR(void);
float calcGas(int gas);

public:

void begin(int address);
void begin();
void changeI2cAddr(uint8_t newAddr);
void powerOn(void);
void powerOff(void);
void doCalibrate(void);

//get gas concentration, unit: ppm
float measure_CO(){return calcGas(CO);}
float measure_NO2(){return calcGas(NO2);}
float measure_NH3(){return calcGas(NH3);}
float measure_C3H8(){return calcGas(C3H8);}
float measure_C4H10(){return calcGas(C4H10);}
float measure_CH4(){return calcGas(CH4);}
float measure_H2(){return calcGas(H2);}
float measure_C2H5OH(){return calcGas(C2H5OH);}

float getR0(unsigned char ch); // 0:CH3, 1:CO, 2:NO2
float getRs(unsigned char ch); // 0:CH3, 1:CO, 2:NO2

public:

void ledOn()
{
dta_test[0] = CMD_CONTROL_LED;
dta_test[1] = 1;
write_i2c(i2cAddress, dta_test, 2);
}

void ledOff()
{
dta_test[0] = CMD_CONTROL_LED;
dta_test[1] = 0;
write_i2c(i2cAddress, dta_test, 2);
}

void display_eeprom();
void factory_setting();
void change_i2c_address(unsigned char addr);
unsigned char getVersion();
};

extern MutichannelGasSensor gas;

#endif

/*********************************************************************************************************
END FILE
*********************************************************************************************************/
20 changes: 20 additions & 0 deletions examples/GetVersion/GetVersion.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// Get firmware version of Grove Multichannel Gas Sensor
#include <Wire.h>
#include "MutichannelGasSensor.h"

#define SENSOR_ADDR 0X04 // default to 0x04

void setup()
{
Serial.begin(115200);
gas.begin(SENSOR_ADDR);

unsigned char version = gas.getVersion();
Serial.print("Version = ");
Serial.println(version);
}

void loop()
{
// nothing to do
}
25 changes: 25 additions & 0 deletions examples/I2C_Address/I2C_Address.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// change i2c address
// Loovee
// 2016-11-10

#include <Wire.h>
#include "MutichannelGasSensor.h"

#define SENSOR_ADDR_OLD 0x04 // default to 0x04
#define SENSOR_ADDR_NEW 0x19 // change i2c address to 0x19

void setup()
{
Serial.begin(115200);
gas.begin(SENSOR_ADDR_OLD); //
gas.change_i2c_address(SENSOR_ADDR_NEW);
Serial.print("I2C ADDRESS SET TO 0X");;
Serial.println(SENSOR_ADDR_NEW, HEX);
}

void loop()
{

}

// END FILE
57 changes: 57 additions & 0 deletions examples/RawData/RawData.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
// get RAW data from the sensor
// Loovee
// 2016-11-10

#include <Wire.h>
#include "MutichannelGasSensor.h"

#define SENSOR_ADDR 0X04 // default to 0x04

void setup()
{
Serial.begin(115200);
gas.begin(SENSOR_ADDR); //
}

void loop()
{
float R0_NH3, R0_CO, R0_NO2;
float Rs_NH3, Rs_CO, Rs_NO2;
float ratio_NH3, ratio_CO, ratio_NO2;

R0_NH3 = gas.getR0(0);
R0_CO = gas.getR0(1);
R0_NO2 = gas.getR0(2);

Rs_NH3 = gas.getRs(0);
Rs_CO = gas.getRs(1);
Rs_NO2 = gas.getRs(2);

ratio_NH3 = Rs_NH3/R0_NH3;
ratio_CO = Rs_CO/R0_CO;
ratio_NO2 = Rs_NH3/R0_NO2;

Serial.println("R0:");
Serial.print(R0_NH3);
Serial.print('\t');
Serial.print(R0_CO);
Serial.print('\t');
Serial.println(R0_NO2);

Serial.println("Rs:");
Serial.print(Rs_NH3);
Serial.print('\t');
Serial.print(Rs_CO);
Serial.print('\t');
Serial.println(Rs_NO2);

Serial.println("ratio:");
Serial.print(ratio_NH3);
Serial.print('\t');
Serial.print(ratio_CO);
Serial.print('\t');
Serial.println(ratio_NO2);

Serial.println("------------------------");
delay(1000);
}
81 changes: 81 additions & 0 deletions examples/ReadSensorValue_Grove/ReadSensorValue_Grove.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
/*
This is a demo to test gas library
This code is running on Xadow-mainboard, and the I2C slave is Xadow-gas
There is a ATmega168PA on Xadow-gas, it get sensors output and feed back to master.
the data is raw ADC value, algorithm should be realized on master.
please feel free to write email to me if there is any question
Jacky Zhang, Embedded Software Engineer
[email protected]
17,mar,2015
*/

#include <Wire.h>
#include "MutichannelGasSensor.h"

void setup()
{
Serial.begin(115200); // start serial for output
Serial.println("power on!");
gas.begin(0x04);//the default I2C address of the slave is 0x04
gas.powerOn();
Serial.print("Firmware Version = ");
Serial.println(gas.getVersion());
}

void loop()
{
float c;

c = gas.measure_NH3();
Serial.print("The concentration of NH3 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_CO();
Serial.print("The concentration of CO is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_NO2();
Serial.print("The concentration of NO2 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_C3H8();
Serial.print("The concentration of C3H8 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_C4H10();
Serial.print("The concentration of C4H10 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_CH4();
Serial.print("The concentration of CH4 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_H2();
Serial.print("The concentration of H2 is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

c = gas.measure_C2H5OH();
Serial.print("The concentration of C2H5OH is ");
if(c>=0) Serial.print(c);
else Serial.print("invalid");
Serial.println(" ppm");

delay(1000);
Serial.println("...");
}
Loading

0 comments on commit cf0da3a

Please sign in to comment.