スタートアップエンジニアの作ってみた日記

ものづくりが下手な横好きなエンジニアによる作ってみた的なブログです。

CO2センサを作ってみた(CCS811搭載 空気品質センサモジュールを使ってみた)

最近、在宅勤務が増えてきて家で一人作業することが多くなりました。

家で作業をしているとなかなか集中できなかったり眠くなったりしちゃいますよね。

 

これは自分の集中力のせいではなく、部屋の二酸化炭素の濃度のせいなのでは思い、CO2センサーを作って設置してみました。

 

使用したのは以下の3つです。

■CCS811搭載 空気品質センサモジュール

https://www.switch-science.com/catalog/3298/

Arduino Uno

LCD液晶Display

https://www.amazon.co.jp/gp/product/B07BJ5PW3R/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1

 

1.配線

センサモジュールとArduinoはI2C通信と言われる通信を行います。

以下のように二つを接続してください。

モジュール  Arduino

GND ー GND

VCC ー 3.3V

SDA ー Analog 4

SCL ー Analog 5

 

f:id:goengine:20200720233803p:plain

 

 ArduinoLCDディスプレイの接続に関してはこちらの記事をご参照ください。

https://goengine.hatenablog.com/entry/2019/03/17/211934

 

2.Arduinoの準備

次にソフトウェアの準備です。

スイッチサイエンスの販売サイトよりライブラリをダウンロードしましょう。

f:id:goengine:20200720234701p:plainSparkFun_CCS811_Arduino_Library-master\SparkFun_CCS811_Arduino_Library-master\examples\Example1_BasicReadings.ino
を開きます。

そのまま書き込むと、" SparkFunCCS811.h"がありませんと怒られてしまうので、このファイルがincludeされるようにします。

以下に示すリンクをクリックします。

f:id:goengine:20200721000233p:plain

 

するとライブラリマネージャーが出てくるのでインストールを選びます。

f:id:goengine:20200721000443p:plain

 

これで準備ができたので早速さきほどのファイルを書き込んでみましょう。

問題がなく書き込めれば、これで計測がきちんと行えているかと思います。

シリアルモニタを見るとCO2の計測値が出てくるかと思います。

f:id:goengine:20200720235840p:plain

 

これでサンプルを動かし、計測はすることはできるのですが、パソコンがないと値が見れず不便なので私はサンプルコードを少しいじりLCDディスプレイに表示してみました。

 

こちらがそのソースコードになります。参考にしてください。

/******************************************************************************
  Read basic CO2 and TVOCs
  Marshall Taylor @ SparkFun Electronics
  Nathan Seidle @ SparkFun Electronics
  April 4, 2017
  https://github.com/sparkfun/CCS811_Air_Quality_Breakout
  https://github.com/sparkfun/SparkFun_CCS811_Arduino_Library
  Read the TVOC and CO2 values from the SparkFun CSS811 breakout board
  A new sensor requires at 48-burn in. Once burned in a sensor requires
  20 minutes of run in before readings are considered good.
  Hardware Connections (Breakoutboard to Arduino):
  3.3V to 3.3V pin
  GND to GND pin
  SDA to A4
  SCL to A5
******************************************************************************/
#include <Wire.h>

#include "SparkFunCCS811.h" //Click here to get the library: http://librarymanager/All#SparkFun_CCS811

#include <LiquidCrystal_I2C.h>

int count = 0;

LiquidCrystal_I2C lcd(0x27,16,2);

int list_num =3;
int co2_list[3] = {0,0,0};


#define CCS811_ADDR 0x5B //Default I2C Address
//#define CCS811_ADDR 0x5A //Alternate I2C Address

CCS811 mySensor(CCS811_ADDR);

void display_setup()
{
  lcd.init();                      
  lcd.backlight();
  lcd.setCursor(0, 0);
}

void display_print(int co2_noudo)
{
   int _position = 5;
   lcd.setCursor(0, 1);
   lcd.print("CO2:");
   lcd.init();
   if(co2_noudo > 1000){
    _position = _position -1;   
   }
   lcd.setCursor(_position, 1);
   lcd.print(co2_noudo);
   
   lcd.setCursor(8, 1);
   lcd.print("[ppm]");
}


void setup()
{
  Serial.begin(115200);
  Serial.println("CCS811 Basic Example");

  Wire.begin(); //Inialize I2C Hardware
  display_setup();

  if (mySensor.begin() == false)
  {
    Serial.print("CCS811 error. Please check wiring. Freezing...");
    while (1);
  }
}

void loop()
{
  //Check to see if data is ready with .dataAvailable()
  if (mySensor.dataAvailable())
  {
    //If so, have the sensor read and calculate the results.
    //Get them later
    mySensor.readAlgorithmResults();
   
  }
  for(int i =list_num;i > 1;i--){
    co2_list[i-1] = co2_list[i-2];
  }
  co2_list[0] = mySensor.getCO2();
  int co2_ave;
  co2_ave = (co2_list[0] + co2_list[1] +co2_list[2])/3.0; 
  display_print(co2_ave);
  Serial.print(co2_list[0]);
  Serial.print(",");
  Serial.println(co2_ave);
  

  delay(10); //Don't spam the I2C bus
}

そしてできたものがこちらです。

 

ちなみに僕の場合CO2の値は関係なく普通に眠くなっているだけだということがわかりました。。笑

 

以下のサイトを参考にさせていただきました。

ありがとうございます。

https://www.switch-science.com/catalog/3298/

http://blog.akanumahiroaki.com/entry/2018/06/12/080000