Indoor Air Quality (IAQ) monitoring is growing trend in the home automation space. And one way to measure air quality is to measure the carbon dioxide (CO2) that is present in your home. Indeed, high levels of CO2 in your home can lead to getting drowsy, having headaches, and functioning at lower activity levels. There are not that many sensors out there that you can buy and easily interface with open-source platforms like Arduino. Luckily, the people at offered to send us a K30 CO2 sensor to independently test their Arduino code. In this article, I will show you how to use this sensor with Arduino and how to interface it with the CC3000 WiFi chip to build a wireless CO2 sensor. Let’s dive in!

Hardware requirements

The whole project is based on the Arduino platform, so of course you will need an Arduino board. I really recommend using the Arduino Uno board for this project, as it is the only board that is currently compatible with the CC3000 library at the time this article was written.

You will also need the K30 sensor to measure the CO2 levels in your home, which can be found directly on the website.

Then, you need the CC3000 chip. I recommend is using the Adafruit CC3000 breakout board, which is the only one I tested that worked without problem. It is nice and compact, has voltage regulators onboard, as well as an onboard antenna.

Finally, you need a breadboard and some jumper wires to make the connections between the different parts.

Software requirements

For this project, you need the usual Arduino IDE. You will also need the Adafruit’s CC3000 library, as well as the library for the K30 sensor. Download the K30 CO2 sensor library (zip) here. To install a library, just download the folders, and put them into your /Arduino/libraries/ folder.

You will also need a web server running on your computer. There are thousands of tutorial for that, but I recommend using MAMP if you are on OS X.

The complete code for this project can be found on our GitHub repository.

Hardware configuration

The hardware configuration for this project is actually not that complicated, thanks to the good information that you will find about the CC3000 breakout board. Connect the IRQ pin of the CC3000 board to pin number 3 of the Arduino board, VBAT to pin 5, and CS to pin 10. Then, you need to connect the SPI pins to the Arduino board: MOSI, MISO, and CLK go to pins 11,12, and 13, respectively. Finally, take care of the power supply: Vin goes to the Arduino 5V, and GND to GND.

The K30 sensor is qui easy to connect: you need to connect two power pins (5V and GND) and two pins for the serial connection. The following picture summarises the hardware connections for the K30 sensor:


Testing the K30 sensor

We will now test the K30 sensor individually, just to be sure that your hardware connections are correct and that the sensor is functioning correctly. The kSeries library that you installed at the beginning of this tutorial handles most of the hard work to interface with the sensor. You first have to include the library:

#include "kSeries.h"

You then need to create an instance of the sensor. It uses the software serial library internally, so you also need to specify which pins you used for the serial connection:

kSeries K_30(6,7);

Then, reading the CO2 value from the sensor is actually easy:

double co2 = K_30.getCO2('p');

The rest of the sketch simply consists in printing out the value to the serial monitor. This is the complete sketch to test the K30 sensor:

#include "kSeries.h"

// Create K30 instance on pin 6 & 7
kSeries K_30(6,7);

void setup()

void loop()
  // Get CO2 value from sensor
  double co2 = K_30.getCO2('p');

  // Print the value on Serial
  Serial.print("Co2 ppm = ");

  // Wait 2 seconds

You can then upload the sketch to your Arduino board, and open the serial monitor. You should see the CO2 reading being printed every 2 seconds. The value should be between 500 ppm and 700 ppm if you are indoors.

Putting it all together

We can now dive into the core of this article, and connect the K30 wirelessly to a remote server. This is quite similar to what you can see in our article about the WiFi-connected weather station.

First, the Arduino sketch. Of course, you need to import the right libraries:

#include <Adafruit_CC3000.h>
#include <ccspi.h>
#include <SPI.h>
#include <string.h>
#include "utility/debug.h"
#include "kSeries.h"

Then, you need to define inside the code what is specific to your configuration: WiFi name & password, IP address of your computer, port of your server (usually 80), and the repository you put your server code in:

#define WLAN_SSID "yourNetwork"
#define WLAN_PASS "yourPassword"

uint32_t ip = cc3000.IP2U32(192,168,0,1);
int port = 80;
String repository = "/arduino-k30-wifi/";

We can then create the CC3000 instance:

Adafruit_CC3000 cc3000 = Adafruit_CC3000(ADAFRUIT_CC3000_CS, ADAFRUIT_CC3000_IRQ, ADAFRUIT_CC3000_VBAT,

And let’s not forget to create the instance for the K30 sensor:

kSeries K_30(6,7);

In the setup() part of the sketch, we need to connect the CC3000 to the network:


In the loop() part, we need to get data from the sensor, and to send it to the server by doing a GET request. The first part is really simple:

double co2 = K_30.getCO2('p');

And transform this into a string:

String co2_ppm = String((int) co2);

Establishing the connection to the remote server can be tricky. That’s why the sketch is including a function called send_request to handle that part. This is the code to send the request:

String request = "GET "+ repository + "sensor.php?data=" + co2_ppm + " HTTP/1.0";

We can see that this code is calling a file named sensor.php on the server side. The server.php file will basically get the variable containing the data, and save it into a file:


  // Store data
  if ($_GET["data"]) {

    $myFile = "data.txt";
    $fh = fopen($myFile, 'w');
    fwrite($fh, $_GET["data"]);



Then, another file called data_display.php reads the content of the files and displays it:


  $myFile = "data.txt";
  $fh = fopen($myFile, 'r');
  $line = fgets($fh);

  echo $line;


Finally, the file sensor.html is in charge of displaying the data in the browser. The data is automatically updated with this JavaScript code:

<script type="text/javascript">

}, 1000);


As usual, all the files are available on our GitHub repository. You can now put all the server files into your server repository, and upload the Arduino sketch to your board. If you open the serial monitor, this is what you should see (don’t forget to start your web server !):


This is the final result when you access the server from your computer:


Of course, this interface can be accessed in the browser of any device connected to your local WiFi network, like your phone or your tablet.

As always, please comment below if you have any question, a suggestion on how to improve the article, or if you find some nasty bugs in the code! If you liked this article, please share it around! Finally, this is the list of the parts that were used in this project:

You can also find most of the required parts for this tutorial in our Arduino WiFi kit.