Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
CO2 measurement module MH-Z14A for ESP32 arduino core. use library https://github.com/squix78/esp8266-oled-ssd1306
#include "SSD1306.h"
HardwareSerial Serial1(0);
SSD1306 display(0x3c, 5, 4);
uint8_t cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
uint8_t reset[9] = {0xFF,0x01,0x87,0x00,0x00,0x00,0x00,0x00,0x78};
uint8_t res[9] = {};
uint8_t idx = 0;
bool flag = false;
uint16_t co2=0;
void setup() {
display.init();
display.flipScreenVertically();
display.setContrast(255);
Serial.begin(9600);
Serial1.begin(9600);
delay(100);
display.setTextAlignment(TEXT_ALIGN_CENTER_BOTH);
display.setFont(ArialMT_Plain_24);
display.clear();
display.drawString(DISPLAY_WIDTH/2, 30, "preheating...");
display.display();
delay(3*60*1000);
display.setFont(ArialMT_Plain_16);
}
void loop() {
Serial1.write(cmd,9);
while(Serial1.available()>0)
{
res[idx++]=Serial1.read();
flag=true;
}
idx = 0;
if(flag)
{
flag=false;
co2 = 0;
co2 += (uint16_t)res[2] <<8;
co2 += res[3];
}
Serial.println(co2);
display.clear();
display.drawString(DISPLAY_WIDTH/2, 30, "co2 : " + String(co2)+" ppm");
display.display();
delay(60*1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment