arduinoprojects

git clone https://git.tarina.org/arduinoprojects
Log | Files | Refs

Example5_WakeAndInterrupt.ino (4288B)


      1 /******************************************************************************
      2   Wake from sleep and read interrupts
      3 
      4   Marshall Taylor @ SparkFun Electronics
      5 
      6   April 4, 2017
      7 
      8   https://github.com/sparkfun/CCS811_Air_Quality_Breakout
      9   https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library
     10 
     11   This example configures the nWAKE and nINT pins.
     12   The interrupt pin is configured to pull low when the data is
     13   ready to be collected.
     14   The wake pin is configured to enable the sensor during I2C communications
     15 
     16   Hardware Connections (Breakoutboard to Arduino):
     17   3.3V to 3.3V pin
     18   GND to GND pin
     19   SDA to A4
     20   SCL to A5
     21   NOT_INT to D6
     22   NOT_WAKE to D5 (For 5V arduinos, use resistor divider)
     23     D5---
     24          |
     25          R1 = 4.7K
     26          |
     27          --------NOT_WAKE
     28          |
     29          R2 = 4.7K
     30          |
     31         GND
     32 
     33   Resources:
     34   Uses Wire.h for i2c operation
     35 
     36   Development environment specifics:
     37   Arduino IDE 1.8.1
     38 
     39   This code is released under the [MIT License](http://opensource.org/licenses/MIT).
     40 
     41   Please review the LICENSE.md file included with this example. If you have any questions
     42   or concerns with licensing, please contact techsupport@sparkfun.com.
     43 
     44   Distributed as-is; no warranty is given.
     45 ******************************************************************************/
     46 #include <Wire.h>
     47 
     48 #include <SparkFunCCS811.h> //Click here to get the library: http://librarymanager/All#SparkFun_CCS811
     49 
     50 #define CCS811_ADDR 0x5B //Default I2C Address
     51 //#define CCS811_ADDR 0x5A //Alternate I2C Address
     52 
     53 #define PIN_NOT_WAKE 5
     54 #define PIN_NOT_INT 6
     55 
     56 CCS811 myCCS811(CCS811_ADDR);
     57 
     58 //Global sensor object
     59 //---------------------------------------------------------------
     60 void setup()
     61 {
     62   //Start the serial
     63   Serial.begin(115200);
     64   Serial.println();
     65   Serial.println("...");
     66 
     67   Wire.begin();
     68 
     69   //This begins the CCS811 sensor and prints error status of .beginWithStatus()
     70   CCS811Core::CCS811_Status_e returnCode = myCCS811.beginWithStatus();
     71   Serial.print("CCS811 begin exited with: ");
     72   //Pass the error code to a function to print the results
     73   Serial.println(myCCS811.statusString(returnCode));
     74 
     75   //This sets the mode to 60 second reads, and prints returned error status.
     76   returnCode = myCCS811.setDriveMode(2);
     77   Serial.print("Mode request exited with: ");
     78   Serial.println(myCCS811.statusString(returnCode));
     79 
     80   //Configure and enable the interrupt line,
     81   //then print error status
     82   pinMode(PIN_NOT_INT, INPUT_PULLUP);
     83   returnCode = myCCS811.enableInterrupts();
     84   Serial.print("Interrupt configuation exited with: ");
     85   Serial.println(myCCS811.statusString(returnCode));
     86 
     87   //Configure the wake line
     88   pinMode(PIN_NOT_WAKE, OUTPUT);
     89   digitalWrite(PIN_NOT_WAKE, 1); //Start asleep
     90 }
     91 //---------------------------------------------------------------
     92 void loop()
     93 {
     94   //Look for interrupt request from CCS811
     95   if (digitalRead(PIN_NOT_INT) == 0)
     96   {
     97     //Wake up the CCS811 logic engine
     98     digitalWrite(PIN_NOT_WAKE, 0);
     99     //Need to wait at least 50 us
    100     delay(1);
    101     //Interrupt signal caught, so cause the CCS811 to run its algorithm
    102     myCCS811.readAlgorithmResults(); //Calling this function updates the global tVOC and CO2 variables
    103 
    104     Serial.print("CO2[");
    105     Serial.print(myCCS811.getCO2());
    106     Serial.print("] tVOC[");
    107     Serial.print(myCCS811.getTVOC());
    108     Serial.print("] millis[");
    109     Serial.print(millis());
    110     Serial.print("]");
    111     Serial.println();
    112 
    113     //Now put the CCS811's logic engine to sleep
    114     digitalWrite(PIN_NOT_WAKE, 1);
    115     //Need to be asleep for at least 20 us
    116     delay(1);
    117   }
    118   delay(1); //cycle kinda fast
    119 }
    120 
    121 //printSensorError gets, clears, then prints the errors
    122 //saved within the error register.
    123 void printSensorError()
    124 {
    125   uint8_t error = myCCS811.getErrorRegister();
    126 
    127   if (error == 0xFF) //comm error
    128   {
    129     Serial.println("Failed to get ERROR_ID register.");
    130   }
    131   else
    132   {
    133     Serial.print("Error: ");
    134     if (error & 1 << 5)
    135       Serial.print("HeaterSupply");
    136     if (error & 1 << 4)
    137       Serial.print("HeaterFault");
    138     if (error & 1 << 3)
    139       Serial.print("MaxResistance");
    140     if (error & 1 << 2)
    141       Serial.print("MeasModeInvalid");
    142     if (error & 1 << 1)
    143       Serial.print("ReadRegInvalid");
    144     if (error & 1 << 0)
    145       Serial.print("MsgInvalid");
    146     Serial.println();
    147   }
    148 }