How to Interface Winsen ZH06 Laser Dust Sensor with Arduino
Environmental monitoring is one of the easiest DIY projects you can undertake, thanks to the availability of compact, low-power, and affordable sensors. These sensors include temperature sensors, pressure sensors, humidity sensors, ambient light sensors, gas sensors, and more. Among these, air quality sensors are particularly important when it comes to monitoring the air you breathe. Air quality is influenced by various factors, including the composition of gases and the type and quantity of particles in the air. In this tutorial, we will explore how a low-cost air quality sensor can provide valuable insights into the air you breathe. You will learn about the working principle of the sensor and how to seamlessly interface it with your favourite Arduino boards.
Particulate Matter
Particulate Matter (PM) are microscopic particles of solid or liquid matter suspended in the air. The particles and the air together is called the aerosol. PM is commonly specified by their size in micrometer (um), since they are very small. The following illustration shows the sizes of commonly found particles including biological bacteria and viruses.
The amount PM in the air we breath is extremely important to a healthier life. Since the particles are very small, they can deeply penetrate through our lungs and reach all internal parts of our body through the bloodstream. Such foreign materials in the bloodstream are almost always harmful to our health in some way and can lead to severe lifestyle diseases like heart disease, lung disease and cancer which lead to premature deaths ☠️. So it is very important to make sure that the air we breath are free from are free from harmful PM. The easiest way to achieve this is to keep our surroundings clean by keeping the sources of PM to a minimum. The next step is to avoid exposure to PM contaminated air by using respirators (not masks) and powered air purifiers. Most commercial air purifiers you can buy today include a special sensor to measure the density of PM in the air. The density of PM is specified in microgram per cubic meter (ug/m3). The densities of PM of size in the range of 1 um, 2.5 um and 10 um are the most widely used metrics to indicate the quality of air in terms of PM densities. These are called PM1.0, PM2.5 and PM10 respectively. The following chart shows the safe and hazardous levels of PM densities.
Today, we are introducing you a PM density sensor called ZH06 from a company called Winsen. Winsen makes gas and air quality sensors. The ZH06 uses a laser to measure the densities of PM1.0, PM2.5 and PM10 in the air. You can interface this sensor easily with any Arduino boards and make an air quality monitoring device for your own use. We will show you everything you need to know about the sensor and the code.
Winsen ZH06
The Winsen ZH06 is a laser dust sensor that can measure PM1.0, PM2.5 and PM10 densities. You can buy this sensor for around INR 1.7K from places like Robu. It is a compact and self contained sensor with serial data output and PWM output. So it is easy to interface it with any of your microcontroller boards.
Specifications
Specification | Value |
---|---|
Model | ZH06/ZH06-I |
Detection Range | 0.3-10 um |
Effective Range | 0-1000 ug/m³ |
Detection Interval | 1 s |
PM2.5 Detection Accuracy | 0-100 ug/m³: ±15 ug/m³ 101-1000 ug/m³: ±15% Reading (Test Condition: 25 ±2°C, 50 ±10% RH, TSI8530, Cigarette, GBT18801-2015) |
Preheating Time | 30 s |
Output | UART TTL Output (3.3V level) |
PWM Output (3.3V level) | |
Working Voltage | 4.9~ 5.5 V (DC) |
Logical Voltage | 3.3V |
Working Current | <120 mA |
Dormancy Current | <20 mA |
Response Time | T90 < 45s |
Working Humidity | 0~80% RH (No Condensation) |
Working Temperature | -10~60 °C |
Storage Temperature | – 30~70 °C |
Dimensions | 47 x 37 x 12.2 mm (L x W x H) |
Weight | <30g |
MTTF | Continuous > 10000H |
Working Principle
The working principle of the laser-based dust or particulate density sensors is based on light scattering. When light passes through a medium that contains the particles, the light is scattered when the size of the particles is equal to or more than the wave length of the light itself. This is called Mie Scattering. A light source like laser is a good source of same wavelength and coherent light. The laser light is allowed to pass through a sealed chamber that sustains a continuous flow of ambient air with the help of a fan. A photodetector sensor is placed near the chamber to measure the intensity of the scattered light. When there is more amount of particles in the air, the intensity of the scattered light will be higher. Using special algorithms, these intensities can be approximated to the density of the particles in the air. A particulate sensor can be designed to measure the intensity of the scattered light or the unscattered light. In both cases, the intensities will indicate the density of the particulates in the air. Using different wavelengths, or using different algorithms, we can measure the densities of different sized particles. This process is illustrated below.
Pinout
Pin # | Name | Function |
---|---|---|
1 | VDD | Positive supply |
2 | GND | Negative supply |
3 | NC | Not connected |
4 | RXD | UART Receive pin |
5 | TXD | UART transmit pin |
6 | NC | Not connected |
7 | NC | Not connected |
8 | PWM | PWM output |
The sensor comes with an 8-pin, 1.25 mm Molex PicoBlade series SMD right-angle connector (Molex 532610871) on the sensor and a compatible double-sided cable (Molex 510210800).
Wiring
We are going to use a FireBeetle-ESP32E board from DFRobot for demonstrating the capabilities of the sensor. We just need to connect four wires from the sensor to the microcontroller board. Wire up the sensor according to the following table. If you are planning to use a different microcontroller board, you can wire it up accordingly.
ZH06 | FireBeetle-ESP32E |
---|---|
VDD | VCC |
GND | GND |
RXD | D11 (GPIO16 ) |
TXD | D10 (GPIO17 ) |
Since the sensor came with a double-sided cable, we had to cut one end and solder a 2.54 mm header pins in order to connect it to a breadboard.
CSE_ZH06 Library
To make it easy to interface the sensor with any Arduino-compatible boards, CIRCUITSTATE has made a custom library called CSE_ZH06 for the Arduino platform. This open-source library can be downloaded from our GitHub or directly installed from the Arduino Library Manager. The library is also available through PlatformIO.
CSE_ZH06 library supports both hardware serial ports and software serial ports. For boards with only one hardware UART port, you may want to use that for general debugging or programming the board. That means, we can not connect the sensor to that port. In such cases, we can use the SoftwareSerial library to use any free GPIO pins for the TX and RX functionalities. ESP32 has multiple hardware serial ports that can be mapped to almost all of the GPIO pins, giving more flexibility in the wiring. If you are using a different board, check if it has multiple hardware serial ports. Boards like Arduino Uno, Nano etc, only have single hardware serial ports that are used for programming. You need to use software serial ports with such boards.
Arduino Code
Following is the Read_PM_Data example included in the CSE_ZH06 library. This program reads the PM1.0, PM2.5 and PM10 densities and print the values to the serial monitor every second. We are using he Serial1
port of ESP32 for talking to the sensor while the default Serial
(Serial0
) is used for printing the values. The CSE_ZH06 library depends on the CSE_MillisTimer library, also from CIRCUITSTATE. Make sure to install it before compiling the example.
//======================================================================================//
/*
Filename: Read_PM_Data.ino
Description: Reads the PM data from the Winsen ZH06 laster dust sensor using the `CSE_ZH06` library.
Framework: Arduino, PlatformIO
Author: Vishnu Mohanan (@vishnumaiea, @vizmohanan)
Maintainer: CIRCUITSTATE Electronics (@circuitstate)
Version: 0.0.1
License: MIT
Source: https://github.com/CIRCUITSTATE/CSE_ZH06
Last Modified: +05:30 20:40:55 PM 23-12-2024, Monday
*/
//======================================================================================//
#include <Arduino.h>
#include <CSE_ZH06.h>
// Define the serial port pins here.
#define PIN_SERIAL_TX1 16
#define PIN_SERIAL_RX1 17
CSE_ZH06 pmSensor (Serial1); // Create a CSE_ZH06 object to access the sensor
// Setup runs once.
void setup() {
Serial.begin (115200); // Initialize the debug serial port
Serial1.begin (9600, SERIAL_8N1, PIN_SERIAL_RX1, PIN_SERIAL_TX1); // Initialize the sensor serial port
pmSensor.begin(); // Initialize the sensor
}
// Inifinite loop.
void loop() {
if (pmSensor.getPmData()) {
Serial.println ("Data received. Printing data..");
Serial.print ("PM1.0: ");
Serial.print (pmSensor.pm1);
Serial.println (" ug/m3");
Serial.print ("PM2.5: ");
Serial.print (pmSensor.pm25);
Serial.println (" ug/m3");
Serial.print ("PM10: ");
Serial.print (pmSensor.pm10);
Serial.println (" ug/m3");
Serial.println();
}
else {
Serial.println ("Data not received. Retrying..");
}
delay (1000);
// pmSensor.sleep();
// delay (3000);
// pmSensor.wake();
// delay (3000);
}
Read_PM_Data.inoThe sensor supports multiple modes and we are using the Question & Answer (Command & Response) mode here. The following image shows the command we need to send and the expected reply format from the sensor. You can check the datasheet of the sensor for more details on the commands.
After uploading the code, you can open the serial monitor and get the PM values.
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Data received. Printing data..
PM1.0: 6 ug/m3
PM2.5: 8 ug/m3
PM10: 9 ug/m3
Serial MonitorHope this tutorial informative for you. We will come with more tutorials in the future. We are also planning to build an air quality monitor device with this sensor. Stay tuned for that. Happy Coding! 😉
Links
- Particulate Matter (PM) – US EPA
- Particulate Matter Sensing for Air Quality Measurements – Sensirion
- Winsen ZH06 – Product Page
- Buy Winsen ZH06 – Robu.in
- Winsen-ZH06-Datasheet-V1.2 [PDF]
- Winsen-ZH06-Datasheet-V1.1 [PDF]
- CSE_ZH06 – CIRCUITSTATE – GitHub
- CSE_MillisTimer – CIRCUITSTATE – GitHub
- Molex PicoBlade 532610871 – 1.25 mm Header Horizontal SMD
- Molex PicoBlade 510210800 – 1.25 mm Receptable
Short Link
- Short URL to this page – https://www.circuitstate.com/winsenzh06