Building a PM 2.5 dust sensor with Arduino and SDS011

Anya A.
2 min readMar 22, 2021

What you need:

  1. 1 x An Arduino board
  2. 1 x Beadboard
  3. 1 x LED

Libraries can be download on this link: https://my.pcloud.com/publink/show?code=XZ2WdMkZi4NSB0cwO5hUeE8YkAfwNzACfowX

PM 2.5 Sensor
Circuit wiring
Circuit Wiring

Code for Arduino IDE

  1. Open Arduino IDE > install all required libraries > write down the following code:
#include <Wire.h>
#include <LCD.h>
#include <LiquidCrystal_I2C.h>
#include "SdsDustSensor.h"
#define I2C_ADDR 0x3F //
#define BACKLIGHT_PIN 3
int rxPin = 10;
int txPin = 11;
SdsDustSensor sds(rxPin, txPin);
LiquidCrystal_I2C lcd(I2C_ADDR,2,1,0,4,5,6,7);void setup()
{
Serial.begin(9600);
sds.begin();
lcd.begin (16,2);
// Switch on the backlight
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
}
void loop()
{
PmResult pm = sds.readPm();
int AQI;
String state;
if (pm.isOk()) {
Serial.print("PM2.5 = ");
Serial.print(pm.pm25);
Serial.print(", PM10 = ");
Serial.println(pm.pm10);
lcd.home ();
lcd.setCursor(0,0);
lcd.print("PM 2.5:");
lcd.setCursor(8,0);
lcd.print(pm.pm25);
lcd.print(" ");
lcd.setCursor(0,1);
getAQI(pm.pm25, &AQI, &state);
lcd.print("AQI:");
lcd.setCursor(4,1);
lcd.print(AQI);
Serial.print(AQI);
lcd.print(" ");
lcd.setCursor(8,1);
lcd.print(state);
// if you want to just print the measured values, you can use toString() method as well
Serial.println(pm.toString());
} else {
Serial.print("Could not read values from sensor, reason: ");
Serial.println(pm.statusToString());
}
delay(500);
}
void getAQI(float pm25, int* aqi, String* state){
// aqi = Ilow + (C-Clow)*(Ihigh-Ilow)/(Chigh-Clow) with I=aqi and C=concentration
if(pm25 < 12){
*aqi = 0 + (pm25-0)*(50.0-0)/(12-0);
*state = "Good";
}else if(pm25 < 35){
*aqi = 51 + (pm25-13)*(100.0-51)/(35-13);
*state = "Moderate";
}else if(pm25 < 55){
*aqi = 101 + (pm25-36)*(150.0-101)/(55-36);
*state = "Beware";
}else if(pm25 < 150){
*aqi = 151 + (pm25-56)*(200.0-151)/(150-56);
*state = "Unhealthy";
}else if(pm25 < 250){
*aqi = 201 + (pm25-151)*(300.0-201)/(250-151);
*state = "Very UH";
}else{
*aqi = 300 + (pm25-250)*(500.0-300)/(500-250);
*state = "Harzard";
}
}

2. Before compiling the code, choose board type and specify the port

Screenshot of Code Compilation

Here is how to convert AQI to microgram per ug/m3 for PM2.5 particle

--

--