본문 바로가기
개발 공부/프로젝트

이산화 탄소 센서 사용법 알아보기(CO2 Sensor MG811)

by momo'sdad 2023. 11. 23.

이산화 탄소 센서(CO2 Sensor MG811)

http://mechasolution.com/shop/goods/goods_view.php?goodsno=8854&category=002015

 

메카솔루션 공식 쇼핑몰

 

mechasolution.com

 

 

CO2 이산화 탄소 센서에 대해 알아보기!

생물이 호흡을 할때,

산소를 들이쉬고, 이산화 탄소를 내쉬게 된다.

 

이러한 이산화 탄소를 측정하기위해,

다음과 같이 동작하게 된다.

센서 내부에는 히터와 금속판이 있는데,

히터로 공기를 가열하면, 공기의 입자가 금속판에 달라붙게 되는데

입자가 달라붙는 정도에따라, 저항값이 변하게 된다.

 

 

 

CO2 이산화 탄소 센서 특징 알아보기!

동작전압 5V

아날로그, 디지털 출력

가열 회로가 내장되어 있다.

가열될 시, 약 40도를 유지한다. 그리 높은 온도는 아니지만 오랫동안 닿아 있으면 화상을 입을 수 있다.

 

 

소스코드

/************************Hardware Related Macros************************************/
#define MG_PIN (2) //define which analog input channel you are going to use – A0 pin
#define BOOL_PIN (13)    // D9 – Dout pin
#define DC_GAIN (8.5) //define the DC gain of amplifier
/***********************Software Related Macros************************************/
#define READ_SAMPLE_INTERVAL (50) //define how many samples you are going to take in normal operation
#define READ_SAMPLE_TIMES (5) //define the time interval(in milisecond) between each samples in
//normal operation
/**********************Application Related Macros**********************************/
//These two values differ from sensor to sensor. user should derermine this value.

#define ZERO_POINT_VOLTAGE (0.185) //define the output of the sensor in volts when the concentration of CO2 is 400PPM

#define REACTION_VOLTGAE (0.010) //define the voltage drop of the sensor when move the sensor from air into 1000ppm CO2
/*****************************Globals***********************************************/

float CO2Curve[3] = {2.602, ZERO_POINT_VOLTAGE, (REACTION_VOLTGAE / (2.602 - 3))};
//two points are taken from the curve.
//with these two points, a line is formed which is
//"approximately equivalent" to the original curve.
//data format:{ x, y, slope}; point1: (lg400, 0.324), point2: (lg4000, 0.280)
//slope = ( reaction voltage ) / (log400 –log1000)

void setup()
{
  Serial.begin(115200); //UART setup, baudrate = 9600bps
  pinMode(BOOL_PIN, INPUT); //set pin to input
  digitalWrite(BOOL_PIN, HIGH); //turn on pullup resistor
  Serial.print("MG-811 Demostration\n");
}

void loop()
{
  int percentage;
  float volts;

  volts = MGRead(MG_PIN);
  Serial.print( "SEN-00007:" );
  Serial.print(volts);
  Serial.print( " V / before_amp : " );
  Serial.print(volts / DC_GAIN);
  Serial.print( " V " );

  percentage = MGGetPercentage(volts, CO2Curve);
  Serial.print("CO2:");
  if (percentage == -1) {
    Serial.print( "<400" );
  } else {
    Serial.print(percentage);
  }
  Serial.print( "ppm\n" );
  if (digitalRead(BOOL_PIN) ) {
    Serial.print( "=====BOOL is HIGH======" );
  } else {
    Serial.print( "=====BOOL is LOW======" );
  }
  Serial.print("\n");
  delay(200);
}
/***************************** MGRead *********************************************
  Input: mg_pin - analog channel
  Output: output of SEN-000007
  Remarks: This function reads the output of SEN-000007
************************************************************************************/
float MGRead(int mg_pin)
{
  int i;
  float v = 0;
  for (i = 0; i < READ_SAMPLE_TIMES; i++) {
    v += analogRead(mg_pin);
    delay(READ_SAMPLE_INTERVAL);
  }
  v = (v / READ_SAMPLE_TIMES) * 5 / 1024 ;
  return v;
}

/***************************** MQGetPercentage **********************************
  Input: volts - SEN-000007 output measured in volts
  pcurve - pointer to the curve of the target gas
  Output: ppm of the target gas
  Remarks: By using the slope and a point of the line. The x(logarithmic value of ppm)
  of the line could be derived if y(MG-811 output) is provided. As it is a
  logarithmic coordinate, power of 10 is used to convert the result to non-logarithmic
  value.
************************************************************************************/

int MGGetPercentage(float volts, float *pcurve)
{
  if ( pow(10, ((volts / DC_GAIN) - pcurve[1]) / pcurve[2] + pcurve[0] >= 0 ))
  {
    return pow(10, ((volts / DC_GAIN) - pcurve[1]) / pcurve[2] + pcurve[0]);
  } else {
    return -1;
  }
}

 

dm2468 센서

#include "CO2Sensor.h"

CO2Sensor co2Sensor(2, 0.99, 100);

void setup() {
  Serial.begin(115200);
  Serial.println("=== Initialized ===");
  co2Sensor.calibrate();
}

void loop() {
  int val = co2Sensor.read();
  Serial.print("CO2 value: ");
  Serial.print(val);
  Serial.println("ppm");
  delay(1000);
}

 

CO2센서 첨부파일

CO2Sensor-master.zip
0.32MB

 

 

 

 

위 공식을 그래프로 만들어 보면 다음과 같은 그래프가 나오게 된다.

 

위 소스코드가 동작하면

다음과 같이 출력된다.

 

 
반응형