# The CO2 and air quality sensor

### Objectives:

* The aim of this activity is to measure the amount of CO2 in the environment and measure the air quality based on the ambient temperature.
* Connect the circuit for the CCS811 sensor.
* Write the code (and upload on the Arduino Uno board) to serial print the CO2, ambient temperature and volatile organic compounds.&#x20;

![](/files/-M2330xTUyL3-_v4hUgl)

### What do you need ?

* Arduino Uno Board.&#x20;
* male-to-male jumper cables.&#x20;
* USB power cable for the Uno.
* keyestudio CCS811 Carbon Dioxide Temperature Air Quality Sensor

### Setup the Circuit

![The circuit for the CO2 and air quality sensor](/files/-M233ycL55WCuXKu1dq-)

![Pin description of the CCS811 sensor](/files/-M235ER2uXfPXimu2vce)

1. Insert the CO2 sensor of the breadboard.
2. Connect the GND pin of the sensor to one of the GND pins of the Arduino Uno.
3. Connect the VCC pin of the sensor to the 5V pin of the Uno.
4. Connect the SDA pin of the sensor to the Analog pin 4 (A4) of the Uno.
5. Connect the SCL pin of the sensor to the analog pin 5 (A5) of the Uno.
6. Connect the WAKE pin of the sensor to the other GND pin of the Uno.

### The Code&#x20;

* Open your Arduino IDE software and configure the board and the port accordingly.&#x20;
* Download the [Adafruit CCS811 library](https://drive.google.com/drive/folders/1T175_657leBks22ww0Fwo_Txzmk026TR) and include it in your Arduino IDE.
* Add the following code to your IDE:

```
#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);
}
```

Open the serial port. Blow on the sensor and see how the sensor values changes.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://reacoda.gitbook.io/molemi-iot/introducing-sensors/the-co2-and-air-quality-sensor.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
