ESP32 and CCS811 gas sensor example

In this example we will connect a CCS811 gas sensor to an ESP32, first of all lets look at the sensor

The CCS811 is a low-power digital gas sensor solution, which integrates a gas sensor solution for detecting low levels of VOCs typically found indoors, with a microcontroller unit (MCU) and an Analog-to-Digital converter to monitor the local environment and provide an indication of the indoor air quality via an equivalent CO2 or TVOC output over a standard I2C digital interface.

I usually like to find a suitable module or breakout top use a sensor, here is the one I chose.

Features

Integrated MCU
On-board processing
Standard digital interface
Optimised low power modes
IAQ threshold alarms
Programmable baseline
2.7mm x 4.0mm LGA package
Low component count
Proven technology platform

Specs

Interface I²C
Supply Voltage [V] 1.8 to 3.6
Power Consumption [mW] 1.2 to 46
Dimension [mm] 2.7 x 4.0 x 1.1 LGA
Ambient Temperature Range [°C] -40 to 85
Ambient Humidity Range [% r.h.] 10 to 95

Parts List

 

Part Link
CCS811 CJMCU-811 CCS811 Air Quality Gas Sensor
ESP32 New ESP-32 esp32 Lite V1.0.0 For Rev1 wifi Module + bluetooth board 4MB FLASH
Connecting Cable Free shipping Dupont line 120pcs 20cm male to male + male to female and female to female jumper wire

 

 

Schematics/Layout

Remember and connect WAKE to gnd

Layout

esp32 and CCS811 layout
esp32 and CCS811 layout

Schematic

esp32 and CCS811 schematic
esp32 and CCS811 schematic

 

Code

Again we use a library this is the adafruit one – you can use the library manager and add this.

And this is the out of the box example


#include "Adafruit_CCS811.h"

Adafruit_CCS811 ccs;

void setup() {
Serial.begin(9600);

Serial.println("CCS811 test");

if(!ccs.begin()){
Serial.println("Failed to start sensor! Please check your wiring.");
while(1);
}

//calibrate temperature sensor
while(!ccs.available());
float temp = ccs.calculateTemperature();
ccs.setTempOffset(temp - 25.0);
}

void loop() {
if(ccs.available()){
float temp = ccs.calculateTemperature();
if(!ccs.readData()){
Serial.print("CO2: ");
Serial.print(ccs.geteCO2());
Serial.print("ppm, TVOC: ");
Serial.print(ccs.getTVOC());
Serial.print("ppb Temp:");
Serial.println(temp);
}
else{
Serial.println("ERROR!");
while(1);
}
}
delay(500);
}

 

Output

Open the serial monitor – this is what I saw. The higher CO2 level was when I breathed on the sensor

CO2: 954ppm, TVOC: 84ppb Temp:17.12
CO2: 400ppm, TVOC: 0ppb Temp:13.32
CO2: 400ppm, TVOC: 0ppb Temp:14.63
CO2: 889ppm, TVOC: 74ppb Temp:20.24
CO2: 400ppm, TVOC: 0ppb Temp:20.53
CO2: 400ppm, TVOC: 0ppb Temp:19.67
CO2: 400ppm, TVOC: 0ppb Temp:0.93
CO2: 1513ppm, TVOC: 169ppb Temp:0.75
CO2: 501ppm, TVOC: 15ppb Temp:-0.37
CO2: 815ppm, TVOC: 63ppb Temp:26.76
CO2: 400ppm, TVOC: 0ppb Temp:27.10

Links

ccs811 datasheet