SDS011 dust monitor on an Arduino Uno
An SDS011 laser dust sensor on the Uno’s SoftwareSerial in query mode: the SdsDustSensor library asks for PM2.5 and PM10 every 3 seconds and grades the air. The whole build, an Arduino Uno and 1 more part, runs here in your browser on the firmware below; open it in the editor to change the wiring or the code and run it again.
Teaching? Assign it to your class in one click.
Beginner About 10 minutes to build and run. Runs in your browser. Free, and no account needed.
The parts retail for about $52. On Mokxi they're free.
What you'll learn
- Sensors
- School
- Using the Dust sensor
- Difficulty: Beginner
- Time: About 10 minutes
- Board: Arduino Uno R3
- Parts: 3
How it works
An SDS011 laser dust sensor on the Uno’s SoftwareSerial in query mode: the SdsDustSensor library asks for PM2.5 and PM10 every 3 seconds and grades the air.
Arduino Uno R3 runs the sketch below. The circuit around it: Dust sensor, SDS011. delay waits 3000 milliseconds. Serial.begin opens the serial port at 115200 baud.
Parts list
3 parts, plus the jumper wires. They retail for about $52; on Mokxi they're free.
Wiring table
Every connection, pin by pin, read from the circuit itself. Each row joins the pin on the left to the pin on the right, by a jumper wire or a breadboard strip.
| Part and pin | Connects to |
|---|---|
| Dust sensor, SDS011 pin GND | Arduino Uno R3 pin GND |
| Dust sensor, SDS011 pin RX | Arduino Uno R3 pin 3 |
| Dust sensor, SDS011 pin TX | Arduino Uno R3 pin 2 |
| Dust sensor, SDS011 pin 5V | 5 V |
The code
The firmware exactly as the editor opens it. Change a line there and press Run: it compiles in the browser.
// SDS011 dust monitor: a Nova Fitness laser dust sensor on SoftwareSerial.
//
// SDS011 TX -> pin 2 SDS011 RX <- pin 3 5V -> 5 V GND -> GND
//
// The sensor's fan pulls air past a laser, and it counts the light the dust
// scatters. Out of the box it sends PM2.5 and PM10 once a second; here it is
// put in query mode, so it sends a reading only when asked, every 3 seconds
// (the manual asks for no faster). The SdsDustSensor library builds the
// 19 byte commands and checks the 10 byte answers.
//
// The grade is the US EPA's 24 hour PM2.5 bands: Good up to 12, Moderate up
// to 35, "Unhealthy SG" (sensitive groups) up to 55, Unhealthy over that.
//
// Drag the PM2.5 slider on the sensor and watch the grade change.
#include "SdsDustSensor.h"
SdsDustSensor sds(2, 3); // RX (the sensor's TX), TX (the sensor's RX)
const char *grade(float pm25) {
if (pm25 <= 12) return "Good";
if (pm25 <= 35) return "Moderate";
if (pm25 <= 55) return "Unhealthy SG";
return "Unhealthy";
}
void setup() {
Serial.begin(115200);
sds.begin();
Serial.println("SDS011 dust monitor");
Serial.println(sds.queryFirmwareVersion().toString());
Serial.println(sds.setQueryReportingMode().toString());
}
void loop() {
PmResult pm = sds.queryPm();
if (pm.isOk()) {
Serial.print("PM2.5 ");
Serial.print(pm.pm25, 1);
Serial.print(" ug/m3, PM10 ");
Serial.print(pm.pm10, 1);
Serial.print(" ug/m3, ");
Serial.println(grade(pm.pm25));
} else {
Serial.print("Could not read the sensor: ");
Serial.println(pm.statusToString());
}
delay(3000);
}
Code walkthrough
The key functions of the sketch, and what each one does.
setup sketch.ino
Runs once when the board starts. It gets the pins and libraries ready.
void setup() {
Serial.begin(115200);
sds.begin();
Serial.println("SDS011 dust monitor");
Serial.println(sds.queryFirmwareVersion().toString());
Serial.println(sds.setQueryReportingMode().toString());
}loop sketch.ino
Runs over and over for as long as the board has power. This is where the project does its work.
void loop() {
PmResult pm = sds.queryPm();
if (pm.isOk()) {
Serial.print("PM2.5 ");
Serial.print(pm.pm25, 1);
Serial.print(" ug/m3, PM10 ");
Serial.print(pm.pm10, 1);
Serial.print(" ug/m3, ");
Serial.println(grade(pm.pm25));
} else {
Serial.print("Could not read the sensor: ");
Serial.println(pm.statusToString());
}
delay(3000);
}Troubleshooting
The usual mistakes with this circuit, and the fix for each.
- Upload fails or nothing runs after wiring pins 0 and 1 to a module. Arduino Uno R3
Pins 0 and 1 are the USB serial port. Move the module to other pins, or unplug it while uploading. Also check the right board is selected. - Pins misbehave or the board resets when you power motors or servos from it. Arduino Uno R3
Each I/O pin gives about 20 mA, 40 mA max. Power motors and servos from their own supply and join its ground to the Uno's GND.
Change it and keep it
Open it in the editor, change the circuit or the code, and keep your version in a free account.