Sign In SIGN UP UPGRADE

Arduino + Air Quality Sensors

Air Quality Sensors splash image

Getting started

Want to monitor the air quality in your home, workplace, or outdoor surroundings? With an Arduino and some simple code, you can easily hook up an air quality sensor to visualize all the things!

Materials

If you're going to use the Node.js route, you do not need an internet shield — just a USB connection from your Arduino to your computer.

We apologize for the extra pin headers featured in the picture. You don't need 'em!

Air Quality Sensors Parts

Hooking it up

This one is super easy! Just hook up the jumper cables to A0 and A1 on your Grove shield. Thanks Grove!

Software

Download and install all libraries into your usual Arduino Library Folder.
The default is usually at /User/Documents/Arduino/Libraries.

If you're using the Arduino wifi shield, make sure to use version 1.0.3 of the Arduino IDE!

The script

The choice is yours! You can use an Arduino sketch for this project, or a Node.js script.

Using the Arduino sketch

Grab your choice of Arduino script here:

Open up airquality.ino and let's go through it. First we initialize a plotly graph. If you're using a wifi shield, remember to change these wireless credentials to your own in the sketch!

plotly graph("username", "api_key", tokens, "filename", nTraces);

In the setup() function, we're going to initialize the graph. Once it's initialized, we're going to open the stream. graph.openStream() opens the plotly stream and graph.plot() is where all the data gets piped in. Let's check it out:

graph.init();
graph.openStream();

In the loop() function, we're going to start streaming our sensor data!

void loop() {
    int airquality_value = analogRead(airquality_sensor_pin);
    int gas_value = analogRead(gas_sensor_pin);
    volume = (float)gas_value/1024*5.0*1000;
    graph.plot(millis(), airquality_value, tokens[0]);
    graph.plot(millis(), volume, tokens[1]);
    delay(50);
}

Your code is now all set up!

Using the Node.js script

First, you need to set up your Arduino for Node.js interaction.

Next, download the airquality.js script.

Open airquality.js, and let's go through it! First we create a new plotly object with your credentials:

var plotly = require('plotly')("username", "api_key");

The init_data object is what we send to plotly when we initialize the graph. We are sending empty arrays to act as containers for our data. You can also choose the maximum number of points to display.

Don't forget to change the 'streamtoken' to your own!

var init_data = [{
    x : [],
    y : [],
    stream : {
        token : 'streamtoken',
        maxpoints : 5000
    }
}];

We're going to use plotly.plot() to initialize our graph and plotly.stream() to pipe our data.

Wrapping it up

By now you've got your Arduino wired up to the airquality sensor, you've got it set up for internet connectivity, you've uploaded your sketch, and you're ready to go.

Start streaming! Plug in your Arduino, and go check out your live results!