|
| 1 | +/*! |
| 2 | + * @file readGasConcentration.ino |
| 3 | + * @brief Obtain gas concentration corresponding to the current environment, output as concentration value |
| 4 | + * @n Experimental mode: connect sensor communication pin to the main controller and burn |
| 5 | + * @n Communication mode select, DIP switch SEL: 0: I2C, 1: UART |
| 6 | + * @n Group serial number Address in the group |
| 7 | + * @n A0 A1 DIP level 00 01 10 11 |
| 8 | + * @n 1 0x60 0x61 0x62 0x63 |
| 9 | + * @n 2 0x64 0x65 0x66 0x67 |
| 10 | + * @n 3 0x68 0x69 0x6A 0x6B |
| 11 | + * @n 4 0x6C 0x6D 0x6E 0x6F |
| 12 | + * @n 5 0x70 0x71 0x72 0x73 |
| 13 | + * @n 6 (Default address group) 0x74 0x75 0x76 0x77 (Default address) |
| 14 | + * @n 7 0x78 0x79 0x7A 0x7B |
| 15 | + * @n 8 0x7C 0x7D 0x7E 0x7F |
| 16 | + * @n i2c address select, default to 0x77, A1 and A0 are grouped into 4 I2C addresses. |
| 17 | + * @n | A0 | A1 | |
| 18 | + * @n | 0 | 0 | 0x74 |
| 19 | + * @n | 0 | 1 | 0x75 |
| 20 | + * @n | 1 | 0 | 0x76 |
| 21 | + * @n | 1 | 1 | 0x77 default i2c address |
| 22 | + * @n Experimental phenomenon: view the gas concentration corresponding to the current environment through serial port printing |
| 23 | + * @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com) |
| 24 | + * @license The MIT License (MIT) |
| 25 | + * @author PengKaixing(kaixing.peng@dfrobot.com) |
| 26 | + * @version V1.0 |
| 27 | + * @date 2021-03-28 |
| 28 | + * @url https://github.com/DFRobot/DFRobot_MultiGasSensor |
| 29 | + */ |
| 30 | +#include "DFRobot_MultiGasSensor.h" |
| 31 | +#include <Arduino.h> |
| 32 | + |
| 33 | +//Turn on by default, using I2C communication at the time, switch to serial port communication after turning off |
| 34 | +//#define I2C_COMMUNICATION |
| 35 | + |
| 36 | +//#ifdef I2C_COMMUNICATION |
| 37 | +//#define I2C_ADDRESS 0x77 |
| 38 | + DFRobot_GAS_I2C nh3(&Wire,0x7A); |
| 39 | + DFRobot_GAS_I2C co(&Wire,0x78); |
| 40 | + DFRobot_GAS_I2C no2(&Wire,0x7B); |
| 41 | + |
| 42 | +void setup() { |
| 43 | + //Serial port init for viewing printing output |
| 44 | + Serial.begin(115200); |
| 45 | + |
| 46 | + //Change i2c address group |
| 47 | + while(gas.changeI2cAddrGroup(7)==0) |
| 48 | + { |
| 49 | + Serial.println("IIC addr change fail!"); |
| 50 | + delay(1000); |
| 51 | + } |
| 52 | + Serial.println("IIC addr change success!"); |
| 53 | +} |
| 54 | + |
| 55 | + //Sensor init, used to init serial port or I2C, depending on the communication mode currently used |
| 56 | + while(!nh3.begin()) |
| 57 | + { |
| 58 | + Serial.println("No Devices NH3 !"); |
| 59 | + delay(1000); |
| 60 | + } |
| 61 | + //Mode of obtaining data: the main controller needs to request the sensor for data |
| 62 | + nh3.changeAcquireMode(nh3.PASSIVITY); |
| 63 | + delay(1000); |
| 64 | + |
| 65 | + nh3.setTempCompensation(nh3.ON); |
| 66 | + |
| 67 | + Serial.println("The device nh3 0x7A is connected successfully!"); |
| 68 | + |
| 69 | + while(!co.begin()) |
| 70 | + { |
| 71 | + Serial.println("No Devices CO !"); |
| 72 | + delay(1000); |
| 73 | + } |
| 74 | + |
| 75 | + co.changeAcquireMode(co.PASSIVITY); |
| 76 | + delay(1000); |
| 77 | + |
| 78 | + co.setTempCompensation(co.ON); |
| 79 | + |
| 80 | + Serial.println("The device CO 0x78 is connected successfully!"); |
| 81 | + |
| 82 | + while(!no2.begin()) |
| 83 | + { |
| 84 | + Serial.println("No Devices NO2 !"); |
| 85 | + delay(1000); |
| 86 | + } |
| 87 | + |
| 88 | + no2.changeAcquireMode(no2.PASSIVITY); |
| 89 | + delay(1000); |
| 90 | + |
| 91 | + no2.setTempCompensation(no2.ON); |
| 92 | + |
| 93 | + Serial.println("The device CO 0x7B is connected successfully!"); |
| 94 | +} |
| 95 | + |
| 96 | +void loop() { |
| 97 | + String gastypeNH3 = nh3.queryGasType(); |
| 98 | + /** |
| 99 | + *Fill in the parameter readGasConcentration() with the type of gas to be obtained and print |
| 100 | + *The current gas concentration |
| 101 | + *Print with 1s delay each time |
| 102 | + */ |
| 103 | + Serial.print("Ambient "); |
| 104 | + Serial.print(gastypeNH3); |
| 105 | + Serial.print(" concentration is: "); |
| 106 | + Serial.print(nh3.readGasConcentrationPPM()); |
| 107 | + if (gastypeNH3 == "O2") |
| 108 | + Serial.println(" %vol"); |
| 109 | + else |
| 110 | + Serial.println(" PPM"); |
| 111 | + Serial.println(); |
| 112 | + delay(1000); |
| 113 | + |
| 114 | + String gastypeCO = co.queryGasType(); |
| 115 | + /** |
| 116 | + *Fill in the parameter readGasConcentration() with the type of gas to be obtained and print |
| 117 | + *The current gas concentration |
| 118 | + *Print with 1s delay each time |
| 119 | + */ |
| 120 | + Serial.print("Ambient "); |
| 121 | + Serial.print(gastypeCO); |
| 122 | + Serial.print(" concentration is: "); |
| 123 | + Serial.print(co.readGasConcentrationPPM()); |
| 124 | + if (gastypeCO == "O2") |
| 125 | + Serial.println(" %vol"); |
| 126 | + else |
| 127 | + Serial.println(" PPM"); |
| 128 | + Serial.println(); |
| 129 | + delay(1000); |
| 130 | + |
| 131 | + String gastypeNO2 = no2.queryGasType(); |
| 132 | + /** |
| 133 | + *Fill in the parameter readGasConcentration() with the type of gas to be obtained and print |
| 134 | + *The current gas concentration |
| 135 | + *Print with 1s delay each time |
| 136 | + */ |
| 137 | + Serial.print("Ambient "); |
| 138 | + Serial.print(gastypeNO2); |
| 139 | + Serial.print(" concentration is: "); |
| 140 | + Serial.print(no2.readGasConcentrationPPM()); |
| 141 | + if (gastypeNO2 == "O2") |
| 142 | + Serial.println(" %vol"); |
| 143 | + else |
| 144 | + Serial.println(" PPM"); |
| 145 | + Serial.println(); |
| 146 | + delay(1000); |
| 147 | +} |
0 commit comments