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:

#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.

  • 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?


    • 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.

    • Chris,
      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!

  • 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

    • Sure thing!

      • Hawllysson

        Even though the sandbox?

        • Hawllysson

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

          • Vladislav Cizek

            It’s working for me. I had some problems due to bad wiring and absence of the CO2 sensor heating circuit – I’m an amateur, but all in all it works fine. What is a different big consideration is the calibrating method. That is a hard nut to crack.

  • 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?

    • 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 🙂

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

    • Alessio Scordia

      Hi, I have the same problem, did you solve it?

    • Adam Pyszko

      Switching it to Pins 12 & 13 works on the Mega 2560. Finally got the beast running.

  • 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!

    • I am not sure about that, I haven’t played with other K30 sensors. But that’s worth a try 🙂

  • vishalsaw

    Can I use tgs 4161 co2 sensor with this code. Also if not then how can I use that sensor ??

  • Cristianc_

    Hello Marco, thanks a lot for the tutorial. We are building an autonomous CO2/temperature/humidity sensor with arduino but we’ve had some troubles choosing the right battery. Could you give us any recomendation??

  • Angga Dwi Krishnajaya

    Hi, what code do I use when i am using ethernet shield ? Thanks

    • It would be really similar, look at the Ethernet library examples 🙂

  • John Whitsonbell
    • Sure, that’s definitely possible with a similar project, you just need to add more sensors to the project.

  • Adam Pyszko

    Has anybody managed solving the issue with the -203 readout on a Mega 2560?
    It is giving out that error even if the K30 pins 6&7 are disconnected.

    • Chris Luppe

      That is the output when it does not detect the sensor connected. I originally had mine connected to the Tx and Rx pins on the 2560 but it’s actually just digital pins.

    • Adam Pyszko

      Switching it to Pins 12 & 13 works on the Mega 2560. Finally got the beast running.

      Funny thing. It says you should use an external power source of 5V because the sensor takes so much power, when when I did I started getting ~-300ppms, but when i switched it back to the Mega it works just fine and back to ~800s

  • Duomo (UK) Ltd.

    The K30 is a fantastic chip! Great Reliability.We have been the UK distributor for Sense Air for over 10 Years. Get in touch if you need anything. http://www.duomo.co.uk

  • Matt Rosenblatt

    this is cool for a grow room or something… regarding indoor air quality in your home, try a few house plants 😛

  • Sadab Mostafa

    can i use this to monitor outdoor Co2??