Build a Wireless CO2 Sensor with Arduino

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 CO2Meter.com 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 CO2Meter.com 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:

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:

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

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:

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:

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:

We can then create the CC3000 instance:

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

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:

And transform this into a string:

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:

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:

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

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

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.

  • Chris

    I would like to try to make a project very similar to his, but the shipping cost from Co2Sensor is terrible.

    It is more expensive to ship the product than buy it from them to New Zealand.

    ◾FedEx International Economy at $66.88 USD
    ◾FedEx International Priority at $86.38 USD
    ◾UPS Worldwide Expedited at $142.68 USD
    ◾UPS Saver at $145.91 USD

    Is there any resellers who sell these sensors?


    • http://www.marcoschwartz.com Marco Schwartz

      Hello Chris,

      Unfortunately I don’t know of any resellers, they indicated me that the only way to get the sensor was on their website.

      • Dennis Matsari

        Hello Marco,
        I build this project i am using MAMP as a webserver after a while 10 or sometimes a half our its losing contact with the webserver and i have to reset the Arduino and the its start working an after a while the same problem, why its losing contact ? i can reach the cc3000.

    • http://www.co2meter.com Josh Pringle

      Unfortunately we can’t control the rates that UPS and FedEx charge. We are in constant negotiations with those carriers about rates.
      We do have distributors in New Zealand and elsewhere that you can purchase from directly that may reduce the overall shipping charges.
      If you have any other concerns please contact me directly.
      Happy New Year!

  • http://www.epluse.com/en/products/co2-measurement/co2-sensor/ Martin

    Looks like an awesome project. I’m just diving into the whole Arduino world but this might be doable, even for a noob like me. :) Now I just need to get a hold of a CO2 Sensor and I’m good to go.

  • Dusty

    first amazing website what helps me a lot to understand a bit of the arduino world.
    For me on a Arduino Mega the K30 Sensor is only working with the pin 12,13. What is the secret to get the K30 to work with other pins,like in your sketch with -> kSeries K_30(6,7) ?

  • ko
  • Raj A Badri

    can i use MG-811 CO2/Carbon Dioxide Sensor Module instead

    • http://www.marcoschwartz.com/ Marco Schwartz

      Sure thing!

      • Hawllysson

        Even though the sandbox?

        • Hawllysson

          I am Brazilian, and bought a 811-mg sandbox. I could not use. :

  • Nurdan

    Hello, thank you so much for this helpful document!!

    We are designing a similar project but we have mq135 instead k30 in our hand. Can we also follow same steps with you?

    • http://www.marcoschwartz.com/ Marco Schwartz

      You’re welcome! Sure, it would be exactly the same steps with another sensor.

  • Idil

    Hello, we are trying to use Arduino Mega instead of Uno. We tried your methodology on Arduino Uno and it worked very well. However, it did not work on Arduino, K30 sensor is giving -203.00 all the time. What can our problem be, is it related with library? Or related with timers of Arduino Mega? How can we fix it? PLEASE HELP US OUT :)

    • http://www.marcoschwartz.com/ Marco Schwartz

      I haven’t tried it with the Mega, but that’s something you will have to ask directly to the manufacturer.

  • LES

    Can the K30 ASCII be used with the Aduino Uno as well? I’m already using K30 ASCII with Campbell CR1000 data loggers, and was just wondering if I can easily try out the Arduino system with a K30 ASCII that I already have. Thanks!