Display the results of CO2 concentration measurements on an OLED display | そう備忘録

Display the results of CO2 concentration measurements on an OLED display

OLED Display

I got a 0.96 inch square OLED(Organic Light Emitting Diode) display and connected it to ESP32-WROOM-32 to display the measurement results of the CO2 sensor (MH-Z19C).

CO2 concentration on OLED display

The specifications of the OLED display are as follows

  • Display size: 0.96 inches
  • Resolution: 128 x 64 px
  • Supply voltage: 3.3V to 5V *
  • Display controller: SSD1306
  • Interface: I2C
  • Display color: White
  • I2C address: 0x3C or 0x3D

This time, I tried connecting to both 3.3v and 5V, and both were displayed without any problem.

When previously tested with an LCD (liquid crystal display), the display darkened considerably when connected to 3.3V, but this was not the case with this product.

Surface

OLED display Surface

backside

Backside of OLED display
  • Pins from left to right are SDA, SCL, VCC, and GND for I2C connection.
  • I2C address is shorted to 0x78 on the left side, which is 0x3C. If shorted to 0x7A on the opposite side, it becomes 0x3D.

About CO2 Sensor

The MH-Z19C CO2 sensor was used to measure carbon dioxide.

This sensor can output CO2 concentration to PWM or UART (serial).

For details of MH-Z19C, please refer to this article.

Wiring Diagram

The wiring diagram is as follows

CO2 concentration on OLED display Wiring diagram

The pin layout of the ESP32-WROOM-32 module used in this project is the same as that of the ESP32 DEVKIT V1 (30 pins).

On the board, the bottom left pin is printed as VIN, but it is actually a 5V output pin.

Please refer to this article for the pin assignment of ESP32 DEVKIT V1.

ESP-WROOM-32

As mentioned above, the OLED display works fine at 3.3V.

In the wiring diagram, it is powered from 5V, but there was no problem even if it was powered from 3V3 at the bottom right.

Preliminary Preparation

Arduino IDE (v1.8.19) on Windows 10 was used as the development environment.

SSD1306 library

Since this OLED display uses the SSD1306 as its display controller, I anticipated that the Adafruit SSD1306 library would be available and installed it beforehand.

I started the Arduino IDE, click to Tools>Manage Libraries, started the Library Manager, searched for “SSD1306″ and installed “Adafruit SSD1306” that showed up.

Github is here.

Installing the Adafruit SSD1306 Library

Sketch

A sketch of the CO2 concentration on an OLED display is shown below.

/*
 * CO2 Sensor (MH-Z19C) readings are displayed on an OLED (Organic Light Emitting Diode) display
 * LCD:I2C
 */

#include <MHZ.h> // CO2 Sensor
#include <Adafruit_SSD1306.h> // OLED display

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define RX_PIN 16 // GPIO16
#define TX_PIN 17 // GPIO17
#define OLED_RESET 4 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C Address

Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
MHZ co2(RX_PIN, TX_PIN, MHZ19C); // Co2 Sensor

void setup() {
    Serial.begin(115200);
    if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
        Serial.println(F("SSD1306 allocation failed"));
        for(;;); // Don't proceed, loop forever
    }

    display.display();
    delay(2000);
    display.clearDisplay(); // Buffer Clear

    delay(100);
    if (co2.isPreHeating()) {
        display.setTextSize(1);
        display.setTextColor(SSD1306_WHITE);
        display.setCursor(0, 0);
        display.println(F("Preheating"));
        display.display();
        delay(100);
        int x_pos = 0;
        int y_pos = 10;

        while (co2.isPreHeating()) {
            delay(5000);
            display.setCursor(x_pos, y_pos); // X Y
            display.cp437(true); // Code Page 437 font(full 256 char)
            display.write('.');
            display.display();
            if (x_pos <= SCREEN_WIDTH){
                x_pos +=10;
            } else {
                x_pos =0;
                y_pos +=10;
            }
        }
    }
    // co2.setAutoCalibrate(true);
}

void loop() {
    int ppm_uart = co2.readCO2UART();
    Serial.println("Co2:" + String(ppm_uart) + " ppm");
    display.clearDisplay(); // Buffer Clear
    display.setCursor(0, 0); // X Y
    display.println("CO2:" + String(ppm_uart) + " ppm");
    int temperature = co2.getLastTemperature();
    Serial.println("temp:" + String(temperature) + " c");
    display.setCursor(0, 20); // X Y
    display.println("temp:" + String(temperature) + " c");
    display.display();
    // display.startscrollleft(0x00, 0x0F);

    delay(120000);
}

Supplemental Explanation

Acquisition of CO2 concentration

Please refer to the previous article for the logic of obtaining Co2 concentration by MH-Z19C.

Scrolling display

Commented out, but the display string scrolls to the left when line 69 is utilized.

    // display.startscrollleft(0x00, 0x0F);

Execution Result

The image at runtime is shown below.

CO2 concentration is displayed on the first line and temperature on the second line.

CO2 concentration measurement results on OLED display

This concludes this article.

Finally.

I hope this article will be useful to someone somewhere.

souichirou kikuchi

I'm Japanese. A reminder to remember what I've done. I'm blogging in the hope that it will be helpful to others who want to do similar things. I mainly write blogs about LEGO, AWS (Amazon Web Services), WordPress, Deep Learning and Raspberry Pi. At work, I'm working on installing collaborative robots and IoT in factories. I passed the JDLA (Japan Deep Learning Association) Deep Learning for GENERAL in July 2019. If you have any questions, please leave them in the comments at the bottom of the article.

comment

Name, Email, and Website are optional.
and, your Email address will not be published.