A Step-by-Step Guide to Building a PM2.5 Detector

In recent days, friends from the south have been coming to Beijing to experience the haze, and many have expressed that although the haze in the south is moist and elegant, it cannot compare to the richness and depth of Beijing’s haze. I have also heard that the haze in Hebei is rough and bold, but it lacks a bit of cultural depth.

The haze must be appreciated for its accumulation; the old haze of Beijing is indeed thicker than the new haze of the south, soft as it enters the nose, yet rich and mellow, with a slight aftertaste upon careful savoring.

Fog is thick in the hometown, while haze is pure in Beijing.

Since haze has become the norm, it is quite necessary to DIY a PM2.5 detector. Using the beginner-friendly Arduino UNO R3 development board, you can create a PM2.5 detector with a very simple circuit and minimal code. To ensure measurement accuracy, we choose a laser dust sensor to test the level of haze pollution in the air, while using the DHT11 to detect the ambient temperature and humidity, making it very suitable for middle and primary school students to practice.

1. Materials Needed to Make a PM2.5 Detector

  • One Arduino UNO R3 development board

  • One laser PM2.5 sensor

  • One 1.3-inch OLED screen

  • One DHT11 temperature and humidity sensor

  • One breadboard

  • Several Dupont wires

  • One resistor with a resistance of about 5k

A Step-by-Step Guide to Building a PM2.5 Detector

All materials needed to make the PM2.5 detector

2. Component Introduction

  1. Arduino UNO R3 Development Board

    Arduino is an open-source design platform consisting of hardware Arduino boards and software Arduino IDE integrated development environment. The Arduino board is an open-source hardware platform based on the AVR microcontroller, with various models and supporting resources. The most basic and commonly used is the Arduino UNO R3 board, which is inexpensive, priced at about 23 yuan, compact, and easy to use. The Arduino IDE is an excellent software development platform with a simple and friendly interface, quick to get started, and has good cross-platform compatibility.

    A Step-by-Step Guide to Building a PM2.5 Detector

    Arduino UNO R3 Board

    Arduino programming is based on entry-level C language, with a low threshold. The most important thing is that in the Arduino environment, many functions have been encapsulated into functions that can be called directly, greatly reducing programming complexity. Students do not need to have too much microcontroller knowledge to connect the Arduino board with various sensors and electronic components, quickly building various interesting and practical circuits to achieve rich functions. Arduino also supports third-party libraries and hardware, making it flexible and extensible.

    A Step-by-Step Guide to Building a PM2.5 Detector

    Arduino board ports and pin function definitions

  2. Laser PM2.5 Sensor

    To ensure testing accuracy, a laser PM2.5 sensor is selected. The downside is that the price is quite outrageous; this thing is not expensive to produce, but due to a lack of competition, the price has remained high.

    This sensor has 6 pins, but in fact, only 4 are used, and the pin definitions are as follows.

    A Step-by-Step Guide to Building a PM2.5 Detector

    Sensor manual download https://pan.baidu.com/s/1hsPyn2G

    The sensor uses asynchronous serial communication (UART) and communicates in frames. The frame format is fixed, with each frame consisting of 9 bytes, divided into command frames and response frames. When an external device sends a command frame to the sensor, the sensor will reply with the corresponding response frame.

    Baud rate: 9600;

    Parity: None;

    Stop bits: 1;

    Data bits: 8;

    A Step-by-Step Guide to Building a PM2.5 Detector

  3. OLED Screen

    The OLED display uses organic light-emitting diodes and does not require a backlight, making it a newer type of display. The downside is that it is more expensive and does not last as long as LCDs.

    Organic light-emitting display technology consists of a very thin organic material coating and a glass substrate. When an electric charge passes through, these organic materials emit light. The color of the OLED light emission depends on the materials of the organic light-emitting layer, so manufacturers can achieve the desired color by changing the materials of the light-emitting layer. Active matrix organic light-emitting displays have built-in electronic circuit systems, so each pixel is driven independently by a corresponding circuit. OLEDs have advantages such as simple structure, self-emission without backlight, high contrast, thin thickness, wide viewing angles, fast response speed, suitability for flexible panels, and a wide temperature range.

    This example uses a 128×64 1.3-inch monochrome OLED display with IIC interface, with a reference price of about 27 yuan.

    A Step-by-Step Guide to Building a PM2.5 Detector

    When wiring, be sure to carefully check the power and ground lines to avoid reversing them.

  4. Temperature and Humidity Sensor

    DHT11 is a cheap temperature and humidity sensor that uses a humidity-sensitive resistor to measure humidity and a digital temperature sensor to test temperature, processed by a microcontroller and output serially. The DHT11 is priced at about 4 yuan but is somewhat ugly. The DHT22 is priced at about 15 yuan, and although it is whiter, it still lacks aesthetic appeal. Both have the same pin definitions, but the timing of operation is different, which needs to be noted during application. Here, we choose the DHT11 model.

    A Step-by-Step Guide to Building a PM2.5 Detector

    The hardware connection of the DHT11 is very simple, as shown in the schematic below:

    A Step-by-Step Guide to Building a PM2.5 Detector

3. Software Environment Configuration

First-time use of Arduino UNO R3

Download

Download the integrated development environment ARDUINO 1.8.3 from the official website

https://www.arduino.cc/en/Main/Software

Install

Double-click arduino-1.8.3-windows.exe, follow the installation prompts, and proceed with the default installation. After installation, an Arduino software logo will appear on the desktop, which can be double-clicked for future use.

Configure the Development Environment

Insert the Arduino UNO R3 development board, and the system will automatically install the USB driver.

Select the Appropriate Board

A Step-by-Step Guide to Building a PM2.5 Detector

Select the Port

You can check the corresponding port of the device through the device manager; this machine corresponds to COM5.

A Step-by-Step Guide to Building a PM2.5 Detector

Verification

Open the file, examples, and find the Blink program

A Step-by-Step Guide to Building a PM2.5 Detector

Click the checkmark to verify if the program can compile successfully; if everything is normal, click the right arrow to upload the compiled program to the AVR microcontroller of the Arduino UNO R3. You can also directly click that arrow; the IDE will first perform verification, and if it passes, it will upload directly.

A Step-by-Step Guide to Building a PM2.5 Detector

If you see the LED light flashing, it indicates that everything is normal. Congratulations, installation successful, and the board is working properly.

Add Third-Party Libraries

Start the Arduino IDE

Project -> Load Library -> Manage Libraries, search for DHT, and install the DHT sensor library to support DHT11

Project -> Load Library -> Manage Libraries, search for U8GLIB, and install U8GLIB to support OLED

4. Start Practicing

Operating the Temperature and Humidity Sensor

1. First, connect the power and ground from the Arduino UNO R3 board to the breadboard

A Step-by-Step Guide to Building a PM2.5 Detector

2. Connect the temperature and humidity sensor properly, where the sensor’s data output SDA connects to the A0 port of the Arduino board. The connection method for temperature and humidity is very simple, as shown in the schematic earlier in this article; besides power and ground, only one SDA line is pulled up to 5V through a 5-10k resistor and connected to the A0 port of the Arduino board.

A Step-by-Step Guide to Building a PM2.5 Detector

3. Load the example program

File -> Examples -> DHT sensor library -> DHTtester

4. Modify the data input port

#define DHTPIN A0 // what digital pin we’re connected to

5. Modify the example to support DHT11, removing the // in front of that line

#define DHTTYPE DHT11 // DHT 11

6. Upload the program, open the serial monitor, set the baud rate to 9600, and observe the ambient temperature and humidity.

Operating the OLED Screen

1. Disconnect the power from the Arduino UNO R3 board

2. Connect the OLED screen properly; since it is an IIC interface, the circuit connection is very simple. Besides connecting the power and ground, just connect SCL to the A5 pin of the Arduino board and SDA to the A4 pin.

A Step-by-Step Guide to Building a PM2.5 Detector

3. Load the example program

File -> Examples -> U8glib -> helloworld

4. Modify the example to support OLED, removing the // in front of that line

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE);// I2C / TWI

5. Upload the program and check if the OLED screen displays correctly.

Operating the PM2.5 Sensor

This sensor is a laser digital PM2.5 sensor, with a built-in laser and photoelectric receiving component, using the principle of light scattering. The laser generates scattered light on particles, which is converted into an electrical signal by the photoelectric receiver, and then the PM2.5 and PM10 values are calculated using a specific algorithm.

The sensor uses serial output; besides connecting pin 1 to ground and pin 2 to power, connect pin 3 TXD and pin 4 RXD to the D5 and D6 ports of the Arduino board, respectively. Of course, you can also connect them to other ports as long as the program is modified accordingly.

Common commands only require three: the power-on command, the command to read the PM2.5 value, and the power-off command. You might as well try writing them according to the manual; for simplicity, you can display the results using the serial monitor.

5. Write the Program to Realize the PM2.5 Detector

Since the hardware circuit has been gradually connected, we can now write a program for the entire circuit, unifying the functions of each part. The software example code is as follows:

#include <DHT.h>

#include “U8glib.h”

#include <SoftwareSerial.h>

// Note: This is just a simple example program aimed at helping beginners get started

#define DHTPIN A0 // Connect to the data pin of the temperature and humidity sensor

// Choose the appropriate temperature and humidity sensor model

#define DHTTYPE DHT11 // DHT 11

//#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321

uint8_t PM25_OPEN[] = {0xAA,0x01,0x00,0x00,0x00,0x00,0x01,0x66,0xBB}; // Power-on command

uint8_t PM25_CLOSE[] = {0xAA,0x03,0x00,0x00,0x00,0x00,0x01,0x68,0xBB}; // Power-off command

uint8_t PM25_DATA[] = {0xAA,0x02,0x00,0x00,0x00,0x00,0x01,0x67,0xBB}; // Read data command

DHT dht(DHTPIN, DHTTYPE);

// Software serial definition format SoftwareSerial(rxPin, txPin, inverse_logic)

SoftwareSerial mySerial(5, 6); // Define software serial, D5 and D6 connect to the PM2.5 sensor’s 3(TXD) and 4(RXD)

U8GLIB_SH1106_128X64 u8g(U8G_I2C_OPT_NONE); // I2C / TWI OLED screen definition

uint16_t pm25, pm10;

float h, t, hic;

void setup() {

mySerial.begin(9600); // Set software serial baud rate to match PM2.5 sensor

if ( u8g.getMode() == U8G_MODE_R3G3B2 ) {

u8g.setColorIndex(255); // white

}

else if ( u8g.getMode() == U8G_MODE_GRAY2BIT ) {

u8g.setColorIndex(3); // max intensity

}

else if ( u8g.getMode() == U8G_MODE_BW ) {

u8g.setColorIndex(1); // pixel on

}

else if ( u8g.getMode() == U8G_MODE_HICOLOR ) {

u8g.setHiColorByRGB(255,255,255);

}

pmOn();

delay(2000);

}

int i=0;

void loop() {

u8g.firstPage();

if (i==0) pmRead();

if(i>=5) i=0;else i++;

dhtRead();

do {

draw();

} while( u8g.nextPage() );

delay(500);

}

void pmOn(){

mySerial.write(PM25_OPEN,9);

}

void pmOff(){

mySerial.write(PM25_CLOSE,9);

}

void pmRead(){

uint8_t data[9];

//while(mySerial.available());

mySerial.write(PM25_DATA,9);

delay(100);

for(int i=0;i<9;i++){

if (mySerial.available()) {

data[i] = mySerial.read();

}

}

if(data[0]==0xAA && data[8]== 0xBB){

pm25 = data[4]*256 + data[5];

pm10 = data[2]*256 + data[3];

}

}

void dhtRead(){

// Reading temperature or humidity takes about 250 milliseconds!

// Sensor readings may also be up to 2 seconds ‘old’ (its a very slow sensor)

h = dht.readHumidity();

// Read temperature as Celsius (the default)

t = dht.readTemperature();

// Compute heat index in Celsius (isFahreheit = false)

hic = dht.computeHeatIndex(t, h, false);

}

void draw(void) {

// graphic commands to redraw the complete screen should be placed here

char sendBuff[20];

u8g.setFont(u8g_font_unifont_78_79);

sprintf(sendBuff,”%c”,14);

u8g.drawStr( 0, 18, sendBuff);

u8g.setFont(u8g_font_unifont);

sprintf(sendBuff,”H:%2d%% T:%2d%cC”,(int)h,(int)t,0xB0);

u8g.drawStr( 20, 18, sendBuff);

u8g.setFont(u8g_font_profont22);

sprintf(sendBuff,”PM2.5:%4d”,pm25);

u8g.drawStr( 6, 42, sendBuff);

sprintf(sendBuff,”PM 10:%4d”,pm10);

u8g.drawStr( 6, 64, sendBuff);

}

Running Result

A Step-by-Step Guide to Building a PM2.5 Detector

6. The Spirit of Craftsmanship

To make this PM2.5 detector more practical, you can also create printed circuit boards. To facilitate beginners, the main control chip can be the same CPU as the Arduino UNO R3 board.

A Step-by-Step Guide to Building a PM2.5 Detector

A Step-by-Step Guide to Building a PM2.5 Detector

A Step-by-Step Guide to Building a PM2.5 Detector

Students participating in this production: As long as you succeed in making it, you will receive a mysterious little gift, which is the work shown by the sister below. Can you guess what it is?

A Step-by-Step Guide to Building a PM2.5 Detector

A Step-by-Step Guide to Building a PM2.5 Detector

By the way, this little gift just won second place in the “2017 National Electrical Engineering and Electronics Basic Course Experimental Teaching Case Design Competition” hosted by the Teaching Guidance Committee of the Ministry of Education. The sister is one of the authors.

A Step-by-Step Guide to Building a PM2.5 Detector

Leave a Comment

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