MH-Z14A CO2 Sensor
MH-Z14A CO2 Sensor
Introduction :
고급 이산화탄소 센서로써, 광학식으로 CO2 농도를 측정한다.
3.3V를 사용하기때문에 5V에서는 레벨 컨버터를 사용하여야 한다.
Features :
출력인터페이스: UART, PWM (0.4V~2V DC)
높은 분해능과 감도
저전력 파워소모
Specification :
측정 범위: 0~5000ppm
오차: +-(50ppm+5%)
동작 전압: 4.5~5.5V
전류: <85mA
인터페이스 레벨: 3.3V
초기 예열시간: 3분
응답시간: <90초
동작온도: 섭씨 0~50도
동작 습도: 0~95%RH
사이즈: 33mm x 20mm x 9mm (LxWxH)
무게: 23g
Doc :
MH-Z14A DataSheet :
mh-z14a_co2-manual-v1_01.pdf
연결회로도
Code
include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A1);
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
byte response[9];
String ppmString = " ";
void setup() {
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
mySerial.write(cmd,9);
mySerial.readBytes(response, 9);
byte chck = 0;
if(response[8] == (0xff&(~(response[1]+response[2]+response[3]+response[4]+response[5]+response[6]+response[7]) + 1))){
Serial.println("OK");
}
else {
Serial.print("chksum : ");
Serial.println(response[8],HEX);
Serial.print("read : ");
Serial.println(0xff&(~(response[1]+response[2]+response[3]+response[4]+response[5]+response[6]+response[7]) + 1),HEX);
while(mySerial.available() > 0){
mySerial.read();
}
}
int ppm = (response[2] << 8)|response[3];
ppmString = String(ppm); //int to string
Serial.print("PPM ");
Serial.println(ppm);
delay(2000);
}
|
MH-Z14A Python code : https://github.com/keepworking/MH-Z14A-PI
import serial import time class MHZ14A(): packet = [0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79] zero = [0xff, 0x87, 0x87, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf2] def __init__(self,ser="/dev/ttyAMA0"): self.serial = serial.Serial(ser,9600,timeout=1) time.sleep(2) def get(self): self.serial.write(bytearray(self.packet)) res = self.serial.read(size=9) res = bytearray(res) checksum = 0xff & (~(res[1]+res[2]+res[3]+res[4]+res[5]+res[6]+res[7])+1) if res[8] == checksum: return {"ppm":(res[2]<<8)|res[3],"temp":res[4]} else: print hex(checksum) return -1 def close(self): self.serial.close def main(): co2 = MHZ14A("/dev/ttyAMA0") try: print co2.get() except: print "err" co2.close() if __name__ == '__main__': main() |