DIY Home PM2.5 Detector
Hello, everyone. Today we want to make a monitor to detect PM2.5 content in the air. We are more and more concerned about the impact of the environment on ourselves. I believe that with this, you can also know whether the environment you live in will affect your health!
Supplies
- Arduino Uno
- 0.91" SSD1306 OLED
- Makerfabs PM2.5 Sensor
The structure and circuit of the infrared principle PM2.5 sensor are relatively simple. The light source is an infrared LED light source. The air inlet and outlet mainly rely on resistance to generate heat to obtain hot air flow. When particles pass through, reflection occurs after touching the LED light source. The photosensitive detector receives reflected light of different intensities, and then through amplification processing and gets the detection result after operation.
The structure and circuit of the laser principle PM2.5 sensor are relatively complicated. Its light source is a laser diode. The sampled air is pushed by a fan or blower and passed through a complex design of the air duct for detection. When the fine particles in the air enter the area where the laser beam is located, the laser light will be scattered; the scattered light radiates 360° in space, and a photodetector is placed at an appropriate position to receive the scattered light, which is then generated by the photoelectric effect of the photodetector. After the current signal is amplified and processed by the operational amplifier circuit, the fine particle concentration value can be obtained. The output signal can be used in various ways such as serial port, I²C, PWM, etc.
This PM2.5 sensor has the following advantages:
High-precision detection: The PM2.5 sensor adopts advanced laser scattering technology, which can accurately detect tiny particulate matter in the air, including PM1, PM2.5, and PM10.
Easy to use: The PM2.5 sensor is small in size and easy to operate, just place it in a suitable place indoors or outdoors to start monitoring.
Hardware Connection
In the PM2.5 datasheet, We can Check the PIN definition to connect the Arduino Uno, the pin5 is in Serial sending mode, so it can connect the digital pin2. The power cable is connected to 3.3V, GND to GND. The SSD1306, can use the SDA to connect the A4, and the SCL to connect the A5. The power cable is connected to 3.3V/5V with Arduino Uno.
Software
The project code is placed on GitHub.
In this program, we need to use software to simulate the serial port to receive the data from the sensor, and then the processor analyzes the read information through the protocol file, and then displays the parsed data on the OLED screen, because the screen is small, showing the most important PM1, PM2.5 and PM10 data.
The 32-bit byte data generated by the sensor is sent to the MCU through the serial port. We need an unsigned 8-bit integer variable to store this data.
if (mySerial.available() >= 32) { // If there are 32 bytes available to read in the buffer
uint8_t buffer[32];//A variable named "buffer" is defined, which is an unsigned array of 8-bit integers of 32 bytes (256 bits) in length.
mySerial.readBytes(buffer, 32); // If there are 32 bytes available to read in the buffer
In the protocol file, we know that the first two digits are the start digit check digit, and we need to check whether the data is correct.
// Check if the start code is correct
if (buffer[0] == 0x42 && buffer[1] == 0x4d) {
// Calculate the frame length
uint16_t frame_length = ((uint16_t)buffer[2] << 8) | buffer[3];
if (frame_length == 28) { // Check if the frame length is correct
Calculating the length of the frame is also to ensure accurate data analysis, if the frame length is 28 bits, the analysis of measurement data can be performed.
if (frame_length == 28) { // Check if the frame length is correct
// Parse the data
uint16_t pm1_0_standard = ((uint16_t)buffer[4] << 8) | buffer[5]; // Extract the value of standard particle matter PM1.0 using the 5th and 6th bytes. By parity of reasoning
uint16_t pm2_5_standard = ((uint16_t)buffer[6] << 8) | buffer[7];
uint16_t pm10_standard = ((uint16_t)buffer[8] << 8) | buffer[9];
uint16_t pm1_0_atmosphere = ((uint16_t)buffer[10] << 8) | buffer[11];
uint16_t pm2_5_atmosphere = ((uint16_t)buffer[12] << 8) | buffer[13];
uint16_t pm10_atmosphere = ((uint16_t)buffer[14] << 8) | buffer[15];
uint16_t particles_0_3 = ((uint16_t)buffer[16] << 8) | buffer[17];
uint16_t particles_0_5 = ((uint16_t)buffer[18] << 8) | buffer[19];
uint16_t particles_1_0 = ((uint16_t)buffer[20] << 8) | buffer[21];
uint16_t particles_2_5 = ((uint16_t)buffer[22] << 8) | buffer[23];
uint16_t particles_5_0 = ((uint16_t)buffer[24] << 8) | buffer[25];
uint16_t particles_10 = ((uint16_t)buffer[26] << 8) | buffer[27];
uint16_t checksum = ((uint16_t)buffer[30] << 8) | buffer[31];
}
Calculate the data checksum and verify that all data has been traversed. If the verification is accurate, you can use the serial port to print out the measurement data such as PM2.5.
if ((sum & 0xffff) == checksum) { //by parity of reasoning
Serial.print("PM1.0(μg/m³): ");
Serial.println(pm1_0_standard);
Serial.print("PM2.5(μg/m³): ");
Serial.println(pm2_5_standard);
Serial.print("PM10(μg/m³): ");
Serial.println(pm10_standard);
Serial.print("PM1.0 in atmosphere(μg/m³): ");
Serial.println(pm1_0_atmosphere);
Serial.print("PM2.5 in atmosphere(μg/m³): ");
Serial.println(pm2_5_atmosphere);
Serial.print("PM10 in atmosphere(μg/m³): ");
Serial.println(pm10_atmosphere);
Serial.print("Particles >0.3μm/0.1L: ");
Serial.println(particles_0_3);
Serial.print("Particles >0.5μm/0.1L: ");
Serial.println(particles_0_5);
Serial.print("Particles >1.0μm/0.1L: ");
Serial.println(particles_1_0);
Serial.print("Particles >2.5μm/0.1L: ");
Serial.println(particles_2_5);
Serial.print("Particles >5.0μm/0.1L: ");
Serial.println(particles_5_0);
Serial.print("Particles >10μm/0.1L: ");
Serial.println(particles_10);
}
Using the Adafruit SSD1306 library, call the display function to display the measurement data.
display.clearDisplay(); // clear the display
display.setTextSize(1); // Set text size
display.setTextColor(WHITE); // Set text color
display.setCursor(0, 0); // Set cursor position
display.print("PM1.0: "); // Display PM1.0 data
display.print(pm1_0_standard);
display.println("ug/m3");
display.setCursor(0, 10); // Set cursor position
display.print("PM2.5: "); // Display PM2.5 data
display.print(pm2_5_standard);
display.println("ug/m3");
display.setCursor(0, 20); // Set cursor position
display.print("PM10: "); // Display PM10 data
display.print(pm10_standard);
display.println("ug/m3");
display.display(); // Update screen display
Downloads
Result
As can be seen from the picture, the PM2.5 content detected by our purchased household air freshener is only slightly different from the data measured by our detector (within the acceptable error range), and when We put the sensor by the window near the road, and we can clearly feel the increase of PM2.5 content in the air. On the road with more dust, the content of PM2.5 also increased again. Therefore, we can check the PM2.5 air comparison table by measuring the value of PM2.5, and we can know whether the place where we live will affect our health and safety.
If you also want to DIY a similar PM2.5 detector, you can follow my tutorial and start to monitor the environmental pollution level at any time. This feeling is really wonderful!