Improving Aquaculture Water Quality Using Dual-Input Fuzzy Logic Control for Ammonia Nitrogen Management

: In this paper, a closed-loop control system using dual-input fuzzy logic theory is proposed to improve the water quality of aquaculture. The new closed-loop control system is implemented on a Raspberry-Pi-embedded platform using Python programming. The proposed closed-loop control system integrates an RS485 function, a database transfer module, a simulating variable group function, and a trigger function import to achieve savings in human resources, power, and water consumption. The proposed closed-loop control system is equipped with an ammonia nitrogen sensor and solenoid valves for the water exchange. The experimental results demonstrate that the intelligent controller can rapidly eliminate ammonia nitrogen within the range of 2.0 ppm and maintain robust control in response to changes in ammonia nitrogen excretion from a school of ﬁsh. The experimental results provide insights into the relationship between tank capacity, water exchange solenoid valves, and ammonia nitrogen degradation time, which can be used to optimize aquaculture density and improve industrialization. The experimental results demonstrate that the savings for power and water can be achieved above 95%.


Introduction
Aquaculture is one of the fastest-growing sectors of agriculture, with an increasing demand for fish and seafood.However, aquaculture production is subject to water quality problems that can affect the survival and growth of fish.One of the most important water quality parameters for aquaculture is ammonia, which is toxic to fish at high concentrations.Therefore, controlling ammonia levels is critical for successful aquaculture production.
Traditional methods for monitoring and controlling water quality in aquaculture involve manual labor and periodic sampling, which are time-consuming, expensive, and often lead to delayed responses to water quality problems.Therefore, an intelligent control system that can automatically monitor and adjust water quality parameters, such as ammonia, is necessary to optimize aquaculture production and reduce costs.
In recent years, fuzzy logic control has been widely used in industrial control systems due to its ability to deal with uncertainty and nonlinear systems.In this paper, we propose a closed-loop control system based on two-dimensional fuzzy logic theory to control ammonia levels in aquaculture.The proposed closed-loop control system is implemented on a Raspberry-Pi-embedded platform using Python programming.The proposed closed-loop control system integrates an RS485 function, a database transfer module, a simulating variable group function, and a trigger function import to achieve savings in human resources, power, and water consumption.
In this paper, we provide an experiment to validate our proposed approach with an ammonia nitrogen sensor and solenoid valves for the water exchange.The experimental results demonstrate that the intelligent controller can rapidly eliminate ammonia nitrogen within the range of 2.0 ppm and maintain robust control in response to changes in ammonia nitrogen excretion from a school of fish.The results provide insights into the relationship between tank capacity, water exchange solenoid valves, and ammonia nitrogen removal time, which can be used to optimize aquaculture density and industrialization.
Various approaches have been proposed for monitoring and controlling water quality in aquaculture.Machine-learning-based models have been widely used for predicting water quality parameters, such as dissolved oxygen, temperature, pH, and turbidity.For example, Chen et al. [1] proposed a prediction model based on artificial neural networks (ANNs) to predict the water quality of shrimp ponds.However, these models did not consider ammonia levels, a critical parameter for aquaculture production.
Other approaches for controlling ammonia levels in aquaculture involve using chemical treatments, such as nitrification inhibitors and ammonia-binding resins.However, these methods can be expensive, and chemicals can negatively affect the environment.
Fuzzy logic control has recently been proposed for regulating aquaculture water quality parameters.For example, Zhou et al. [2] proposed a fuzzy logic controller for controlling dissolved oxygen levels in recirculating aquaculture systems.However, these approaches need to consider the two-dimensional nature of the problem, which involves both ammonia levels and aquaculture density.
Recirculating Aquaculture Systems (RASs) have been widely used in aquaculture to improve water quality and reduce environmental impacts [3].However, controlling water quality in RASs can be challenging due to the complexity of the system and the dynamic nature of water quality parameters.
Nagothu proposed a fuzzy logic control system for water quality control in RASs that considers multiple parameters, including dissolved oxygen, temperature, pH, and ammonia [4].The results showed that the fuzzy logic controller could maintain stable water quality within the desired range.
Hu et al. [5] proposed an intelligent decision support system for water quality assessment in aquaculture ponds.This system uses a combination of fuzzy logic and artificial neural networks to predict water quality parameters, including ammonia, and provide recommendations for water quality management.
Timmons and Ebeling provided a comprehensive overview of RASs, including system design and operation, water quality management, and environmental sustainability [6].Their book offers practical guidance for the design and operation of RASs and emphasizes the importance of water quality management for successful aquaculture production.
Boyd offered a comprehensive guide for bottom soils, sediment, and pond aquaculture [7].The book covers various aspects of pond aquaculture, including water quality management, nutrient cycling, and sediment dynamics.
Martins et al. [8] reviewed the new developments in RASs in Europe and provided a broad perspective on environmental sustainability.The authors discussed the benefits and challenges of RASs and provided recommendations for sustainable aquaculture production.
Minggong et al. [9] proposed an intelligent water quality monitoring and controlling system for aquaculture.Their system uses sensors and fuzzy logic control to monitor and adjust water quality parameters, including ammonia.The results showed the system could maintain stable water quality within the desired range.
Nouraki et al. [10] reviewed the use of machine learning models for predicting water quality variables in aquaculture.The authors discussed the advantages and limitations of different machine-learning algorithms and provided recommendations for future research.
The major novelty and research motivation are provided as follows: (1) In our past reviews about the applications of aquaculture management, we found that few results can be implemented and validated successfully.In this paper, an intelligent control system is proposed which can automatically monitor and adjust water quality to optimize aquaculture production and reduce costs.(2) Fuzzy logic control has been proposed as a promising approach for water quality control in aquaculture.The proposed results using nonlinear fuzzy logic control can be applied directly rather than the trial-and-error method in the traditional PID control.(3) In this paper, we use the Mamdani method for the fuzzy inference system (FIS).The Mamdani fuzzy inference method is presented by some linguistic variables and fuzzy sets, which makes them very similar to the natural language descriptions and easier to understand and implement.(4) For more reliable results, multiprogramming, and networked applications, the Raspberry Pi is used instead of a controller with lower power consumption.The RS485 function is a reliable protocol which is used by the developer of ammonia nitrogen sensors.
This paper will be organized as follows.The theoretical foundations and problem formulation are provided in Section 2. The methods and experiments are developed in Section 3. Some results are illustrated to show the main proposed results in Section 4. A conclusion is provided in Section 5.

Theoretical Foundations and Problem Formulation
This section introduces the concept of ammonia nitrogen, its impact on fish, the idea of a fuzzy controller, and its modularization and application.

Total Ammonia Nitrogen
Ammonia exists in two chemical forms in aquaculture water: un-ionized ammonia (NH 3 ) and ammonium ions (NH 4 + ).The combination of these two forms is called total ammonia or total ammonia nitrogen (TAN) [11].According to the relationship of NH 3 , NH 4 + , temperature, and pH values given in a table in [11], NH 3 accounts for 0.0080 of the total ammonia nitrogen value at a water temperature of 30 • C and a pH of 7.0.The converted NH 4 + value is 0.9920.In other words, if the NH 4 + value detected by the ammonia nitrogen sensor is 0.124 ppm, the NH 3 value is calculated as 0.01 ppm.

Effects of NH 3 on Fish Health and Survival Rate
The fraction of un-ionized ammonia increases with pH and temperature but decreases slightly with increasing salinity [11].Knowing the fraction of ammonia in its un-ionized form (NH 3 ) is essential because it is about 100 times more toxic to fish than that of the ionized form (NH 4 + ).NH 3 levels above 0.05 ppm can damage fish, and concentrations above 2.0 ppm can quickly cause fish to die [11].
NH 3 is toxic to fish because after it enters the blood, it oxidizes the Fe 2+ contained in hemoglobin molecules into Fe 3+ , which inhibits the oxygen-carrying capacity of the blood and can cause the suffocation and death of the fish in severe cases.Ammonia nitrogen mainly enters through the fish gill epidermis and intestinal mucosa, followed by the nervous system, causing damage to the liver and kidneys of fish, causing hyperemia on the body surface and viscera, muscle hyperplasia and tumors, and even hepatic coma and death.Even at low concentrations of ammonia, long-term exposure can damage gill tissue, causing the bending, adhesion, or fusion of gill filaments [11].
The amount of NH 3 a fish produces after feeding can be estimated [12].The total ammonia nitrogen emission per unit feeding intake of a 50 g grouper is 3.59 mg TAN/g feed [12], and fish consume an amount of food that is about 4% of the body weight per feeding [13].For example, feeding a 50 g spotted grouper 2 g of food will lead to the excretion of about 3.59 mg/g of TAN within the next 6-10 h.At a water temperature of 30 • C and a pH of 7, NH 3 accounts for 0.8% of this TAN.Assuming the tank holds 20 L of water, the concentration of NH 3 is estimated to be 0.003 ppm.
The fishery water quality standards of China (GB11607-1989) stipulate that when the concentration of NH 3 is less than 0.02 ppm, it will not affect fishes' biological life activities such as fish growth and reproduction.The maximum concentration allowed for NH 3 is 0.1 ppm.Above this level, different fish are affected differently but all are affected negatively.For example, when it reaches 0.289 ppm, all carp will die.At 0.46 ppm, it will poison all tilapia.A concentration of 0.97 ppm will damage the gills, liver, and kidneys of grass carp, and their growth will be restricted [14].With regard to the above international standards, this article sets the maximum allowable value of NH 3 concentration in aquaculture water to 0.1 ppm, and the maximum is not more than 0.2 ppm.

The Closed-Loop Control System with Disturbance Term
Figure 1 shows a closed-loop control system with a sensor [15].The NH 3 value and the rate of change, d/dt, of the NH 3 are the dual inputs to the fuzzy logic operation of the fuzzy controller.The number of solenoid valves in the drainpipe is deduced.The valves are used to increase or decrease the displacement, and the concentration of NH 3 in the water is thereby changed.Some blocks are explained as follows: d/dt denotes the rate of change for NH 3 , fuzzy controller denotes the use of fuzzy rule to calculate the number of pipes, solenoid valve denotes the execution of switch of valve, water tank denotes the process of the experiment tank, and sensor denotes the detection of NH 3 .
feeding [13].For example, feeding a 50 g spotted grouper 2 g of food will lead to the cretion of about 3.59 mg/g of TAN within the next 6-10 h.At a water temperature of 30 and a pH of 7, NH3 accounts for 0.8% of this TAN.Assuming the tank holds 20 L of wat the concentration of NH3 is estimated to be 0.003 ppm.
The fishery water quality standards of China (GB11607-1989) stipulate that when concentration of NH3 is less than 0.02 ppm, it will not affect fishes' biological life activit such as fish growth and reproduction.The maximum concentration allowed for NH 0.1 ppm.Above this level, different fish are affected differently but all are affected neg tively.For example, when it reaches 0.289 ppm, all carp will die.At 0.46 ppm, it will p son all tilapia.A concentration of 0.97 ppm will damage the gills, liver, and kidneys grass carp, and their growth will be restricted [14].With regard to the above internatio standards, this article sets the maximum allowable value of NH3 concentration in aqu culture water to 0.1 ppm, and the maximum is not more than 0.2 ppm.

The Closed-Loop Control System with Disturbance Term
Figure 1 shows a closed-loop control system with a sensor [15].The NH3 value a the rate of change, d/dt, of the NH3 are the dual inputs to the fuzzy logic operation of fuzzy controller.The number of solenoid valves in the drainpipe is deduced.The valv are used to increase or decrease the displacement, and the concentration of NH3 in water is thereby changed.Some blocks are explained as follows: d/dt denotes the rate change for NH3, fuzzy controller denotes the use of fuzzy rule to calculate the number pipes, solenoid valve denotes the execution of switch of valve, water tank denotes t process of the experiment tank, and sensor denotes the detection of NH3.In the event of a disturbance, the stability and robustness of the control system crucial for maintaining the total ammonia nitrogen value within the fish tank using t closed-loop control system.Figure 2 simulates the random uncertainty of the fish's exc tion after feeding and how its impact on the robustness of the fuzzy controller is eva ated.The block of TAN of fish denotes some disturbance of the fish's excretion.Here, total ammonia nitrogen value in the fish tank is observed and the time it takes to redu NH3 to zero is evaluated.These values are used to calculate the concentration of ammo nitrogen in the aquaculture system and maintain the maximum level under the upp limit of 2.0 ppm for fish survival (the reference for aquaculture industries).In the event of a disturbance, the stability and robustness of the control system are crucial for maintaining the total ammonia nitrogen value within the fish tank using the closed-loop control system.Figure 2 simulates the random uncertainty of the fish's excretion after feeding and how its impact on the robustness of the fuzzy controller is evaluated.The block of TAN of fish denotes some disturbance of the fish's excretion.Here, the total ammonia nitrogen value in the fish tank is observed and the time it takes to reduce NH 3 to zero is evaluated.These values are used to calculate the concentration of ammonia nitrogen in the aquaculture system and maintain the maximum level under the upper limit of 2.0 ppm for fish survival (the reference for aquaculture industries).

Fuzzy Theory
According to Zadeh's proposed fuzzy theory [16] and Mamdani's commentary on fuzzy logic control (FLC) [17], FLC is an easy-to-control nonlinear controller with advantages of adaptation, robustness, and fault tolerance, making it suitable for systems with nonlinear, time-varying, and uncertain modes.
Qiao et al. [18] used nonisometric triangular membership functions and local linearization techniques to approximate FLC as a traditional PID controller.Lewis et al. [19] adopted a nonisometric triangular membership function to analyze the relationship between input and output, allowing FLC to provide some nonlinearity.
The advantage of using fuzzy control theory is that the mathematical model of the controlled system is based on expert knowledge and operator experience.This knowledge is then transformed into "IF-THEN" semantic control rules.FLC generates an output that brings the controlled system to a stable state by applying fuzzy inference rules to these control rules.

Fuzzy Theory
According to Zadeh's proposed fuzzy theory [16] and Mamdani's commen fuzzy logic control (FLC) [17], FLC is an easy-to-control nonlinear controller w vantages of adaptation, robustness, and fault tolerance, making it suitable for s with nonlinear, time-varying, and uncertain modes.
Qiao et al. [18] used nonisometric triangular membership functions and local ization techniques to approximate FLC as a traditional PID controller.Lewis et adopted a nonisometric triangular membership function to analyze the relations tween input and output, allowing FLC to provide some nonlinearity.
The advantage of using fuzzy control theory is that the mathematical mode controlled system is based on expert knowledge and operator experience.This kno is then transformed into "IF-THEN" semantic control rules.FLC generates an outp brings the controlled system to a stable state by applying fuzzy inference rules t control rules.

Establishing the Structure of the Fuzzy System
The architecture of the fuzzy system can be defined through the following va [20]: (1) Input values <1> NH3, with the units of ppm; <2> d(NH3)/dt, with the units of ppm/sec.
(2) Output variable change water (CW): The amount of water that can be cha proportional to the number of fresh water inlet pipes.The more water pipes t available, the greater the amount of water change that can be provided.

Establishing the Structure of the Fuzzy System
The architecture of the fuzzy system can be defined through the following variables [20]: (1) Input values <1> NH 3 , with the units of ppm; <2> d(NH 3 )/dt, with the units of ppm/sec.
(2) Output variable change water (CW): The amount of water that can be changed is proportional to the number of fresh water inlet pipes.The more water pipes that are available, the greater the amount of water change that can be provided.

Defining the Fuzzy Sets of Input and Output
The defined fuzzy sets of input and output variables are as follows [20]: Output: (CW), the number of water pipes = {4 p, 3 p, 2 p, 1 p, 0 p}.

Setting the Membership Function
According to the practical and expert operational experiences, we formulated the membership function as follows [20]: (1) The membership function of NH 3 is shown in Figure 3.
(2) The membership function of d(NH 3 )/dt is shown in Figure 4.
(3) The membership function of CW is shown in Figure 5.

Establishing the Fuzzy Rules
The fuzzy data inference, the decision-making rules, the field personnel's operating experience, and the expert experience given in the literature can be formulated into the following fuzzy laws [20].
(1) If (NH 3 is low) and (d(NH 3 )/dt is down) then (CW is P0).(2) The membership function of d(NH3)/dt is shown in Figure 4. (3) The membership function of CW is shown in Figure 5.   (2) The membership function of d(NH3)/dt is shown in Figure 4. (3) The membership function of CW is shown in Figure 5.   (2) The membership function of d(NH3)/dt is shown in Figure 4. (3) The membership function of CW is shown in Figure 5.

The Fuzzy Rule Table
The contents of the laws are provided in the rule table (Table 1) and are based on the fuzzy rules given in [20].[20] The CW water pipe changes are calculated according to the following rules.then (CW is P2).( 8) If (NH3 is high) and (d(NH3)/dt is no) then (CW is P3).( 9) If (NH3 is high) and (d(NH3)/dt is up) then (CW is P4).

The Fuzzy Rule Table
The contents of the laws are provided in the rule table (Table 1) and are based on th fuzzy rules given in [20].[20] The CW water pipe changes are calculated according to the following rules.

Embedded Systems
According to Marwedel's principles of design [21], the embedded system is organized to integrate the various elements of the information processing and computing system with the controlled environment.The information processing system is embedded in a closed product, and its main features are immediacy, reliability, and efficiency.These features were adapted in the present design as follows.

Adapting the Raspberry Pi Platform
(1) The experiment utilized a Raspberry Pi 4B with a 4GB LPDDR4-3200 SDRAM, an external 128 GB SSD, and installed with Berry Boot v2.0 multi-boot.We also used the built-in Raspberry Pi OS operating system to protect the Raspberry Pi from power failure, accidents, or other disturbances during operation.It is crucial to note that the Pi OS operating system was necessary for the experiment to run smoothly, avoid unnecessary troubles, and protect the research results from destruction or loss.(2) To avoid errors caused by network interruptions, rather than setting up a judgment program in the cloud, we directly connected the Raspberry Pi to relays for real-time situation processing.

Database
We built a database on Google Sheets connecting to the Google Cloud Plat via IFTTT.This database can record the test data uploaded by the fuzzy controller and draw a statistical chart of the three variables, NH 3 , d(NH 3 )/dt, and the number of pipes.

Research Methods
This study adopted two research methods: (1) actual sensor experiments and (2) simulation tests.

Ammonia Nitrogen Sensor Experiment
To evaluate the fuzzy logic controller's (FLC) efficacy in controlling the ammonia nitrogen content, the following experiments were conducted.
(1) 1.89 ppm NH 3 was added to the experimental tank and the FLC's removal performance was observed and recorded.(2) NH 3 was randomly dropped into the experimental tank to simulate fish excretion and the FLC's removal performance was observed and recorded.

Simulation Test
A simulation program was developed using Python and the following variables were added to observe the FLC's efficacy in controlling the ammonia nitrogen content: (1) The water tank's capacity was set at 20 L, 40 L, 60 L, 80 L, and 100 L.
(2) Data was collected from the sensors at different time intervals: one, two, three, five, and seven minutes.
(3) The maximum number of fish that the tank can accommodate without exceeding the NH 3 concentration limit was explored (i.e., the maximum allowable population density).

Discussion on Power and Water Consumption
The discussion on power and water consumption was divided into two sections: (1) Power Consumption: Different fish species, body sizes, weights, and other factors will affect ammonia nitrogen excretion.This study used spotted groupers, each weighing 50 g, as an example.The experimental conditions of the water and power consumption between FLC and ordinary pumps were compared:

<1>
A general control pump with only an electrical switch and no ammonia nitrogen was defined as the test control.<2> The general control pumps used a set of in-water and out-water pumps.These two sets were used to change the water at the rate of about 400 L/h for 24 h.<3> The water and power consumption using FLC was recorded after each fish feeding.Subsequent research can use this model to set up different parameters to obtain the desired information.
(2) Water Consumption: The water consumption was measured during the experiment, and the water use of the FLC was compared with the results using ordinary pumps.

Experimental Equipment
Figures 7 and 8 show the ammonia nitrogen sensor and the ion controller used in the experiment, respectively.First, the ammonia sensors' data were transmitted to the ion controller, which sends the data to the Raspberry Pi via the RS485 communication device.After analyzing the FLC program in the Raspberry Pi, the number of pipe sets required was calculated, and the solenoid valve relays were triggered to change the water (Figure 9).The experimental equipment consisted of three water tanks arranged in a tiered stack.The upper tank was a clean water reservoir, the middle tank was the experimental tank with ammonia nitrogen, and the lower tank was a wastewater receptacle.The sets of water inlet and outlet pipes were oriented on opposite sides of the experimental tank in order to remove NH 3 effectively.The water tank level was maintained using solenoid valves with the same flow rate (Figure 10).The flow chart for the controller program design is illustrated in Figure 11.The specifications of all parts in our experiment are provided in Table 2.The selections are based on the accuracy and cost of our research.In this paper, the sensors of ammonia nitrogen were put into the center of tank to ensure the average concentration of NH 3 .For large fish ponds, some sensors should be put into suitable locations (corner and center) to obtain the available concentration of NH 3 .In this experiment, the independent variable was the number of water pipes and the dependent variable was the size of water pipes.

Ammonia Nitrogen Sensor Experiment
The primary purpose of this experiment was to observe the efficacy of the fuzzy logic controller in combination with the ammonia nitrogen sensor for controlling the concentration of NH 3 in water.In this experiment, we assumed that the transport delay could be neglected based on the short-distance environment.The time for a real-time response should be selected as 2 min with the refresh time of the sensor.

Standard FLC Test with a Concentration of 1.89 ppm NH 3
The initial concentration the solution used was 1.89 ppm, slightly under the previously defined limit of 2.00 ppm.(It was difficult to control the concentration to be exactly 2.00 ppm.)The solenoid valve's flow rate was 40.0 mL/s, and the sensor was set to collect data every two seconds.The experimental procedure of reducing the NH 3 concentration of 1.89 ppm to 0 ppm lasted 20 min and 38 s.The NH 3 , d(NH 3 )/dt, and relevant pipe statistics are shown in Figure 12.The figure is explained as follows: (1) In the beginning of the experiment when the initial NH 3 concentration was high, the FLC used four sets of solenoid valves to change the water.As the concentration decreased, the water flow necessary to continue reducing the concentration decreased, so the number of solenoid valves that were used decreased until the NH 3 concentration reached 0. (2) In the sixth cycle (from left to right), the concentration of NH 3 was 0.04 ppm, which is below the critical concentration of 0.05 ppm at which harm to fish occurs.At this point, only one solenoid valve remained open and the amount of water flow was relatively low.From then on, the water exchange continued at a slower rate until the contamination was completely cleared, thus allowing water to be conserved.
of 1.89 ppm to 0 ppm lasted 20 min and 38 s.The NH3, d(NH3)/dt, and relevant pipe s tistics are shown in Figure 12.The figure is explained as follows: (1) In the beginning of the experiment when the initial NH3 concentration was high, FLC used four sets of solenoid valves to change the water.As the concentration creased, the water flow necessary to continue reducing the concentration decreas so the number of solenoid valves that were used decreased until the NH3 concent tion reached 0. (2) In the sixth cycle (from left to right), the concentration of NH3 was 0.04 ppm, wh is below the critical concentration of 0.05 ppm at which harm to fish occurs.At t point, only one solenoid valve remained open and the amount of water flow w relatively low.From then on, the water exchange continued at a slower rate until contamination was completely cleared, thus allowing water to be conserved.

Randomly Adding Ammonia Nitrogen Solution to Imitate the Excretion of Fish
Once it was established that the FLC could effectively control the NH 3 concentration, the question of whether the FLC could respond appropriately by adjusting to varying levels of NH 3 similar to random fish excretions was explored.During this experiment, different doses of ammonia nitrogen solutions were randomly added into the tank, thereby presenting a disturbance variable.The response of the FLC was observed, especially to see whether this would cause any unexpected or unfavorable responses.
As shown in Figure 13, randomly dropping different doses of ammonia nitrogen solution into the bath caused the measured values to fluctuate, with the blue line of NH 3 , the red line of d(NH 3 )/dt, and the yellow bar indicating the pipe implementation rising and falling one after another.However, these values eventually returned to zero without any bugs or failures, thereby confirming the robustness and stability of the FLC and the solenoid valve operation.

Simulation Test
Replacing actual experiments with simulation programs will obtain approximate results for different variables, such as the tank capacity, the sensor data acquisition rate, and the aquaculture density.

Performance Using a Varying Water Tank Capacity
To observe the FLC's efficacy in relation to the volume of water in the tank, different amounts of water were used in the test: 20 L, 40 L, 60 L, 80 L, and 100 L. In this experiment, the initial NH 3 concentration was 1.89 ppm, the water flow rate was 40.0 mL/s, and the sensor data acquisition intervals were 2 min.
As shown in Figure 13, randomly dropping different doses of ammonia nitrogen so lution into the bath caused the measured values to fluctuate, with the blue line of NH3 the red line of d(NH3)/dt, and the yellow bar indicating the pipe implementation rising and falling one after another.However, these values eventually returned to zero without any bugs or failures, thereby confirming the robustness and stability of the FLC and the solenoid valve operation.

Simulation Test
Replacing actual experiments with simulation programs will obtain approximate re sults for different variables, such as the tank capacity, the sensor data acquisition rate, and the aquaculture density.

Performance Using a Varying Water Tank Capacity
To observe the FLC's efficacy in relation to the volume of water in the tank, differen amounts of water were used in the test: 20 L, 40 L, 60 L, 80 L, and 100 L. In this experiment the initial NH3 concentration was 1.89 ppm, the water flow rate was 40.0 mL/s, and the sensor data acquisition intervals were 2 min.
(1) For the 20 L water tank, it took 20 min and 38 s to reduce the concentration of NH from 1.89 ppm to 0.0 ppm, which was exactly the same time as the previous experiment's 20 min and 38 s, indicating that the simulation program's parameters have a certain reliability.The relationship of NH3, d(NH3)/dt, and the number of pipe sets is shown in Figure 14.(2) For the 40 L water tank, it took 52 min and 59 s to reduce the concentration of NH from 1.89 ppm to 0.0 ppm.The relationship of NH3, d(NH3)/dt, and the number of pipe sets is shown in Figure 15.(2) For the 40 L water tank, it took 52 min and 59 s to reduce the concentration of NH from 1.89 ppm to 0.0 ppm.The relationship of NH3, d(NH3)/dt, and the number o pipe sets is shown in Figure 15.(5) For the 100 L water tank, it took 139 min and 51 s to reduce the concentration of NH3 from 1.89 ppm to 0.0 ppm.The relationship of NH3, d(NH3)/dt, and the number of pipe sets is shown in Figure 18.(5) For the 100 L water tank, it took 139 min and 51 s to reduce the concentration of NH 3 from 1.89 ppm to 0.0 ppm.The relationship of NH 3 , d(NH 3 )/dt, and the number of pipe sets is shown in Figure 18.A statistical chart of the capacity of the water tank and the time required to lower the NH3 concentration is shown in Figure 19.As can be seen, the larger the water tank, the longer it takes to remove NH3.Implementing another solenoid valve should increase the water exchange rate and shorten the removal time.However, changing all the water in the tank with new water within 2 min (data catch time) did not help save water and power, and the FLC became useless.The actual usefulness of the FLC was in reducing the concentration of NH3 to a specified limit (from 2.0 ppm to 0.04 ppm) within a controlled time frame, where the time may be specified as 2 min, 30 min, 1 hour, or even a few days, depending on the tolerance to NH3 exhibited by the specific species of fish.A statistical chart of the capacity of the water tank and the time required to lower the NH 3 concentration is shown in Figure 19.As can be seen, the larger the water tank, the longer it takes to remove NH 3 .Implementing another solenoid valve should increase the water exchange rate and shorten the removal time.However, changing all the water in the tank with new water within 2 min (data catch time) did not help save water and power, and the FLC became useless.The actual usefulness of the FLC was in reducing the concentration of NH 3 to a specified limit (from 2.0 ppm to 0.04 ppm) within a controlled time frame, where the time may be specified as 2 min, 30 min, 1 h, or even a few days, depending on the tolerance to NH 3 exhibited by the specific species of fish.
the tank with new water within 2 min (data catch time) did not help save water and p and the FLC became useless.The actual usefulness of the FLC was in reducing th centration of NH3 to a specified limit (from 2.0 ppm to 0.04 ppm) within a controlled frame, where the time may be specified as 2 min, 30 min, 1 hour, or even a few depending on the tolerance to NH3 exhibited by the specific species of fish.

Observing the Efficacy of FLC using Different Data Acquisition Intervals
In this experiment, the following test conditions were selected: 0.86 ppm NH centration (close to the 0.9 ppm tolerance of groupers), a 100 L water tank, and a 40.0 water flow rate.Five different data acquisition intervals were tested: one min, two three min, five min, and seven min, and the time required to reduce the NH3 concent to 0 ppm was recorded.As shown in Table 3 and Figure 20, the longer the data acqu interval, the faster the removal of NH3.Using a longer acquisition interval appeared more efficient in terms of the time required; however, using a longer interval resul continually changing the water even after the NH3 had already been removed, lead more water consumption and waste.

Observing the Efficacy of FLC Using Different Data Acquisition Intervals
In this experiment, the following test conditions were selected: 0.86 ppm NH 3 concentration (close to the 0.9 ppm tolerance of groupers), a 100 L water tank, and a 40.0 mL/s water flow rate.Five different data acquisition intervals were tested: one min, two min, three min, five min, and seven min, and the time required to reduce the NH 3 concentration to 0 ppm was recorded.As shown in Table 3 and Figure 20, the longer the data acquisition interval, the faster the removal of NH 3 .Using a longer acquisition interval appeared to be more efficient in terms of the time required; however, using a longer interval resulted in continually changing the water even after the NH 3 had already been removed, leading to more water consumption and waste.Aquaculture density refers to the number of fish in a certain volume of water.The maximum aquaculture density is that which can accommodate the greatest number of fish in a certain size of tank while maintaining a safe NH 3 concentration.The maximum aquaculture density is relative to the species of fish in question, as different kinds of fish have different tolerances to ammonia nitrogen.
If the highest NH 3 concentration that can be tolerated by the fish and the volume of the water tank is known, the FLC can calculate the maximum number of fish allowable under these conditions and clean out the NH 3 within two hours.

Power and Water Consumption Comparison
According to Section 4.1.2,randomly dripping ammonia nitrogen solution into

Power and Water Consumption Comparison
According to Section 4.1.2,randomly dripping ammonia nitrogen solution into the fish tank was carried out to simulate fish excretion and compare the power and water consumption of the FLC with non-FLC pumps, and this required 24 h to change the water.
Table 4 shows the statistical data for the power and water consumed after one feeding time.The FLC only consumed 3.6 w per hour for each solenoid valve (12 V, 0.30 A), and each non-FLC pump (pump) consumed 3 w per hour.Comparing the consumption of power and water of the FLC and pump, it was 4.9% and 4.4%, respectively, so the power-saving rate was 95.1%, and the water-saving rate was 95.6%.If the number of feeding times are increased to two, four, or six times daily, then the power-saving and water-saving rates will steadily decrease, as shown in Table 5 and Figure 22.The relationship between the number of feeding times per day, the power savings, and the water savings.

Discussion
The experimental results demonstrated the strength, novelty, advancement, and in- The relationship between the number of feeding times per day, the power savings, and the water savings.

Discussion
The experimental results demonstrated the strength, novelty, advancement, and industrial applicability of the FLC's ability to control the ammonia nitrogen content of a reservoir of fish.Certain aspects of these results are discussed as follows.

Tests with No Disturbance Variable
In practical sensor measurements, the FLC successfully removed NH 3 in a short time when tested with a concentration of 1.89 ppm NH 3 .In simulated experiments using the same concentration, the FLC required a similar time to remove NH 3 , demonstrating the correctness of the formula used to derive the FLC model.

Tests with Disturbance Variables
Whether it was sensor measurement or simulation testing, the FLC showed no system failures or errors in the experiment when randomly introducing NH 3 to mimic fish excretion as an interfering variable.NH 3 at different doses could be cleared in a short time, demonstrating the robustness, controllability, and novelty of the FLC.It is also the world's first successful experiment applying an FLC to reduce ammonia nitrogen levels in aquaculture.

Aquaculture Density Estimation
We analyzed the relationship among the tank capacity, valve water flow rate, and NH 3 removal time using simulated test data.According to the scale of farming and fish survival, the parameters of these factors can be adjusted appropriately.For example, a modest increase in the valve water flow rate in larger tanks can shorten the NH 3 removal time.The water flow increase in the valve is based on the following formula (obtained through multiple simulation experiments) as a reference: V-P × S × t must be greater than 0. Otherwise, the FLC will not operate.
where V = The water tank capacity (in L); P = The number of irrigation pipes and solenoid valve sets (calculated by four sets); S = The water flow rate of the valves (in mL/sec); t = The sensor data acquisition time (in seconds).

Determination of Aquaculture Density Based on a Known Fish Excretion Rate
High-density aquaculture with high survival rates can be maintained because the NH 3 concentration can be controlled under the limit and rapidly reduced when necessary.Aquaculture density (an integer value) in a water tank can be determined based on the tolerance of fish to NH 3 (in ppm), the conversion factor of the fish excretion rate to NH 3 (in ppm), and the water tank capacity (in liters).The formula for calculating aquaculture density is as follows.
The deduced breeding density formula can be written as: T × V = f NH3 × F. The variables for the above equations are defined as: T = The tolerance of fish to NH 3 (in ppm).V = The capacity of the water tank (in L). f NH3 = The value of fish excretion converted into NH 3 (in ppm).F = The number of fish (an integer).
When estimating the number of fish by total weight, the number of fish can be obtained by rounding down to the nearest integer.

Determination of Aquaculture Density Based on an Unknown Fish Excretion Rate
When adopting an extensive aquaculture system in which the NH 3 levels of separate batches of fish are monitored with sensors, it is possible to determine a suitable stocking density for each batch, such that the NH 3 concentration remains within the fish's tolerance level or does not exceed 2.0 ppm.In this case, adopting a FLC system can effectively remove NH 3 within a short time, enabling optimal aquaculture density.

Assessment of Electric Power Consumption and Water Consumption
According to Table 3, it is evident that the power and water consumption of the FLC are significantly lower compared to the traditional method of continuously changing the water for 24 h a day using a water pump.The FLC system consumes minimal power due to using solenoid valves without pumps, resulting in a 97% reduction in power consumption and a 99% reduction in water consumption.Although wastewater recycling after purification is possible, this study does not address this issue.

Conclusions
This study presents a practical and effective method for determining the stocking density of fish in aquaculture.The proposed formulas and culture systems can be widely applied in aquaculture to ensure the optimal stocking density, survival rate, and NH 3 removal.The experimentation proved that the fuzzy logic theory can be used to control the ammonia nitrogen concentration.Overall, the results show that the proposed FLC system can effectively remove NH 3 and save power and water consumption compared to traditional non-FLC pumps.The simulation tests and random disturbance experiments confirm the system's robustness and stability.The ammonia nitrogen intelligent controller that was developed using the closed-loop control system and two-dimensional fuzzy theory can effectively and quickly remove NH 3 in the water.Within the range of 2.0 ppm NH 3 concentration, the intelligent controller exhibits a strong control ability when simulating changes in ammonia nitrogen excretion by fish schools.The simulated results suggest that FLC can significantly reduce manual labor and the consumption of power and water.These results may be a useful reference for the future industrialization of aquaculture.The correlation between water tank size and the number and placement of sensors will be considered an important coefficient to design the experiment.After suitable settling, the ammonia concentration can be distributed evenly.It is expected that similar methods can be used in subsequent studies to control other water quality parameters.The state-ofthe-art control technologies, such as adaptive control, an adaptive-network-based fuzzy inference system (ANFIS), neural networks, and machine learning, can be applied to this control issue in our future work.The memory of embedded hardware, the accuracy and reliability of sensors and devices, and generalizability are our limitations in our developed approach and results.Some relevant issues for water quality management and testing can be considered in our future work about this article [22,23].

Patents
The results of this research partially validate the recently published patent: "A method of intelligent control of ammonia nitrogen value in aquaculture water and its equipment".Taiwan Patent TWI785737 B, filed on 13 August 2021 and issued on 1 December 2022.

Figure 2 .
Figure 2. Ammonia nitrogen closed-loop control system with disturbance.

Figure 2 .
Figure 2. Ammonia nitrogen closed-loop control system with disturbance.

Figure 7 .
Figure 7.The appearance and dimensions of the ammonia nitrogen (NH4 + ) sensor used in this study.

Figure 7 .
Figure 7.The appearance and dimensions of the ammonia nitrogen (NH 4 + ) sensor used in this study.

Figure 7 .
Figure 7.The appearance and dimensions of the ammonia nitrogen (NH4 + ) sensor used in this study.

Figure 8 .
Figure 8.The appearance and size of the ammonia nitrogen (NH4 + ) controller used in this study.

Figure 8 .Figure 9 .
Figure 8.The appearance and size of the ammonia nitrogen (NH 4 + ) controller used in this study.

Figure 10 .
Figure 10.Connection diagram of this experiment.Figure 10.Connection diagram of this experiment.

Figure 11 .
Figure 11.Flow chart of controller program.

Figure 12 .
Figure 12.The relationship between NH3, d(NH3)/dt, and the number of pipes utilized.The ini concentration of NH3 = 1.89 ppm, and the flow rate is 40.0 mL/s.

Figure 12 .
Figure 12.The relationship between NH 3 , d(NH 3 )/dt, and the number of pipes utilized.The initial concentration of NH 3 = 1.89 ppm, and the flow rate is 40.0 mL/s.

Figure 13 .
Figure 13.The relationship between NH3, d(NH3)/dt, and the number of pipes utilized under con ditions of randomly disturbing the NH3 concentration.

Figure 13 .
Figure 13.The relationship between NH 3 , d(NH 3 )/dt , and the number of pipes utilized under conditions of randomly disturbing the NH 3 concentration.(1) For the 20 L water tank, it took 20 min and 38 s to reduce the concentration of NH 3 from 1.89 ppm to 0.0 ppm, which was exactly the same time as the previous experiment's 20 min and 38 s, indicating that the simulation program's parameters have a certain reliability.The relationship of NH 3 , d(NH 3 )/dt, and the number of pipe sets is shown in Figure 14.Mar.Sci.Eng.2023, 11, x FOR PEER REVIEW 15 of 23

Figure 14 .
Figure 14.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 20 L water tank.

Figure 14 .( 2 )
Figure 14.The chart of removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 20 L water tank.(2)For the 40 L water tank, it took 52 min and 59 s to reduce the concentration of NH 3 from 1.89 ppm to 0.0 ppm.The relationship of NH 3 , d(NH 3 )/dt, and the number of pipe sets is shown in Figure 15.

Figure 14 .
Figure 14.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 20 L water tank.

Figure 15 .( 3 )
Figure 15.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 40 L water tank.

Figure 15 . 2 Figure 16 .( 4 )
Figure 15.The chart of removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 40 L water tank.(3) For the 60 L water tank, it took 83 min and 35 s to reduce the concentration of NH 3 from 1.89 ppm to 0.0 ppm.The relationship of NH 3 , d(NH 3 )/dt, and the number of pipe sets is shown in Figure 16. .Mar. Sci.Eng.2023, 11, x FOR PEER REVIEW 16 of 2

Figure 16 .( 4 )
Figure 16.The chart of removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 60 L water tank.(4)For the 80 L water tank, it took 111 min and 59 s to reduce the concentration of NH 3 from 1.89 ppm to 0.0 ppm.The relationship of NH 3 , d(NH 3 )/dt, and the number of pipe sets is shown in Figure 17.

Figure 16 .( 4 )
Figure 16.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 60 L water tank.

Figure 17 .
Figure 17.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 80 L water tank.

Figure 17 .
Figure 17.The chart of removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 80 L water tank.

Figure 18 .
Figure 18.The chart of removal time, NH3, d(NH3)/dt, and the number of pipes utilized for the 100 L water tank.

Figure 18 .
Figure 18.The chart of removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 100 L water tank.

Figure 19 .
Figure 19.The relationship between the capacity of the water tank and the time required to r NH3.

Figure 19 .
Figure 19.The relationship between the capacity of the water tank and the time required to remove NH 3 .

Figure 21 .
Figure 21.The chart of FLC-aided removal time, NH3, d(NH3)/dt, and the number of pipes utili for the 20 L water tank with the maximum aquaculture density for the simulated random excre of a school of groupers.

Figure 21 .
Figure 21.The chart of FLC-aided removal time, NH 3 , d(NH 3 )/dt, and the number of pipes utilized for the 20 L water tank with the maximum aquaculture density for the simulated random excretion of a school of groupers.

Figure 22 .
Figure 22.The relationship between the number of feeding times per day, the power savings, and the water savings.

Table 1 .
The contents of the rules.

Table 1 .
The contents of the rules.
The Python programming language was installed in the Raspberry Pi OS operating system and the Visual Studio Code editing software was used to write the program codes.This enabled easy modification and error reminders while writing the program codes.(2)The following modules and functions were imported and set up to execute the main code:

Table 2 .
Specification of parts in this experiment.

Table 2 .
Specification of parts in this experiment.

Table 3 .
Different sensor data acquisition intervals and the NH 3 cleaning time.

Table 4 .
The comparison of power and water consumption between FLC and pump.

Table 5 .
The relationship between feeding times, power savings, and water savings.