Categories
Assignments

Lab 4. Processing sensor data

You’ve worked with several sensors so far in this class. Now, in this lab, you’ll learn more about how to access and process sensor data. First, you’ll read about an Arduino-compatible air quality sensor. Then, you’ll use the serial monitor to view and save incoming data. Finally, you’ll plot your data in Excel (or you can use R if you prefer*).

Background

Citizen air quality monitoring has soared in popularity alongside the proliferation of Do-It-Yourself (DIY) sensing devices. There are good reasons: (1) air quality monitoring is approachable with available sensors; (2) many existing air quality monitoring stations—often located outside of residential areas—may not adequately reflect the pollution urban residents experience in their neighborhood.

DIY air quality monitoring has the potential to reveal neighborhood-scale pollution exposure far more clearly than regional air quality monitoring. The challenge, however, is that many low-cost sensors are not as accurate as research-grade equipment. Some are not sensitive, which means that small but significant changes in air quality may not be adequately captured by the sensors. Some do not properly measure the absolute value of the pollutant concentration. But, considering the cost of research-grade equipment, many of these sensors do fairly well.

In this class, we will examine a particulate matter sensor called Grove (by SeeedStudio), or Shinyei PPD42NS. In the literature, it is usually referred to as the latter. Please read this nice, accessible overview on low-cost particulate matter sensors (the last example is the Shinyei PPD42NS sensor). Other reports on the Shinyei’s performance, which you may be interested in skimming, are available here:  EPA, Kelly et al, Austin et al, Wang et al. Another useful paper on low-cost air quality sensing (which doesn’t include the Shinyei PPD42NS) is available here: Williams et al.

We will continue to talk more about this sensor, its performance, and how we can use it over the next few weeks.

Assignment

1. How does the particulate matter sensor work? (1 point) This is a very useful deconstruction (commonly called a “tear down”) of the sensor, where it is opened up and you can see what’s happening inside.

2. Read the datasheet. You can find a link to the datasheet on the mouser website, where I bought the sensors from. In the data sheet, you will learn how to use your sensor. You can find sample code and information on how to wire the board (page 6).

You can also find code on how to use the sensor on GitHub. It is approximately the same code as you find in the datasheet (a couple tweaks to the pin assignment and print statements). Note that some sensors only have code available on GitHub and not in the datasheet.

For more help, here’s a link to a YouTube video on how to use the sensor. It’s a little weird—there’s no audio and it uses a cigarette to demonstrate changes in air quality (smoking is bad, folks).

3. Set up your sensor and upload the code. Open the serial monitor and watch it log data. It will take a little over 30 seconds for the first print statement to appear.

Note:

  • The sensor takes a long time to warm up (the datasheet says 3 minutes). This means you should disregard at least the first 3 minutes of data.
  • The sensor is meant to be used in an upright position (it is inconvenient that the wire feeds are at the bottom of the board). Put it in this position and others. Watch how the readings change.
  • The sensor works best in a dark environment, since it is uses a led to sense dust. Can you tell a difference in the readings when used in light versus dark? Some people build enclosures to protect their dust sensors and darken its environment, but be conscious of how this could limit air flow.
  • If you are getting a reading of 0.62, why is that? Do you think there is anything you could do to change that? (This could be an indication of the sensor not working, but I tested them all this week and I doubt they’ve broken since.)

4. Close your serial monitor and go to Tools > Serial Plotter. Go do something else for a little while (e.g., 10-15 minutes). Come back and look at your graph. You’ll find the data are really noisy.

5. To smooth the noise, it would be ideal to take many data points and average them. This is sometimes called a “moving window.” Amend the code to print a concentration value that is the average of the last 4 data points. In other words: continue to print a new concentration value to the serial monitor every 30 seconds (as the sketch currently does), but this value should be the average of the current reading + the previous 3 readings. (2 points)

  • Tip 1.  There are several ways to do this and you can approach it as you’d like, but I would use an array to store the sensed concentrations. You could manage the array with a loop and indexing to step through the length of the array, but for this application it may be easier not to. In fact, to keep things simple, I recommend you don’t create a loop. But, note that if you wanted to be able to easily change the width of your moving window, it would be nice to put it in a loop. If you want to take this on, I suggest getting the program running with hard coded array values, and then updating the code to accommodate a variable-length array.
  • Tip 2. Review the available math functions on the Arduino reference page. It’s likely easiest to use these, rather than including a library with a mean or average function.

6. Amend the code’s output so we can easily use it to create a graph of particulate matter concentration versus time. (2 points)

Print to the serial monitor 2 columns of data separated by a tab. (This is called “tab-delimited.” You could also use a comma if you wanted to create a CSV.) The first column should be time. You can use the millis function (as I do in the example below), but note that there are also other time functions that produce nicer output. Between the two columns insert a tab (you can find how to do that here). In the second column, print the concentration. For now, don’t worry about putting labels or headers in your dataset.

Your output will look something like:

7. Leave the sensor running for several hours using your modified sketch (overnight would be best). Keep the serial monitor open.

Tip: you may need to change your computer’s power settings to ensure that your hard drive doesn’t shut off while you’re running your sketch. On my PC, I go to Settings > Power & sleep, then under “When plugged in PC goes to sleep after” I choose “Never” from the drop down menu.

8. When you get back to your computer, select all the data in the serial monitor, then copy and paste it into Excel.* You can do this by single-clicking on the A1 cell and hitting paste. Excel will know to separate all the values into different cells.

9. Once you’re in Excel, add labels to columns: Time (msec) and Concentration (pcs/0.01cf). Insert a column to the right of your time column. Label this Time (min). Convert the time variable that’s in milliseconds to minutes.

10. Create a “Scatter with Smooth Lines” plot that shows Concentration (pcs/0.01cf) versus Time (min). Concentration should be on the y axis. Time should be on the x axis. Label your axes. Give your plot a name. Save your plot as a PDF. (2 points)

11. Look at your plot. In 1-2 paragraphs describe: what do you think of the pattern of air pollutants? Does it seem reasonable to you? If there are any spikes or anomalies in your data, what do you think caused them? (1 point)

12. You smoothed your data within your code. Another option could be taking the raw data, and then smoothing it later. What is one advantage and disadvantage of each approach? (2 point)

13. Please submit your  answers to the questions, your code, and your plot to Canvas.

*There seem to be many different ways to interface R and Arduino. I’ve never tried any. If you get Arduino/R working, let me know! Note that even if you don’t use any direct interface with Arduino, you can still do data plotting in R. Here are some options for directly interfacing with the Arduino:

  • This tutorial, “How to send and receive data and take control of your Arduino from R” accesses the Arduino outputs using the Serial package.
  • This R package, cutely named Rduino, is tailor-made for Arduino.
  • There is another intriguing R package available here, but I believe it only works on Mac and Linux and doesn’t seem to have been updated in a while.

Leave a Reply

Your email address will not be published. Required fields are marked *