Blog

Arduino/ESP32 CO2 Sensor with OLED Display

6/3/2025 2 min read
Arduino/ESP32 CO2 Sensor with OLED Display

You are starting an exciting project: displaying environmental data using the SSD1306 OLED display and an SCD30 air quality sensor. If you have already read the previous post about using the SCD30 sensor without a display, you will notice that this script represents an extended application by utilizing the display capabilities of the SSD1306.


Hardware

First, let's go through the required hardware. You need:

  • SSD1306 OLED display
  • SCD30 air quality sensor
  • Microcontroller (e.g., ESP32 or Arduino Uno)
  • I2C connection cables

It is important that you connect the display and the sensor to the I2C ports of your microcontroller.
For the ESP32, these are typically pins 21 (SDA) and 22 (SCL),
while for the Arduino Uno, you should use pins A4 (SDA) and A5 (SCL).


How the Script Works

In your script, you first initialize the required libraries and define the screen dimensions of your display. Then you initialize the SCD30 sensor and the SSD1306 display. The setup() routine is crucial to ensure successful communication over I2C.

In the loop() function, you query the data from the SCD30 sensor and display it on the screen. Displayed are:

  • CO₂ value
  • Temperature
  • Humidity

The display is regularly updated to always show the latest measurement values.

By combining the SCD30 and SSD1306, you can build a powerful air quality monitoring system that is both informative and clear.


Example Code

#include  // Include Wire library for I2C communication
#include "SparkFun_SCD30_Arduino_Library.h" // Library for SCD30 sensor
#include  // Graphics library for the display
#include  // Library for the SSD1306 OLED display

#define SCREEN_WIDTH 128 // OLED display width in pixels
#define SCREEN_HEIGHT 64 // OLED display height in pixels

SCD30 airSensor; // SCD30 sensor object
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1); // Display object

void setup() {
  Wire.begin(); // Start I2C communication
  Serial.begin(9600); // Start serial communication
  airSensor.begin(); // Initialize the SCD30 sensor

  // Initialize the OLED display
  if(!display.begin(SSD1306_SWITCHCAPVCC, 0x3C)) {
    Serial.println(F("SSD1306 allocation failed"));
    for(;;); // Infinite loop on failure
  }

  display.clearDisplay();
  display.setTextSize(2);
  display.setTextColor(SSD1306_WHITE);
}

void loop() {
  if (airSensor.dataAvailable()) {
    display.clearDisplay();
    display.setCursor(0, 0);
    display.print(F("CO2: "));
    display.println(airSensor.getCO2());

    display.setCursor(0, 16);
    display.print(F("Temp: "));
    display.println(airSensor.getTemperature(), 1);

    display.setCursor(0, 32);
    display.print(F("Hum: "));
    display.println(airSensor.getHumidity(), 1);

    display.display();
  }
  delay(500);
}