Last week’s article discussed how to control LEGO with Arduino through Bricktronics, building a bridge of communication between Arduino and LEGO. Through this communication mechanism, we can integrate Arduino’s rich peripheral detectors into our application system, giving wings to our creativity.
When writing last week’s article, it was during a haze period, but the “unruly wind” that came with the cold wave and heavy snow blew the haze away. On Monday, in many places in Beijing, the PM2.5 concentration even dropped to single digits, with blue skies and white snow, and Beijing returned to its historical state overnight.
Amazing air quality
Unfortunately, the “unruly wind” is not common, but haze is.
Living under the “sky”, we are constantly concerned about air quality. In recent years, various purifiers and detectors have emerged, with prices varying widely. As a continuation of the previous article, this issue will take the PM2.5 detection module as an example to achieve the interaction of “sensor + Arduino + LEGO”.
According to the definition of haze, it is a phenomenon where a large number of fine dry particles float uniformly in the air, making the horizontal visibility less than 10km. The core of haze detection is to detect the size and quantity of these tiny particulate matter in the air, for example, the “PM2.5” value we care about is the number of particles in the air that are smaller than 2.5 micrometers. Therefore, the core component of haze detection equipment is the haze detection sensor, most of which use the principle of light scattering to measure air particles. We will have a “special edition” to detail the light scattering method and haze detection.
In line with the principle of “easy to obtain, economical, simple language, and strong portability”, we have chosen the GP2Y1010AU0F, which has the highest exposure rate on the market, as the experimental component.
The GP2Y1010 kit uses an infrared LED as the light source and is a micro dust particle size detector.
The implementation process of this case includes the following steps:
1. Hardware Connection
To make the sensor work, the first step is to connect the detector to the Arduino board. The GP2Y1010 detector uses a 6-wire interface:
Circuit wiring diagram
1、6 are for powering the LED and amplifier circuit, 5V
2、4 are for GND;
3 is for the diode switch, digital interface;
5 is for the data output interface, analog interface;
It is important to note that when connecting the LED positive terminal to the Arduino board, a 150Ω resistor should be added, and a 220μF capacitor should be added between the positive and negative leads of the LED. The Arduino board selects the analog interface A04 as the data reading port for the detector, connected to the detector’s 5 wire (black); and selects the digital interface 7 as the switch control port for the infrared LED.
Breadboard and Arduino wiring
2. Mode-Pressure Conversion
The detection steps of the detector are: irradiate—measure—output:
According to the official manual, a standard data collection process takes 10ms, but all actions are completed within the first 0.32ms, the LED is turned on, the analog port outputs immediately, and stabilizes at 0.28ms, allowing for value reading, and data reading is completed after 0.04ms, followed by turning off the LED.
3. Concentration Calculation
The Arduino’s analog port A04 reads the value Val as an analog value from 0 to 1025, corresponding to a voltage of 0 to 5V,
we need to convert the analog value to a voltage value:
dustVal=Val/1024*5
4. Data Calibration
The voltage value converted from the analog data is not the haze concentration value we need, we also need calibration:
The above figure shows the corresponding curve of voltage and dust concentration in the manual. We need to find the relationship between voltage and concentration. We find that this curve shows an approximate linear relationship when the haze concentration is between 0 and 0.55mg/m³, i.e., when the PM2.5 value is between 0 and 550. When the haze concentration exceeds 550, the voltage value tends to stabilize. In other words, the maximum detection value of this detector is around 550mg/m³ (if your PM2.5 detector reaches 550, it is likely to be this model).
During the calibration process, it is important to note that the voltage output value without dust is around 0.6V, which is not the standard value of 0.9V in the manual:Note1
Note1: Regarding this value deviation, I found other GP2Y1010 manuals for comparison and discovered that one version of the manual states the voltage-concentration relationship without dust is 0.9V. This model, the second generation of Sharp’s product, has been calibrated based on the first generation, making it more accurate. Since I couldn’t find further information, I can only speculate that this difference is due to the calibration differences. Some online sources mention that this device’s values jump, which may be due to users directly copying the online code,0.3V calibration differences affecting the detection values.
So should it be 0.9V or 0.6V? I think it should be based on the actual output of the detector. For example, the model I am currently using has an output voltage value consistently around 0.6 (the “unruly wind” has caused PM2.5 in the entire city of Beijing to hover around 10), so clearly, the dust-free voltage cannot be calibrated to 0.9.
Therefore, when encountering such issues, it is crucial to analyze each specific device individually and never directly copy code from the internet (otherwise, the calculated haze values may appear negative).
To understand the reasons, one must also understand the underlying principles. Learning to analyze and think independently is the most critical.
How to abstract a mathematical function from an existing function graph becomes an algebra problem:
GivenF(x)=aX+b
From the graph, we can obtain two points: (0.4,3) and (0.5,3.5):
Solving the equations gives:a=6,b=0.6
Thusf(x)=6x+0.6, which leads to the haze concentration:x=f(x)/6-0.1≈0.167V-0.1, with the concentration unit beingmg/m³. The PM2.5 concentration unit is μg/m³, so we need to multiply by the conversion factor1000mg/g.
This approximation may have errors. To be more accurate, we need to compare the detected values with the values from a precisely calibrated detection device, then fine-tune the values ofa and b. Without professional equipment for calibration, the results calculated by individuals may differ, leading to discrepancies in detection values. Currently, laser source detectors mostly return haze concentration values directly, with the calibration process completed by the manufacturer and written into the firmware, resulting in more precise outcomes. The reason we chose this mainstream product from two years ago is more to experience the joy of thinking during the calibration process.
Returning to the haze concentration equationx=0.167V-0.1, the manual indicates that the minimum voltage without dust can be taken as0, meaning the detector may output voltages less than0.6V. To avoid the situation where the haze value appears negative, we need to design the program to filter out inputs below0.6V. Thus, carefully understanding the product manual can help us design a more reasonable and robust program.
At this point, the Arduino program is complete. Next is the control of LEGO.
5. LEGO Control
Having obtained the haze concentration, we need a display to show the value, so we casually made one using LEGO:
The transmission structure uses a wheel group drive, which can amplify the rotation distance.
Wheel group drive and gear group
The radius ratio of the two-stage wheel group is 1:4, using a small wheel to drive a large wheel, with both having the same linear speed. According to the circumference formula L=2πR, after one gear group speed change, the rotation amount of the LEGO motor increases by 4 times. After two stages of speed change, the rotation amount of the LEGO servo will increase to 16 times the original. The control precision of the LEGO servo can reach 1°, but theoretically, after amplification by the gear group, the control precision can reach 1/16°.
The calibration process of the LEGO servo is also quite simple. Just observe how much rotation is needed for the pointer to move from scale 0 to scale 500, and then divide the rotation amount by 500 to obtain the conversion ratio between haze changes and rotation amount. In our case, this ratio is 1:29.5.
The sensor completes a measurement in 10ms, but the LEGO servo cannot complete a drive in 10ms. Similarly, a single measurement by the sensor may have errors, but averaging multiple measurements can eliminate measurement errors. Therefore, in the program, we set the measurement frequency to 1s per time, taking the average value over 10s as the display measure for the LEGO indicator, and use this to control the position of the LEGO servo.
When the haze reaches a certain severity, masks need to be worn. We set in the program that when PM2.5 exceeds 120, the mask will be deployed from the holder, and it will automatically retract when below 120.
When we filmed the video, Beijing had just issued a haze warning. At that time, the outdoor PM2.5 concentration in Chaoyang District was 175, and indoors, according to our LEGO detector’s indication, it was also above 150, causing the mask to pop out, and it seems it won’t be retracted tonight…
So we light up an incense stick to give it a boost:
To be continued, stay tuned for the next issue.
Science and Technology Classroom
Online learning platform for science and technology counselors
WeChat ID: sciclass
