Home > Basics > How to design a high-precision nitrate and phosphate detector a solution with costs lower than you can imagine

How to design a high-precision nitrate and phosphate detector a solution with costs lower than you can imagine

Hello everyone, I am xukejing, your old friend, the legendary golden expert of the Core Bar Guest Community, and the self-proclaimed chief development board reviewer. Two years ago, I was the author of the "Lazy Fish Tank". Recently, some friends have consulted me about the issue of water cloudiness in fish tanks, and I have found that many cases are caused by excessive nutrient salt levels in the water. For example, excessive nitrite levels can lead to fish poisoning, while excessive phosphate levels can cause blue-green algae outbreaks in the water. Some enthusiastic netizens have already conducted a comprehensive test using N-in-1 test strips and found that the minimum detectable value for nitrite in aquarium test strips is 1mg/L, which is 20 times higher than the safe limit for fish at 0.05mg/L. By the time the test strip shows any color, the fish would have long been dead. Therefore, today we will discuss how to accurately measure the concentration of nitrite in water.

1 Project Background

1.1 Conventions for Measurement Units

In freshwater, since 1 liter of water is close to 1 kg, mg/L and ppm are approximately equivalent units. However, when measuring saline bodies of water, such as seawater (with a specific gravity of 1.020~1.026), there is a deviation of over 2% from pure water, so mg/L and ppm cannot be considered equivalent. Furthermore, the concentration of nitrite can be measured in mg/L either as nitrogen or as nitrite ion, or even as sodium nitrite. Due to the different molar masses of these elements and compounds, even when expressed in mg/L, the same sample will yield different values in different reference systems. Fortunately, in the field of chemistry, there is a more universally applicable unit of measurement called mol/L (moles per liter). For example, a solution with a concentration of 1 mol/L of sodium nitrite, regardless of whether it is measured in terms of sodium nitrite, nitrite ions, or nitrogen, will always be 1 mol/L. This achieves uniformity in numerical values. In this project, unless otherwise specified, doses will be given in moles as units, such as moles, millimoles, or micromoles.

1.2 Challenges in Low Concentration Measurements

The safe limit for fish of 0.05 mg/L of nitrite, when measured as nitrogen, corresponds to a concentration of approximately 3.57 µmol/L. Lower concentrations, such as 0.01 mg/L, go down to fractions of µmol/L. Please pay attention to the units I am using here: µmol/L. One million µmol is equal to 1 mol, with six zeros in between. This illustrates just how miniscule a quantity a µmol is. According to the national standard specification GB/T 12763.4-2007 for seawater chemical elements investigation, the detection of nitrite utilizes the sulfanilamide and N-(1-naphthyl)ethylenediamine dihydrochloride coupling reaction, while the detection of phosphate utilizes the molybdenum blue reaction with potassium antimony tartrate, ammonium molybdate, and ascorbic acid. The higher the concentration of these nutrients, the darker the color formed after the colorimetric reaction. However, for solutions with a few µmol/L or fractions of µmol/L that undergo colorimetric reactions, they appear transparent. It is difficult for the human eye to discern color readings in such cases, thus requiring special instruments to measure specific absorbance or transmittance. The recommended method in the national standard specification is the spectrophotometric method, which provides accuracy at the arbitration level.

1.3  Market Analysis of Spectrophotometers

Currently, there are various commercial instruments based on spectrophotometric methods available from domestic and international manufacturers. The performance indicators of these instruments from different countries are quite similar. Depending on the target substance and quantity being measured, prices for these instruments mostly range between 1000 and 3000 RMB, such as Hashi's laboratory-grade spectrophotometer. Some ultra-compact, low-precision instruments are priced slightly lower, such as the Italian HanaEgg machine, but still close to 600 RMB.

Some friends express discontent, stating that for 600 RMB, one could buy a domestically-made smartphone with a MediaTek processor. That HanaEgg definitely lacks any technological sophistication, with a plastic casing that feels like it costs only 9 RMB to ship, a black-and-white LCD screen reminiscent of the early days of China's reform and opening-up period, and lacking a Type-C lithium battery charging interface. Its electronic components seem like 20th-century junk. Pricing it at 600 RMB is simply daylight robbery.

1.4 Project Objectives

As a shameless braggart, I must confess that I am not only skilled in boasting but also a hidden expert player. Since the existing products on the market are so overpriced, why not try our hand at DIY? If you find this market unacceptable, then let's transform it! By utilizing the latest technology, we can significantly reduce costs and bring down the price of the spectrometer. Remember the famous Chinese idiom, "Three links for love"? Well, we'll make sure to live up to its spirit.

So, the Italian Hana egg machine costs 600 yuan per unit, right? Just watch as we surpass it with a spectrometer that offers more measurement functions, higher decimal point resolution, and a sensor scheme that is 30 years ahead of its time. Let our international friends witness the ingenuity of our manufacturing powerhouse. We want our horses to run fast and eat less grass, so if the hardware solution exceeds 100 yuan, I will admit defeat. When our champion faces a challenge, our style has always been to bulldoze through like a road roller. Rest assured, I, your humble servant, have decided to embrace this challenge!

2 Project Description

This project has developed an open-source, high-precision water quality tester compatible with ESP32/Arduino. Based on the national standard photometer principle, this instrument can accurately measure concentrations as low as 0.1 micromoles per liter. The device is capable of performing the diazo coupling colorimetric reaction involving sulfanilamide and naphthyl ethylenediamine dihydrochloride for testing nitrite in water. It can also perform the phosphomolybdenum blue colorimetric reaction involving potassium antimony tartrate, ammonium molybdate, and vitamin C. Currently, the absorbance curve for the diazo coupling reaction has been calibrated and the testing code is publicly available on Gitte. Although the calibration curve for the phosphomolybdenum blue reaction has not yet been completed, the code for real-time data upload to Matlab using the phosphorus sensor module has been developed. The calibration curve for phosphates will be perfected in the future. The CAD files required for 3D printing in this project are published on Thingiverse.

2.1 Bill of Materials (Total procurement cost less than 55 yuan)

The hardware design of this project has been optimized to reduce costs. The total procurement cost for each detector is less than 55 yuan. With bulk purchasing, it is estimated to be below 50 yuan. The specific materials are as follows:

For electronic devices, the GY-2561 module costs 11 yuan, the Grove TSL25911 module costs 19 yuan, and the ESP32 development board costs 15 yuan. The cost of 1 green light 520nm 3mm LED and 1 infrared 850nm 3mm LED can be disregarded, as well as 2 250-ohm resistors and several DuPont wires. For the structure, the casing and sensor brackets are all 3D printed, with a material consumption of approximately 58 grams. Calculated at 50 yuan per kilogram, it amounts to about 3 yuan. As for standard parts, two 10mm glass colorimetric dishes cost 5 yuan, while 4 embedded nuts M3x4x3 and 4 M3x8 screws can be disregarded.

Further improvement suggestions: If cost is not a major concern, the author strongly recommends using a 540nm LED instead of a 520nm LED for higher sensitivity when measuring nitrite. Similarly, using an 880nm LED instead of an 850nm LED can enhance sensitivity when measuring phosphates. The procurement cost for LEDs with non-standard wavelengths is slightly higher, but it is still less than the price of a can of cola.

  

2.2 Hardware Solution Introduction

2.2.1 GY-2561 Module

The GY-2561 module utilizes the TSL2561 sensor, which is capable of measuring both infrared and human-visible light.

It outputs a 16-bit ADC signal and allows adjustable integration time (in our project, we use a 403ms integration time) and amplification factor (in our project, we use a 16X amplification).

The dynamic range of this module ranges from 0.1 to 40000 Lux.

With a built-in infraredphotodiode, it is able to provide relatively accurate measurements even in environments with high levels of infrared noise interference.

2.2.2 Waveshare TSL25911 module

Featuring the TSL25911FN, this module is capable of measuring both infrared and visible light (with a maximum range of 80000Lux).

With a 16-bit ADC output, the integration time can be adjusted (for this project, a 600ms integration time is used) and the amplification factor can be customized (a 25X amplification factor is used for this project).

Boasting a high sensitivity of up to 188uLux and an impressive dynamic range of 600M:1.

Equipped with a built-in infrared sensitive diode, it is able to provide relatively accurate measurements even in environments with significant infrared noise interference.

2.2.3 ESP32 Development Board

The ESP32 development board used in this project is the WEMOS LOLIN32 Lite, which features a 32-bit dual-core processor running at 240MHz and 4MB Flash memory.

It supports lithium battery charging and discharging (for cost-saving purposes, the $10 lithium battery has been omitted from this project, but the functionality can be easily added at any time).

It also supports WiFi and Bluetooth connectivity (in this project, WiFi UDP is used for interaction with Matlab).

Furthermore, it is equipped with two 255-level DACs, allowing for adjustable output power of the two LED light sources in this project.

In terms of interfaces, it has two I2C ports (although this project only utilizes one I2C interface, combining both sensors onto the same I2C port).

 

2.3 The parts that need to be 3D printed include:

Upper part of the casing. It is used to secure the ESP32 development board and two color dish holders.

Lower part of the casing.

Color dish holder (Model A), designed for 10mm color dishes. One end holds a 520nm 3mm LED, while the other end secures the GY-2561 light sensor module.

Color dish holder (Model B), designed for 10mm color dishes. One end holds an 850nm 3mm LED, while the other end secures the Waveshare TSL25911 light sensor module.

Light-shielding protective cover, used to block ambient light (the optical design of the color dish holder has been carefully designed to minimize interference from ambient light, so the light-shielding cover may not be necessary when using the device in a dimly lit environment such as a computer screen illuminating the room at night).

It would take over an hour and a half to print everything together. To avoid wasting time on incorrect dimensions, it is recommended to first print the color dish rack for calibration of the 3D printer's size scaling factor. The outer contour of the 10mm color dish measures 12.5mm x 12.5mm, leaving a tolerance of 0.5mm in the model.

For technical details, please refer to the national standard specification: GB/T 12763.4-2007.

The open-source address for the 3D printed parts of this project can be found at Thing files for Open-Source Photometer for Nitrite-Nitrogen Testing by xukejing - Thingiverse.

2.4 Schematic Diagram

Schematic diagram

 

2.5 Hardware Assembly

For the assembly of the two colorimetric dish racks, it is necessary to first attach the LED and light sensor modules (520nm green LED corresponds to the GY-2561 light sensor module, 850nm infrared LED corresponds to the Waveshare TSL25911 light sensor module). Connect the circuits according to the schematic diagram, and then adhere the ESP32 development board and the two colorimetric dish racks to the upper part of the casing (using hot glue).

While the hot-melt adhesive is still in a semi-solid state, place the colorimetric dish on the upper part of the casing and ensure that the holes on the dish holder align with those on the casing.

Four M3x4x3 injection molded captive nuts are embedded in the lower half of the shell, and it is recommended to use a soldering iron to heat and insert the nuts. The soldering iron should be set at a temperature of 300 degrees Celsius.

 

Place the top cover and secure it with four M3x8 screws.

The test liquid was placed in a colorimetric dish. The image below shows the pink and transparent product after the color reaction of the 4umol/L sodium nitrite standard solution.

Place the colorimetric dish into the corresponding sensor-equipped 520nm wavelength colorimetric rack (where 520nm represents green light).

Please cover the light-shielding cap and measurements can be taken.

 

3. Open Source Software

3.1 Open Source Instrument Firmware Code and PC Application Code

The setup of the Arduino and ESP32 development environment can be referred to as "arduino-esp32 development environment setup".

The open source repository for the software code of this project is located on Gitee: Open Source High-Precision Water Quality Testing Instrument.

The library files for Arduino need to be installed from Gitee and placed in the "libraries" directory of the Arduino documentation. In order to improve compatibility with ESP32, I made appropriate modifications to the TSL2561 library by adding an input for the Wire address pointer in the function variables, making it easier to set different I2C ports for ESP32. On one hand, this modification is for ease of sharing, and on the other hand, it is because of the GPL license, thus the modified parts of the library are also open source.

Pay attention to the photometer_esp32.ino file. You need to fill in the following information, including the wireless router name, wireless router password, instrument IP, and host computer IP, respectively. The instrument IP needs to be in the same network segment as the host computer IP. It is recommended to log in to the wireless router backend first, check the router's LAN gateway, subnet mask, DNS, and host computer IP, and then select an unused IP as the instrument IP.

The MATLAB code section can be found in the "matlab" directory of the shared Gitee repository. The file "udp_callback.m" serves as a callback function that is triggered when the host computer receives data from the instrument. In actual usage, you only need to modify the instrument's IP address in "udptest.m". Once you click "run", data collection will begin. The variables for the data are named Cj1, Cj2, Cj3, and Cj4, corresponding to four channels from two sensors.

After collecting the data, you can now utilize various ready-to-use functions in Matlab for further processing. For example, you can use the "mean" function to calculate the average, employ "filterDesigner" to design filters, and utilize "polyfit" for curve fitting. When encountering Matlab, my fellow colleagues, do not panic. A true programmer should be as agile as a master of weapons, adept at employing various tools. Do not confine your thinking by being fixated on a particular tool.

4. Test Example

Before measuring the specific water quality, it is necessary to prepare standard solutions of sodium nitrite with different concentrations. The absorbance of the color reaction at each standard concentration is measured first, and the absorbance curve is calibrated. I have tested a total of eight standard solutions with concentrations of 4 umol/L, 2 umol/L, 1 umol/L, 0.8 umol/L, 0.4 umol/L, 0.2 umol/L, 0.1 umol/L, and 0.0 umol/L for the color reaction's standard absorbance, and then performed curve fitting. The fitting results in Matlab are shown in the figure below:

From the calibration curve in the graph, it can be observed that there is a strong linear relationship between the concentration of nitrite and the absorbance. This not only demonstrates the reliability of the testing method principles but also indicates that the accuracy of the testing instrument meets the expected design standards. The calibration data was obtained by testing eight standard solutions with concentrations of 4 umol/L, 2 umol/L, 1 umol/L, 0.8 umol/L, 0.4 umol/L, 0.2 umol/L, 0.1 umol/L, and 0.0 umol/L on-site using the instrument.

By substituting the pre- and post-reaction absorbance values of the test liquid into the equations, the transmittance and absorbance of the test liquid can be calculated sequentially. Then, by inserting the absorbance of the test liquid into the calibration curve, the concentration of nitrite in the test liquid can be determined as 0.947 umol/L. The measured value is indicated by the red dot in the graph.

Prev:  The Advantages of Partnering with Unikeyic as Your Electronic Component Distributor
Next:  Why Unikeyic is the Go-To Choice for Electronic Component Manufacturers
Would you like more details or pricing on the components mentioned above? Please complete the form below.
Submit Inquiry / Request Info