ESP32 CCS811 Digital Gas Sensor
The CCS811 is a digital gas sensor for monitoring indoor air quality. It measures levels of Total Volatile Organic Compounds (TVOCs) and equivalent CO₂ (eCO₂), providing valuable data for applications like air purifiers, HVAC systems, and smart home devices. Operating over an I²C interface, it simplifies integration into various projects.
Jump to Code Examples
Quick Links
CCS811 Price
About CCS811 Digital Gas Sensor
The CCS811 is an ultra-low power digital gas sensor designed for indoor air quality monitoring. It detects a wide range of Volatile Organic Compounds (VOCs) and provides equivalent CO₂ (eCO₂) levels. The sensor integrates a metal oxide (MOX) gas sensor with a microcontroller unit (MCU) and an Analog-to-Digital Converter (ADC), facilitating easy integration into various applications. Compared to the DS1302, which is a Real-Time Clock (RTC) module, the CCS811 serves a different purpose by focusing on air quality measurement rather than timekeeping.CCS811 Sensor Technical Specifications
Below you can see the CCS811 Digital Gas Sensor Technical Specifications. The sensor is compatible with the ESP32, operating within a voltage range suitable for microcontrollers. For precise details about its features, specifications, and usage, refer to the sensor’s datasheet.
- Protocol: I2C
- Operating Voltage: 3.3V to 5V DC
- Current Consumption: 0.7µA (sleep mode), 1.2mA (active mode)
- Measurement Range: 400 to 8192 ppm (eCO₂), 0 to 1187 ppb (TVOC)
- Interface: I²C
- Operating Temperature: -5°C to 50°C
- Operating Humidity: 10% to 95% RH
- Dimensions: 2.7mm x 4.0mm x 1.1mm
CCS811 Sensor Pinout
Below you can see the pinout for the CCS811 Digital Gas Sensor. The VCC
pin is used to supply power to the sensor, and it typically requires 3.3V or 5V (refer to the datasheet for specific voltage requirements). The GND
pin is the ground connection and must be connected to the ground of your ESP32!
The CCS811 pinout is as follows:
- VDD: Supply voltage (3.3V to 5V).
- GND: Ground connection.
- SDA: I²C data line.
- SCL: I²C clock line.
- nWAKE: Wake pin (active low; connect to GND to enable communication).
- nINT: Interrupt pin (optional; indicates data ready).
- nRESET: Reset pin (active low; optional).
- ADDR: I²C address select (connect to GND for 0x5A or VDD for 0x5B).
CCS811 Wiring with ESP32
Below you can see the wiring for the CCS811 Digital Gas Sensor with the ESP32. Connect the VCC pin of the sensor to the 3.3V pin on the ESP32 or external power supply for power and the GND pin of the sensor to the GND pin of the ESP32. Depending on the communication protocol of the sensor (e.g., I2C, SPI, UART, or analog), connect the appropriate data and clock or signal pins to compatible GPIO pins on the ESP32, as shown below in the wiring diagram.
VDD
to a 3.3V or 5V power source, GND
to ground, SDA
to the microcontroller's SDA pin, and SCL
to the microcontroller's SCL pin. Connect nWAKE
to GND to enable communication. The I²C address can be set by connecting ADDR
to GND (0x5A) or VDD (0x5B). Ensure that the I²C communication parameters are correctly configured in your code.Code Examples
Below you can find code examples of CCS811 Digital Gas Sensor with ESP32 in several frameworks:
ESP32 CCS811 Arduino IDE Code Example
Fill in your main
Arduino IDE sketch file with the following code to use the CCS811 Digital Gas Sensor:
#include <Wire.h>
#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs;
void setup() {
Serial.begin(9600);
Wire.begin();
if (!ccs.begin()) {
Serial.println("Failed to start sensor! Please check your wiring.");
while (1);
}
// Wait for the sensor to be ready
while (!ccs.available());
}
void loop() {
if (ccs.available()) {
if (!ccs.readData()) {
Serial.print("eCO2: ");
Serial.print(ccs.geteCO2());
Serial.print(" ppm, TVOC: ");
Serial.print(ccs.getTVOC());
Serial.println(" ppb");
} else {
Serial.println("Error reading sensor data");
}
}
delay(1000);
}
This Arduino sketch interfaces with the CCS811 sensor using the Adafruit_CCS811 library. In the setup()
function, the sensor is initialized, and the code waits until the sensor is ready. The loop()
function checks if new data is available and reads the eCO₂ and TVOC levels, printing them to the Serial Monitor every second.
Connect your ESP32 to your computer via a USB cable, Ensure the correct Board and Port are selected under Tools, Click the "Upload" button in the Arduino IDE to compile and upload the code to your ESP32.
ESP32 CCS811 ESP-IDF Code ExampleExample in Espressif IoT Framework (ESP-IDF)
If you're using ESP-IDF to work with the CCS811 Digital Gas Sensor, here's how you can set it up and read data from the sensor. Fill in this code in the main
ESP-IDF file:
#include <stdio.h>
#include "driver/i2c.h"
#include "ccs811.h"
#define I2C_MASTER_SCL_IO 22
#define I2C_MASTER_SDA_IO 21
#define I2C_MASTER_NUM I2C_NUM_0
#define I2C_MASTER_FREQ_HZ 100000
void app_main(void) {
i2c_config_t i2c_config = {
.mode = I2C_MODE_MASTER,
.sda_io_num = I2C_MASTER_SDA_IO,
.scl_io_num = I2C_MASTER_SCL_IO,
.sda_pullup_en = GPIO_PULLUP_ENABLE,
.scl_pullup_en = GPIO_PULLUP_ENABLE,
.master.clk_speed = I2C_MASTER_FREQ_HZ
};
i2c_param_config(I2C_MASTER_NUM, &i2c_config);
i2c_driver_install(I2C_MASTER_NUM, I2C_MODE_MASTER, 0, 0, 0);
ccs811_t sensor;
ccs811_init(&sensor, I2C_MASTER_NUM, CCS811_ADDR_LOW);
printf("Initializing CCS811\n");
if (ccs811_start(&sensor) != CCS811_OK) {
printf("Failed to start CCS811 sensor\n");
return;
}
while (1) {
uint16_t eco2, tvoc;
if (ccs811_read_data(&sensor, &eco2, &tvoc) == CCS811_OK) {
printf("eCO2: %d ppm, TVOC: %d ppb\n", eco2, tvoc);
} else {
printf("Error reading data from CCS811\n");
}
vTaskDelay(pdMS_TO_TICKS(1000));
}
}
This ESP-IDF example interfaces with the CCS811 sensor over I²C using SDA (GPIO21) and SCL (GPIO22). The I²C bus is initialized with appropriate parameters, and the CCS811 sensor is configured. In the main loop, eCO₂ and TVOC data are read and displayed every second. Errors are handled gracefully with appropriate messages.
Update the I2C pins (I2C_MASTER_SDA_IO
and I2C_MASTER_SCL_IO
) to match your ESP32 hardware setup, Use idf.py build to compile the project, Use idf.py flash to upload the code to your ESP32.
ESP32 CCS811 ESPHome Code Example
Fill in this configuration in your ESPHome YAML configuration file (example.yml
) to integrate the CCS811 Digital Gas Sensor
i2c:
sda: GPIO21
scl: GPIO22
sensor:
- platform: ccs811
eco2:
name: "eCO2"
tvoc:
name: "TVOC"
address: 0x5A
update_interval: 1s
The ESPHome configuration sets up I²C communication with the CCS811 sensor using SDA (GPIO21) and SCL (GPIO22). The sensor
platform fetches eCO₂ and TVOC data at 1-second intervals, displaying them as named sensors ('eCO2' and 'TVOC'). The I²C address is set to 0x5A, the default address for the CCS811.
Upload this code to your ESP32 using the ESPHome dashboard or the esphome run
command.
ESP32 CCS811 PlatformIO Code Example
For PlatformIO, make sure to configure the platformio.ini
file with the appropriate environment and libraries, and then proceed with the code.
Configure platformio.ini
First, your platformio.ini
should look like below. You might need to include some libraries as shown. Make sure to change the board to your ESP32:
[env:esp32dev]
platform = espressif32
board = esp32dev
framework = arduino
monitor_speed = 115200
ESP32 CCS811 PlatformIO Example Code
Write this code in your PlatformIO project under the src/main.cpp
file to use the CCS811 Digital Gas Sensor:
#include <Wire.h>
#include "Adafruit_CCS811.h"
Adafruit_CCS811 ccs;
void setup() {
Serial.begin(115200);
Wire.begin(21, 22); // SDA: GPIO21, SCL: GPIO22
if (!ccs.begin()) {
Serial.println("Failed to start sensor! Please check your wiring.");
while (1);
}
while (!ccs.available()); // Wait for the sensor to be ready
}
void loop() {
if (ccs.available()) {
if (!ccs.readData()) {
Serial.print("eCO2: ");
Serial.print(ccs.geteCO2());
Serial.print(" ppm, TVOC: ");
Serial.print(ccs.getTVOC());
Serial.println(" ppb");
} else {
Serial.println("Error reading sensor data");
}
}
delay(1000);
}
This PlatformIO example interfaces with the CCS811 sensor using SDA (GPIO21) and SCL (GPIO22). It initializes the sensor and retrieves eCO₂ and TVOC levels, printing the results to the Serial Monitor every second. Errors are handled gracefully.
Upload the code to your ESP32 using the PlatformIO "Upload" button in your IDE or the pio run --target upload
command.
ESP32 CCS811 MicroPython Code Example
Fill in this script in your MicroPython main.py file (main.py
) to integrate the CCS811 Digital Gas Sensor with your ESP32.
from machine import I2C, Pin
import time
# CCS811 I2C address
CCS811_ADDR = 0x5A
# Register addresses
MEAS_MODE = 0x01
ALG_RESULT_DATA = 0x02
APP_START = 0xF4
HW_ID = 0x20
# Initialize I2C
i2c = I2C(0, scl=Pin(22), sda=Pin(21))
# Verify the sensor
hw_id = i2c.readfrom_mem(CCS811_ADDR, HW_ID, 1)
if hw_id[0] != 0x81:
print("CCS811 not found!")
while True:
pass
# Start the sensor
i2c.writeto(CCS811_ADDR, bytes([APP_START]))
time.sleep(0.1)
# Set measurement mode
i2c.writeto_mem(CCS811_ADDR, MEAS_MODE, bytes([0x10]))
def read_data():
data = i2c.readfrom_mem(CCS811_ADDR, ALG_RESULT_DATA, 8)
eCO2 = (data[0] << 8) | data[1]
TVOC = (data[2] << 8) | data[3]
return eCO2, TVOC
while True:
eCO2, TVOC = read_data()
print(f"eCO2: {eCO2} ppm, TVOC: {TVOC} ppb")
time.sleep(1)
This MicroPython script interfaces with the CCS811 sensor over I²C using SDA (GPIO21) and SCL (GPIO22). It verifies the sensor's presence by reading its hardware ID, initializes it by writing to the APP_START
register, and configures the measurement mode. In the main loop, the script reads eCO₂ and TVOC values from the ALG_RESULT_DATA
register and prints the results every second.
Upload this code to your ESP32 using a MicroPython-compatible IDE, such as Thonny, uPyCraft, or tools like ampy
.
Conclusion
We went through technical specifications of CCS811 Digital Gas Sensor, its pinout, connection with ESP32 and CCS811 Digital Gas Sensor code examples with Arduino IDE, ESP-IDF, ESPHome and PlatformIO.