Mengukur kadar Karbondioksida Dengan Sensor CJMCU-8118

Mengukur kadar Karbondioksida Dengan Sensor CJMCU-8118

Kode

 

// https://github.com/adafruit/Adafruit_CCS811/blob/master/examples/CCS811_test/CCS811_test.ino
// https://github.com/closedcube/ClosedCube_HDC1080_Arduino
// https://github.com/closedcube/ClosedCube_HDC1080_Arduino/blob/master/examples/hdc1080measurement/hdc1080measurement.ino

#define I2C_SDA 15
#define I2C_SCL 13
#define LED_ONBOARD 22

#include "Adafruit_CCS811.h"
#include <Arduino.h>
#include <Wire.h>
#include "ClosedCube_HDC1080.h"

Adafruit_CCS811 ccs;
ClosedCube_HDC1080 hdc1080;

void setup() {
  pinMode(LED_ONBOARD, OUTPUT);
  // put your setup code here, to run once:
  Serial.begin(9600);
  delay(1000);

  //wait for serial connection to open (only necessary on some boards)
  while (!Serial);

  Wire.begin(I2C_SDA, I2C_SCL);

  I2C_Scan() ; // just for verifying

  // check CCS811
  Serial.println("CCS811 test");
  if (!ccs.begin()) {
    Serial.println("Failed to start sensor! Please check your wiring.");
    while (1);
  } else {
    Serial.println("CCS811 ready");
  }


  hdc1080.begin(0x40);
  Serial.println("HDC1080");
  Serial.print("Manufacturer ID=0x");
  Serial.println(hdc1080.readManufacturerId(), HEX); // 0x5449 ID of Texas Instruments
  Serial.print("Device ID=0x");
  Serial.println(hdc1080.readDeviceId(), HEX); // 0x1050 ID of the device

  hdc1080.setResolution(HDC1080_RESOLUTION_11BIT, HDC1080_RESOLUTION_11BIT);

}

void loop() {
  float temperature = 25;
  float pressure = 0;
  float humidity = 60; // default humidity
  float co2 = 0;
  float tvoc = 0;



  temperature = hdc1080.readTemperature();
  humidity = hdc1080.readHumidity();

  ccs.setEnvironmentalData( humidity, temperature );

  if (ccs.available()) {
    if (!ccs.readData()) {
      //      Serial.print("CO2: ");
      //Serial.print(ccs.geteCO2());
      //Serial.print("ppm, TVOC: ");
      //Serial.println(ccs.getTVOC());
      co2 = ccs.geteCO2();
      tvoc =   ccs.getTVOC();
    }
  }

  Serial.print("Temp\t");
  Serial.print(temperature);
  Serial.print("\t");

  Serial.print("Humidity\t");
  Serial.print(humidity);
  Serial.print("\t");

  Serial.print("CO2\t");
  Serial.print(co2);
  Serial.print("\t");
  Serial.print("TVOC\t");
  Serial.print(tvoc);
  Serial.println("");
  delay(500);
  digitalWrite(LED_ONBOARD, HIGH);
  delay(500);
  digitalWrite(LED_ONBOARD, LOW);
}

void I2C_Scan() {
  byte error, address;

  int nDevices;
  Serial.println("I2C Scanning...");
  nDevices = 0;
  for (address = 1; address < 127; address++ ) {
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
    if (error == 0) {
      Serial.print("I2C device found at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
      nDevices++;
    }
    else if (error == 4) {
      Serial.print("Unknow error at address 0x");
      if (address < 16) {
        Serial.print("0");
      }
      Serial.println(address, HEX);
    }
  }
  if (nDevices == 0) {
    Serial.println("No I2C devices found\n");
  }
  else {
    Serial.println("done\n");
  }
}

Referensi

 

Tinggalkan Balasan

Situs ini menggunakan Akismet untuk mengurangi spam. Pelajari bagaimana data komentar Anda diproses.