Propose an Automatic Ammonia Concentration of Water Measuring System Combining Image Processing for Aquaculture

Propose an Automatic Ammonia Concentration of Water Measuring System Combining Image Processing for Aquaculture

Phat Nguyen Huu* Hoan Nguyen Duc

School of Electronics and Telecommunications, Hanoi University of Science and Technology, Hanoi 10000, Vietnam

Corresponding Author Email:
8 October 2020
20 May 2021
25 June 2021
| Citation



The underwater environmental monitoring system applying IoT is now developing for 4.0 industry. It helps not only to simplify work but also to increase efficiency and reduce costs and execution time as well as ensure health to avoid contact with toxic solutions. In this paper, we first model the process of measuring NH3 concentration manually to automate. Secondly, the proposed model is combined with the process of processing output image automatically and displaying the results on the server. Thirdly, the system is able to measure the concentration by VNC viewer connecting with Raspberry pi4 via WiFi. The result of the NH3 concentration parameter is sent to users quickly to alert promptly when it reaches a dangerous threshold. Experiments show that the system not only ensures fast processing time (less than 5 minutes) but also improves accuracy (up to 80%) comparing with real devices.


NH3 concentration, automation, image processing, environmental monitoring, mechanics

1. Introduction

The aquaculture industry is considered a spearhead of Vietnam of economic development trend with an export weight of 4.7 billion dollars where shrimp farming accounted for the highest proportion. Projects for aquaculture are heavily invested. However, aquatic products are very difficult to raise because of damaging to people. In aquaculture, water resources are considered to be a determinant. Therefore, monitoring of water environment to detect unusual phenomena that performs solutions quickly is important. However, water testing in existing aquaculture ponds is still performed by hand using poor quality equipment that takes a lot of time and effort.

Ammonia has long been widely studied for industry since its concentration has an important role in water environment. The authors [1] presented the NH3 measurement using optics to analyze its absorption for the spectral bands. However, the system only performs for the laboratory environment. Besides, the method of determining NH3 of water by evaporating dissolved into container and then selectively detecting it using a color sensor is presented by Cho et al. [2]. However, the response time of method is more than 10 minutes and must be observed by human. The other method that uses perovskite halide CH3NH3PbI3 (MAPI) based on sensor material while exposing to NH3 gas only 10 ppm is presented by Maity et al. [3]. However, the disadvantage of method is only used once. Sensitive sensors such as metal oxide (Pt) or nickel oxide (Ni) based on creating and researching GaN are presented by Liu et al. [4]. However, they must operate at high temperatures. Flexible ammonia sensor with high efficiency to detect very low NH3 concentrations below 500 ppb is made on polyethylene terephthalate substrates using synthetic membranes (3,4 -ethylene-dioxythiophene) that is presented by Li et al. [5]. However, the method also shows limited reaction with NH3.

Table 1. Dosage of toxic NH3 tested on several types of shrimp and fish [6-8]



Dosage after 96 hours


Striped pass



Spotted sea trout



Rainbow trout



Cutthroat trout



Farthead minnow



South American White shrimp



White shrimp


One of the causes of water pollution is the ammonia (NH3) concentration generating by food that shrimps do not use. NH3 will affect health of shrimp that can cause death for example mass mortality of shrimp due to high concentration (1.36 mg/l) as shown in Table 1 [6-8]. Currently, there are many methods for determining NH3 concentration of water such as using test kit, gauges, etc. Many farmers have chosen to use the test kit because of its simple implementation and low cost. However, the mixing liquid is quite time-consuming and a harmful for skin and eyes of people. Therefore, we decide to design the NH3 automatic measuring system for shrimp farming that saves time and effort in this paper. Besides, the system is able to measure the concentration by VNC viewer connecting with Raspberry pi4 via WiFi. Therefore, it is happy for the farmers to easily control the stability of the concentration of NH3 when they are on holiday with the VNC viewer installing on a device and WiFi connection. The system not only ensures more accuracy than others but also avoids health impacts to farmers.

The rest of the paper includes four parts and organizes as follows. Section 1 will discuss the related work. In Section 2, we present theory basis ammonia. In Section 3, we present the proposal system. Section 4 will evaluate the proposal system and analyze the results. In final section, we give conclusions and future research directions.

2. Theoretical Basis

2.1 NH3 concentration

Ammonia (NH3) of aquaculture ponds comes mainly from food that shrimps do not eat. Therefore, problems of high NH3 concentration often occur where feed is used [6, 9-12].

Ammonia nitrogen consists of ammonia (NH3) and ammonium (NH4+) where NH3 is toxic gas and [NH4+] is non-toxic. However, the methods of measuring ammonia nitrogen concentration do not distinguish between these two forms since the concentration of nitrogen is balanced depending on pH and temperature as follows [12]:

$\mathrm{NH}_{3}+\mathrm{H}^{+} \rightarrow \mathrm{NH}_{4}^{+}$                (1)

When [OH] increases, the concentration of NH3 also changes (the higher temperature, the greater concentration of NH3).

When [pH] concentration is less than 8.5, [NH4+] will be dozens of times higher than NH3. When [pH] is more than 9, the concentration of NH3 is tens of times higher than that of [NH4+]. As a result, fish and shrimp will be contaminated with NH3 through the gills. If it infects lightly, shrimp will grow slowly and become susceptible to disease. Otherwise, it will cause the result in mass death.

2.2 Amonia test method

The Sera NH3/NH4 (water test kit) is used to monitor the total concentration of NH3 and [NH4+] to check ammonia (NH3) that avoids toxicity to fish and shrimp in aquaculture. Its volume is 15 ml that can use for 60 times [13].

The kit is used as follows:

1) Step 1: Cleaning inside and outside the glass with tap water before and after testing; shaking the bottle before using.

2) Step 2: Washing the glass several times with water sample to be tested, then filling with 5ml of water; drying the outside of bottle.

3) Step 3: Putting 6 drops of reagent bottle of reagent bottle 1 into glass jar containing water sample to be tested; closing cap and shaking.

4) Step 4: Opening the lid, putting 6 drops of reagent second bottle into the bottle; closing, shaking, and opening it.

5) Step 5: Adding 6 drops of reagent third bottle; closing the lid, and shaking.

6) Step 6: After 5 minutes, comparing the color of solution with palette.

7) Step 7: To evaluate the level of toxic free ammonia (NH3) from measured value and [pH] value according to Table 2 [14, 15].

Table 2. NH3 is derived from [NH4+] concentration and pH [14, 15]




pH value
























NH3 level






















3. Proposal System

3.1 Overview of the system

In the paper, we decide to choose to use test kit to determine NH3 concentration because of its simple and solve the disadvantages of method by automation all steps. We propose to build a system of combining mechanical and electronic equipment and automating the manual measurement process to increase its accuracy and speed. NH3 concentration of tested solution will be calculated based on analysis and comparison of pixels of image associating with pH that is captured by camera. Besides, we propose to measure the concentration by VNC viewer connecting with Raspberry pi4 via WiFi.

Figure 1 is the functional model of proposal system. The system operates specifically as follows:

1) Step 1: The system proceeds to mix solution.

2) Step 2: After 5 minutes, the solution will change color completely.

3) Step 3: Raspberry Pi takes images form camera, analyzes color of spots, and combines pH to determine concentration of NH3.

4) Step 4: Displaying results with NH3 concentration and alerts.

Figure 1. Model of proposal system

3.2 System design

To build the system, we need to perform the following steps:

1) Step 1: Overall design.

Figure 2 shows the system block diagram including seven blocks, namely water supply, dispensing solution, mixing solution, power, processor, image processing, and display blocks.

2) Step 2: Detailed design of each block.

Details of the block diagram of the system are as follows:

Power block: It maintains the operation of system by supplying energy. Depending on the components of system, we use three different power sources (5v, 9v, 12v) as shown in Table 3 [16-18].

Water supply block: In order to get the amount of sample and pond water, the block will use a pump and control it by relay. To ensure to pump the desired amount of water, we perform experiments to determine the pumping time. Details of the block are shown in Figures 3 and 4.

Dispensing solution block: It consists of servo, cylinder, CNC mini stepper motor, and 3-prong lock. The process of operation is as follows:

Step 1: The servo controls the 3-pin lock when the pipe from the solution and cylinder are connected.

Step 2: The stepper motor pulls down the piston, and thus the amount of solution is eliminated about -0.12ml.

Step 3: The servo then rotates 180° since the pipe from cylinder to test tube is cleared.



Figure 2. a) Modeling of the mechanical part and b) proposal system block diagram



Figure 3. a) Overview and b) details operation of water supply block

Table 3. The power supply corresponds to components [16-18]





Mini water pump



stirring motor






CNC stepper motor



Raspberry pi4






Kit Arduino


Step 4: Stepper motor drives back the plunger since the solution reaches test tube.

Mixing solution block: It consists of a geared motor, two circular magnets with holes, and a magnetic stirrer. The rotary deceleration motor and two magnets will create a swirling magnetic field and pull the magnetic stirrer in the rotating test tube, and thus the test solution is stirred.

Control block: We use Atmega328 micro-controller that belongs to AVR micro-controller family. It will provide digital signals using PWM to control the servo, CNC stepper motor, and provide enable signals (5V) to close or disconnect relay to adjust delay time. Besides, it also sends a notification of dispensing solution block (five minutes) to raspberry Pi to capture images.

Image processing block: The image processing block consists of a raspberry Pi 4 attaching to REV V1.3 camera module via an FFC cable.

After making solution, Arduino will calculate five minutes to completely change color, and then send signal to raspberry pi via UART protocol. When the signal is received, the camera will take an image, and then perform to process it with OpenCV program. Finally, it compares the NH3 threshold to inform to user.

In order to be able to evaluate the results, we choose to use the library open computer vision (Opencv). It is written in C/C++ since it has a very fast computation speed and can be used with real-time related applications. It has interfaces for C/C++, Python Java, since it supports Windows, Linux, MacOs and Android, iOS. It has more than 2500 optimized algorithms covering a complete range from classic and modern computer to machine learning. The algorithms can be used to carry out image processing fast.

Image analyzing and processing is performed as follows:

When the solution changes color completely, the camera will take an image. The image that includes the test tube and its color scale (as shown in Figure 9) will be placed under same lighting conditions. The raspberry pi will use available data to analyze images capturing by Opencv (using Python 3). Firstly, it converts the capturing image to gray and then compares gray level of solution with colors scale of test kit according to formula:

$\mathrm{NNH}_{4}=\left(\frac{x-a}{b-a}\right) \cdot\left(n_{2}-n_{1}\right)+n_{1}$,                    (2)

where, NNH4 is the actual [NH4+] of water sample, x is the gray level of solution color, a and b are the gray level of two consecutive colors in the reference color scale (a <x <b), n1 is [NH4+] in the gray level a, n2 is [NH4+] at the gray level b.

We will then give [NH4+] and combine pH to give the concentration of NH3. If pH value does not match the values in Figure 9 (7; 7.5; 8; 8.5; 9), we determine the concentration of NH3 by formula:

$\mathrm{N}_{\mathrm{NH} 3}=0.06 \times N_{N H 4}\left[\left(\frac{p-p 1}{0.5}\right) \cdot\left(y_{2}-y_{1}\right)+y_{1}\right]$                  (3)

where, NNH3 is the actual NH3 concentration of water sample, NNH4 is [NH4+] calculating above, p is actual pH of water sample, and p1 is pH value on the largest comparison scale satisfying p1 <p. y2 is multiple of NH3 concentration value at pH value on the smallest comparison scale greater than p and y1 is multiple of the NH3 concentration value at pH = p1 (as shown in Figure 9).

In this paper, we consider the variation of NH3 concentration for small range of pH changes (ranges 7-7.5, 7.5-8, 8-8.5, 8.5-9) as linear (as shown in Figure 10). The analysis and comparison of color spots of color zones are also linearly interpolated in each of the above pH ranges.



Figure 4. Details of operation of water supply block circuit combined with stirring solution: a) schematic and b) schematic 3D

Display block: It displays an image of solution with NH3 concentration and warns of water safety.

3) Step 3: Designing circuit.

In this step we design two circuits as follows:

Water supply circuit: It controls for rinsing tube and water pump, and stirring for the engine. Schematic 2D and 3D of water supply circuit is shown in Figure 5.

Figure 6 shows the schematic diagram of circuit that receives three control signals from Arduino and switches to control.

Liquid mixing circuit: controls the servo and CNC stepper motors. The circuit includes two main functions as follows:

Controlling servo: Using three digital pins from Arduino to provide direct control signal to servo.

Controlling stepper motor: Since the stepper motor control module uses four Arduino digital pins, we use relay blocks to be able to control three stepper motors.

Figure 7 is the schematic diagram and printing circuit of solution mixing block.

4) Step 4: Checking the stability of circuit.

5) Step 5: Completing the product.

Figure 5. Schematic 2D and 3D of water supply circuit

Figure 6. Schematic principle of circuit receives three control signals from Arduino



Figure 7. The solution mixing block a) schematic and b) schematic 3D

4. Experimental Result

4.1 Setup

The system setup process consists of following steps:

Step 1: Mixing liquid.

Step 2: Raspberry Pi sends a signal for camera to take an image after five minutes.

Step 3: Raspberry Pi analyzes the image and gives result of concentration of NH3.

4.2 Result

The blocks operate to absorb the exact amount of solution. The continuous stirring solution helps the process time quickly and improves accuracy. The complete product system is shown in Figure 8.

We test with two environments:

Case 1: Fresh water where shrimp and fish are able to live. We select drinking water from Ta Quang Buu library, Hanoi University of Science and Technology (HUST), Vietnam. The results show that the water is safe.

Case 2: Polluted water where shrimp and fish are not able to live. We choose ToLich River, Vietnam. The results show that the water statement is dangerous.

Figure 8. Actual product image

Figure 9. The color chart is for collating NH3 value [10]

Figure 10. Calculating NH3 concentration based on pixel value and pH



Figure 11. Experimental results with Tien lake (HUST) with NH3 concentration as 0.00969 mg/l a) manual and b) automatic results



Figure 12. Experimental results with tap water with NH3 concentration as 0.000429 mg/l: a) automatic results and b) color solution

Figure 13. Image of polluted water on ToLich river, Vietnam (Source from internet)

Figure 14. Experimental results with ToLich River, Vietnam

We compare the results based on Table 2 and Figure 9.

Experiment with water of Tien lake:

From the results (as shown in Figure 11), we see water reaching the safe level with NH3 concentration of 0.00969 mg/l.

Experiment with water of Ta Quang Buu library:

According to the QCVN 01:2009/BYT standard, the water reaches for living with a maximum [NH4+] of 3 mg/l and [pH] of from 6 to 8.5 mg/l. In Figure 9, NH3 concentration will range from 0.003 to 0.3 mg/l. Therefore, the results are expected to be at safe level or harmful with long term.

Experimental results with tap water from Ta Quang Buu library (as shown in Figure 12) show that it reaches a safe threshold with [NH3] = 0.000429 mg/l.

Experiment with ToLich River, Vietnam:

Figure 13 shows the five days of West Lake to ToLich river [14, 19, 20]. To verify NH3 concentration, we have tested it in experiments.

From the results (as shown in Figure 14), we see that water is extremely dangerous with toxic to aquatic life (concentrations of NH3 > 3.6mg/l).

4.3 Discussion

Speed of system:

It takes about one minute for the system to phase the solution, and then waits for five minutes for the solution to react completely according to the requirements of test kit. Therefore, total time is six minutes from beginning to displaying the final result. The process is measured by Phat and Hoan [21].

Accuracy of system:

We calculate the concentration NH3 < 0.00072 (mg/l) at pH = 7 based on Figure 9 and Test report [22] with concentration NH4+ < 0.12 mg/l.

We conclude that accuracy of NH3 concentration is acceptable with the aim of warning shrimp farmers for three levels (safe, harmful with long-term exposure, and acutely toxic).

5. Conclusion

The paper proposes to design NH3 concentration measurement system based on image processing and automation. The results show that the system performs fast with acceptable errors. However, the errors of system are still large due to automation in several cases. Therefore, our next development direction is to optimize the system to reduce errors; combine measurement of other parameters; and predict an increase or decrease in NH3 concentration using CNN to be able to integrate into existing IoT systems [23-25].


This research is carried out in the framework of the project funded by the Ministry of Education and Training (MOET), Vietnam under the grant B2020-BKA-06. The authors would like to thank the MOET for their financial support.


[1] Spector, O., Jacobson, E. (1999). Advanced ammonia (NH3) monitoring system for industrial application. Proceedings of SPIE - The International Society for Optical Engineering, 41: 1-7.

[2] Cho, Y.B., Jeong, S.H., Chun, H., Kim, Y.S. (2018). Selective colorimetric detection of dissolved ammonia in water via modified Berthelot’s reaction on porous paper. Sensors and Actuators B: Chemical, 256: 167-175.

[3] Maity, A., Raychaudhuri, A., Ghosh, B. (2019). High sensitivity NH3 gas sensor with electrical readout made on paper with perovskite halide as sensor material. Scientific Reports, 9(1): 1-10.

[4] Liu, I., Chang, C., Huang, Y., Lin, K. (2019). On the ammonia sensing performance and transmission approach of a platinum/nickel oxide/gan-based metal-oxide-semiconductor diode. IEEE Journal of the Electron Devices Society, 7: 476-482.

[5] Li, S., Chen, S., Zhuo, B., Li, Q., Liu, W., Guo, X. (2017). Flexible ammonia sensor based on PEDOT: PSS/silver nanowire composite film for meat freshness monitoring. IEEE Electron Device Letters, 38(7): 975-978.

[6] Da Silva, L.F.B.A., Pires, N.M.M., Dong, T., Teien, H.C., Yang, Y., Storebakken, T., Salbu, B. (2018). The role of temperature, ammonia and nitrite to bioluminescence of aliivibrio fischeri: towards a new sensor for aquaculture. 40th Annual International Conference of the IEEE Engineering in Medicine and Biology Society, pp. 4209-4212. 10.1109/EMBC.2018.8513283

[7] Africa, A.D.M., Aguilar, J.C.C.A., Lim, C.M.S., Pacheco, P.A.A., Rodrin, S.E.C. (2017). Automated aquaculture system that regulates Ph, temperature and ammonia. 9th International Conference on Humanoid, Nanotechnology, Information Technology, Communication and Control, Environment and Management (HNICEM), pp. 1-6.

[8] Kaiser, G.E., Wheaton, F.W. (1998). Computerized rapid measurement of ammonia concentration in aquaculture systems. Proceedings of OCEANS 88 a Partnership of Marine Interests, 1: 76-83.

[9] Wang, H., Xiao, X., Wang, H.Z., Li, Y., Yu, Q., Liang, X.M., Feng, W.S., Shao, J.C., Rybicki, M., Jungmann, D., Jeppesen, E. (2017). Effects of high ammonia concentrations on three cyprinid fish: acute and whole-ecosystem chronic tests. Science of the Total Environment, 598: 900-909.

[10] Eddy, F. (2005). Ammonia in estuaries and effect on fish. Journal of Fish Biology, 67(6): 1495-1513.

[11] Randall, D., Wright, P. (1987). Ammonia distribution and excretion in fish. Fish Physiol Biochem, 3(3): 107-120. 

[12] Gross, A., Boyd, C., Wood, C. (2000). Nitrogen budget and transformations in channel catfish ponds. Aquacultural Engineering, 24(1): 1-14.

[13] Datenschutzhinweis (2020). Sera ammonium/ammonia test (NH4/NH3). Sera Company, 1-2. Online:

[14] Angers, R.P., Chantigny, D., Gasser, M., Macdonald, M., Pelster, D., Bertrand, D. (2013). NH3 volatilization, soil NH4+ concentration and soil pH following subsurface banding of urea at increasing rates. Canadian Journal of Soil Science, 93(2): 261-268. 

[15] Chung, Y.M., Halim, Z.A., Raffay, R. (2012). Un-ionized ammonia detection system for water quality monitoring. IEEE Asia-Pacific Conference on Applied Electromagnetics (APACE), pp. 274-279.

[16] Oteri, O.M. (2020). The arduino e-kit as applied in engineering, science and technology e-learning. Sixth International Conference on e-Learning (econf), pp. 285-289.

[17] Drgona, P., Stefun, R. (2018). Application of stepper motors in CNC device. International Conference and Exposition on Electrical and Power Engineering (EPE), pp. 241-246.

[18] Marot, J., Bourennane, S. (2017). Raspberry Pi for image processing education. 25th European Signal Processing Conference (EUSIPCO), pp. 2364-2366.

[19] Nguyen, H., Nguyen, H.T. (2018). Study on creation of water sources supplying the inner river system in Hanoi city with continuous flow for cleaning the aquatic environment. MATEC Web of Conferences, 193: 1-10.

[20] Huong, N., Ohtsubo, M., Li, L., Higashi, T. (2007). Heavy metal pollution of the to-lich and Kim-Nguu river in Hanoi city and the industrial source of the pollutants. Journal of the Faculty of Agriculture, Kyushu University, 52(6): 141-146. 

[21] Phat, N.H, Hoan, N.D. (2020). NH3 Demo. Version 1.0. Online:

[22] National, I.O.E.H. (2020). Test report. Ministry of health, 1-5. Online:

[23] Phat, N.H., Hoang, L.T. (2019). Proposing recognition algorithms for hand gestures based on machine learning model. 19th International Symposium on Communications and Information Technologies (ISCIT), pp. 496-501.

[24] Phat, N.H., Huong N.T.T. (2019). Proposal gesture recognition algorithm combining CNN for health monitoring. Proceedings of 7th NAFOSTED Conference on Information and Computer Science (NICS), pp. 209-213. 10.1109/NICS48868.2019.9023804

[25] Phan, C.T., Pham, D.D., Tran. H.V., Tran, T.V., Phat, N.H. (2019). Applying the IoT platform and green wave theory to control intelligent traffic lights system for urban areas in Vietnam. KSII Transactions on Internet and Information Systems, 13(1): 34-51.