Development of a fuzzy logic-controlled system for home cultivation of sweet basil

As environmental pollution and the global population increase, and the COVID-19 pandemic becomes more severe, demands for indoor farming, especially home food gardening, have also increased. However, most research thus far has focused on large-scale food production, with very few studies having been conducted at the household scale. Also, the devices cultivating household crops with control systems in a continuous way, which minimize fluctuations of environmental conditions, have been rarely developed. Therefore, this study aimed to design a household cultivation system for sweet basil that is automatically and continuously controlled by fuzzy logic with a Raspberry Pi4. Three inputs (temperature, humidity, and growth stage) and seven outputs (fan, humidifier, heater 1, heater 2, LED red, green, and blue) were used with six rules, ensuring that three lights operated independently upon three growth stages. Simulation and actual operation were carried out, resulting in an appropriately controlled system that operated with few defects. In the case of an operation of the input variable, temperature and humidity were maintained at an average of 21.24 °C and 75.58%, respectively, and the LED operation for the growth stage was confirmed to be flawless. For verification of the designed fuzzy system, a comparison between the simulation and actual operation was performed to examine differences and identify problems. To this end, Pearson’s correlation coefficients were used, and the direction of correction of the fuzzy logic system was proposed. Through these results, the feasibility of a home cultivation system using fuzzy logic was demonstrated, and it is expected that further studies applying it will be conducted in the future.


Introduction
According to the United Nations, the global population is expected to reach 9.7 billion by 2050, rising continuously from 7.9 billion in January 2022 (Duro et al., 2020). Thus, potential food shortages should be mitigated by increasing food production to meet future demands from a growing population. To add to this issue, environmental pollution is also worsening. In this context, farming techniques, such as indoor farming, that require less land and decreased resistance to adverse climatic conditions, are potentially important solutions (Stein, 2021;Mitchell, 2022). In terms of controlled environment agriculture (CEA), indoor farming includes various methods such as vertical farms, zfarms, and greenhouses, all of which involve environmental conditions that are controlled for optimal cultivation.
Lately, household food gardening has become increasingly popular due to the COVID-19 pandemic (Mullins et al., 2021). In 2020, 18.3 million Americans participated in gardening for the first time, 49% of whom started because of the pandemic. Additionally, a survey in Canada showed that 51% of respondents grew at least one type of fruit or vegetable at home and 17.4% of those started home food gardening during the pandemic (Mullins et al., 2021). Despite the increasing importance of home food gardening, only a handful of studies have been conducted on this topic.
Within the fundamental aspects that are considered in agriculture and that should also be considered in domestic agriculture, are the phenological stages of growth. The BBCH growth stage scale is a framework used to judge plant growth and to define the phenological growth stages of various crops (Boyes et al., 2001). Accordingly, several authors have introduced this framework to various crop species (Meier et al., 2009;Adiga et al., 2019;Enriquez-Hidalgo et al., 2020).
Plant growth is modulated by light, which affects seed germination, transition to flowering, and adaptation to stressors. Plants can sense and respond to a wide range of spectra, spanning from UV-C (260 nm) to the far-red (720-780 nm) region. Combinations of different light wavelengths affect plant growth, metabolism, and morphology. Many studies have confirmed the effects of light quality on plants, for example, some light treatments increase the volatile content, mass, and antioxidant capacity in basil. Additionally, narrow-bandwidth illumination can induce discrete changes in volatile composition that affect sensory quality in commercial herbs (Carvalho et al., 2016). Furthermore, studies show that the appropriate use of LEDs improves production efficiency via phytochemical enrichment (Taulavuori et al., 2017). Recently, a novel light combination was proposed (Rihan et al., 2020). Generally, 450 nm blue light is used commercially, but two absorbance peaks were observed in the photosynthetically active radiation region at 435 nm and 665 nm. Therefore, a ratio of 1.4 blue to 1.0 red increased growth and yield compared to a 1:1 blue: red ratio.
Despite the apparent importance of light for plant growth, very few studies have tried to adjust light quality according to different plant growth stages.
To control actuators and enhance the effectiveness of the operation, the system needs to be operated by some logic. Fuzzy logic has been used in various appliances such as electric rice cookers and air conditioners because it is free from the binary approach (i.e., it records the degree of validity) (Badami and Stephanou, 1997). Unlike fuzzy logic, crisp logic is the same as Boolean logic (i.e., either 0 or 1). By contrast, fuzzy logic is continuously valued, has membership functions, and considers the degree of membership. Fuzzy logic is highly effective in feedback control systems and is generally easy to implement. The computational structure of fuzzy logic is composed of fuzzification, inference engines, and defuzzification modules. For example, fuzzification translates the numerical values for temperature errors into linguistic value such as "low," "very low," "zero," "high," or "very high." A fuzzy inference engine infers fuzzy outputs using fuzzy implications and the rules of inference of fuzzy logic. Defuzzification uses the fuzzy output of the rules to generate an output formulated as "crisp" numeric values, which are used as the control input. The relationship between input and output is in the form of "If-Then" rules that are based on the dynamic performance of processes (Potdar et al., 2017). Fuzzy logic has been used to manage environmental conditions in a range of agricultural circumstances, such as general crop- (Singhala et al., 2014;Robles Algarıń et al., 2017;Xu et al., 2017;Lakhiar et al., 2018), pest- (Roussel et al., 2000;Shi et al., 2007;Jesus et al., 2008), and irrigation management (Dengel, 2013;Bannerjee et al., 2018;Daud et al., 2018;Jayalakshmi et al., 2020). However, no studies have reported applying fuzzy logic to manage light quality according to different plant growth stages. The closest example used the input variable "light" with three membership functions of "light low," "light optimal," and "light high" (Iliev et al., 2014).
A representative problem of indoor farming is the high cost of the initial setup for cultivating inexpensive crops, such as lettuce. Therefore, the discovery and improvement of competitive crops are essential. Sweet basil (Ocimum basilicum L.) could be such a species, according to multiple studies (Carvalho et al., 2016;Liaros et al., 2016). The demand for this herb is increasing with consumers' awareness of its unique aroma and taste, but arguably this demand is not yet at its peak. For this reason, this study aimed to set up a system to cultivate sweet basil.
Therefore, the goal of this study was to develop an automatically controlled home cultivation system using fuzzy logic so that it proves actuators operate appropriately according to sensors based on the designed logic in the systematic aspect. Several input and output variables were utilized, including growth stage and light quality, and several fuzzy system elements were designed for this purpose. In many existing studies using fuzzy system, there was no proposed modification method to examine the vulnerable components of a given fuzzy logic design. Furthermore, most studies only simulated systems and did not actually implement them. That's why it has been hard to find out if the system can be actually operated as intentioned in reality. Hence, this study aimed to: (1) design an automatic cultivation system targeting a specific species suitable for home food production; (2) control a system using fuzzy logic considering light quality associated with plant growth stage; (3) compare the simulation results to an actual operation; and (4) assess fuzzy logic using Pearson's correlation coefficient to find out the weakness of the designed fuzzy logic.

Target crop
As mentioned in the introduction, the target crop in this study is sweet basil. The conditions for cultivating sweet basil in an environment controlled by a fuzzy logic system were selected considering its market value. According to the herb gardening guide from the University of Illinois Extension website, sweet basil is sensitive to cold temperatures and therefore its media temperature should be at least 21.1°C (Illinois, 2022). Furthermore, basil growth becomes stunted under 10°C, with damaged and blackened leaves. In terms of humidity, most plants require 45-75%, but basil requires 60-65% and it rapidly perishes in dry airflow (Luv2garden, 2020). The fuzzy logic control system was designed based on these environmental preferences.

Designing the cultivator
The cultivator was designed to be employed in two ways in order to use of space more efficiently: standing and wallmounted ( Figure 1). Figure 1 shows concept images of the two ways in a cultivator, virtually showing its expecting uses. The product was prototyped using acrylonitrile butadiene styrene (ABS) material. The structure of the cultivator was partially closed but not completely sealed, with two detachable transparent acrylic covers attached by 12 magnets. Six magnets were set on the front and six were set on the back, where two holes were made in the edges in order to hang the cultivator on a wall. Four pots were placed in the cultivator, which is a reasonable quantity for growing herbs at home. Figure 2 shows the main components used in this study. A Raspberry Pi (Raspberry Pi 4 model B, Raspberry Pi Foundation, UK) was selected to operate and control the system since nearly real-time data can be saved from multiple sensors and it enables additional complicated applications in the long term. A DHT11 sensor was used to measure temperature and humidity, while two heaters were used to control the temperature. A humidifier was 3D printed and inserted into the cultivator, and an ultrasonic transducer was attached to it. The humidifier was used to control both temperature and humidity. A fan was added for ventilation and to decrease the temperature in combination with the humidifier. In addition, RGB LEDs were tailored to independently control the light quality. For analog control of the system, a ULN2003 stepper motor driver board was used for the LEDs, an IRF520 MOSFET driver module for the heaters, a Grove water atomization module for the humidifier, and an NPN transistor (C3198) for the fan. The connection between the Concept design for the two modes of operation: standing (left), and wall-mounted (right). Figure 3. In terms of power supply, three 12 V AC/DC adapters were added for the heaters, as well as one 5 V SMPS for the humidifier, DHT11, and the MOSFET module, connected in parallel.

Raspberry Pi and the other components is shown in
The fabricated system is shown in Figure 4. RGB LEDs were placed on top to provide light to the plants, a humidifier was placed on the upper side of the right pillar, and a fan motor was installed below the humidifier to spread the mist. To simplify the variable name, mist and humidifier were used interchangeably.
A temperature and humidity sensor was placed on the upper side of the left pillar, at a distance from the mist. The heaters were attached to the lower side of each pillar to deliver heat evenly.

Fuzzy logic system
A fuzzy logic system was used for analog control of each environmental condition. The system was designed from the  Circuit design between Raspberry Pi and the other components. Composition of the main components in the cultivator. important environmental elements and they can be controlled by a fan, a mist, a heater. Secondly, this study selected the time duration as plant growth levels which means numerical inputs instead of images representing morphological characteristics. Red, blue, and green LEDs should be independently modulated upon plant growth levels. For these reasons, the system consisted of three inputs and seven outputs, as shown in Figure 5. The input variables were temperature, humidity, and growth stage and the output variables were the fan, mist, heater 1, heater 2, red LED, green LED, and blue LED. These variables were divided into two groups. Group 1 included temperature and humidity, as well as the related outputs, fan, mist, heater 1, and heater 2. Group 2 included the growth stage and its related outputs red LED, green LED, and blue LED. The novel point is that three different colored LEDs were independently assigned as output variables to control light quality depending on the growth stage.
To perform fuzzification, the three input variables (temperature, humidity, and growth stage) were set. The growth stages of many crops have been studied according to the BBCH scale; however, that is not the case for sweet basil. In this study, the plant growth level was defined until harvest, which was considered to be the edible stage. Therefore, growth was partitioned into three stages according to phenotypic characteristics, which correspond to numeric codes on the BBCH scale: 09 (cotyledons or coleoptile break through the soil surface), 10 (first true leaf emerges from coleoptile; or cotyledons completely unfold), and 51 (inflorescence or flower buds become visible). These were used as the three growth s t a g e s o f g e r m i n a t i o n , v e g e t a t i v e g r o w t h , a n d flowering, respectively.
The membership functions (m) for the input variables were as follows. The name of the member function had the structure [u variable level]. As all the membership functions were designed based on basil cultivation requirements, the system identified an average temperature of 20-25°C and 55-65% humidity. Furthermore, because germination periods vary between seeds, the germination growth stage was set at five to seven days. Although the flowering stage depends on crop management, it generally begins on the 35 th day, wherein the vegetative stage comes to an end. Accordingly, each input variable had three levels so that nine formulas could be obtained. The combined graph for the three input variables is shown in Figure 6. Schematic design of the input and output variables.
In order to control the degree of operation of the device, pulse width modulation (PWM) was used to produce a continuous output with digital means. PWM uses a square pulse wave whose pulse width is modulated, leading to variations in the average value of the waveform. The average value of the signal y) directly depends on the duty cycle, which is Frontiers in Plant Science frontiersin.org the proportion of "HIGH" time to regular interval or "period" of time and is expressed as a percentage of "on" time. A lower duty cycle yields lower power so that the power stays "off" for most of the time. As all of the output variables were used in combination with continuous outputs, the x-axes ranged from zero to 100% on the basis of duty cycles. The average value of the signal is as follows: Where f(t) is the waveform with period T, y min is the low value, y max is the high value, and D is the duty cycle.
Regarding LEDs, several factors had be considered when setting the three light regimes, including conflicting reports on their effects. For example, Aldarkazali et al. (2019) asserted that green light interferes with the growth and development of basil and that BR LEDs have a significant positive effect on the quantity and quality of essential oils that can be extracted from this crop. On the other hand, Carvalho et al. (2016) stated that RGB lighting and BR lighting do not affect the basil phenotype. In this study, green light was not excluded because it can penetrate the plant canopy, meaning that it helps plant growth under foliage or within proximity to other plants. Blue was expected to remain in a higher position except during the flowering stage, which was dominated by red and green light.
The name of each member function for the output variable was the same as in the input method. For the seven output variables, four variables were defined by grouping each of the heater (heater 1, heater 2) and LED (red, green, blue) variables, which have overlapping member functions. All of the output variables were configured in three steps to obtain 12 formulas as in the following equations; the combined graph is shown in Figure 7.
After fuzzification, the fuzzy rules were applied as the basis of the controller to manage the output variables. The fuzzy rules were then divided into two groups. In Group 1, the fan and heater were configured to run quickly and powerfully to dissipate heat in cold or high humidity conditions. Simultaneously, the humidifier was activated to prevent it from drying out and becoming cold. When the temperature and humidity reached the average, all of the devices operated at the average speed.
Group 2, however, needed a rule for lighting. Blue light dominated the germination and vegetative phases. Because germinating seeds require a darker space, we set the blue light to average and the rest to low during this period. Vegetative growth requires higher levels of light so we adjusted the blue light to high and the rest to medium. During the flowering stage, the red light should be more intense and it was configured accordingly.  After the input variables had undergone fuzzification and rules-based inference, clipping of the membership functions of the output variables was carried out. Following this, the clipped results were aggregated to be defuzzied. The defuzzied values were calculated using the centroid technique as the crisp output for control. All of the fuzzy rules are shown in Tables 1 and 2. Simulation and verification were performed to verify the operation of the system through hardware design and fuzzy logic. Temperature and humidity were repeatedly measured and recorded inside and outside the cultivator for 40 days. The cultivator was placed within an office similar to a home environment. For statistical analysis, inter-quartile range (IQR) was used to eliminate outliers. The simulation was implemented using the Fuzzy Logic Toolbox provided by MATLAB (R2020b, MATLAB 9.9, MathWorks), and the actual operation was conducted with Python 3.6 in consideration of its commercial feasibility.

System simulation
The Fuzzy Logic Toolbox shows the simulation results in a graphic called "surface view" using the input fuzzy membership function and rules (Figure 8). The fan was designed to operate on its lowest setting when the temperature and humidity reached the average. The cross on the surface view indicates that the fan worked as intended. The mist was supposed to operate less when the temperature was low or the humidity high, and operate more when the temperature was high or the humidity low. The surface view shows that this was the case. The heaters were set to the opposite rules than those of the mist, and the simulation results confirm they operated as expected. Since the LED was the result of one input (the growth stage), a two-dimensional view was generated (Figure 9). Thus, the intended output of red, green, and blue appeared according to the growth stage.

Operation analysis Verification of system behavior
The temperature and humidity results are shown in Table 3. The outer temperature exhibited greater variations compared to the inner temperature. Within the cultivator the temperature ranged between 16 and 31°C, with an average of 21.2°C, while the external temperature ranged from 12 to 31°C with a mean value of 19.8°C. Ideally, basil should be grown between 21 and 26°C , and certainly not below 18°C. Therefore, basil should have a survival rate > 75% within this cultivator, given that the first and third quartiles were 18 and 25°C, respectively. By contrast, basil would have a survival rate < 75% outside the cultivator because the first, second, and third quartiles were 16, 18, and 23°C, respectively. This demonstrates that the environment within the cultivator was more stable than that outside it.
The outer humidity level was substantially lower than that within the cultivator. The difference in humidity inside and outside was much more pronounced than that in temperature ( Figure 10). Research shows that basil withers rapidly in dry conditions (Luv2garden, 2020) and the optimum humidity for basil is between 60% and 80%, which is higher than that required for many other plants (Mijani et al., 2013;Pennisi et al., 2019;Song et al., 2020). The outer humidity was affected by human activities and its changes were unpredictable. The inner humidity measurements showed a pattern suggesting the humidifier worked as intended; when the humidity was reduced, the humidifier was switched on in order to increase it. Therefore, the conditions within the cultivator were more conducive to basil growth than those outside it as the mean inner and outer humidity levels were 75.58% and 16.57% respectively. The differences between the outer and inner temperature and humidity are presented in Figure 10.
Since the growth stage is a fixed value, the operation of the LEDs was checked to determine proper functioning. Table 4 shows the values of each LED duty cycle automatically controlled by the fuzzy algorithm according to the growth stage up to the 40 th day. Following the initial germination period, (Days 5 to 10) as the growth stage changed to vegetative growth, a change was also confirmed in each LED value. The duty cycle of red and green increased from 22.5% to 60%, and the duty cycle of blue increased from 60% to 87.36%. This confirmed that the blue LED value was greater than that of the other LEDs, in accordance with the fuzzy rule condition.  The duty cycle of each LED converged to a similar level until the 35 th day (i.e., the flowering period) and was automatically controlled until the 40 th day, and then maintained. From the beginning of vegetative growth, the red and green duty cycle increased from 60% to 87.36% and the blue duty cycle decreased from 87.36% to 22.59%, confirming that the fuzzy control worked well. The duty cycle of red and green light was lower than that of blue at a ratio of 2.66 for seedlings and 1.46 for asexual growth, as intended. Furthermore, the ratio of red and green at flowering was 3.87, which was higher than that of blue.

Comparison between simulation and actual operation
Each cross-section from the simulated surface view was overlaid onto the graphs of the actual operation in order to visualize any differences ( Figure 11). In the case of LED, it was only necessary to consider the change in the growth stage, so it was excluded from the variables for this analysis. In each graph, the dots represent actual operational data and the shaded areas represent the simulation. The actual behavior of temperature ( Figures 11A-C) was not measured at either end of the full range, but humidity ( Figures 11D-F) was shown to be almost identical to the simulation over the full range. When analyzing only the actual operational data, a clear correlation was observed between the expected and observed humidity values, but the temperature was ambiguous.
Independent of the trend, the fuzzy system was shown to operate as designed when looking at the graph alone. However, when viewed together with the simulation results, other interpretations are possible. The fan and mist were mostly Kim et al. 10.3389/fpls.2022.999106 activated within the bounds of the simulated data in terms of temperature and humidity and each showed similar trends. The heater, on the other hand, greatly deviated from the bounds of the simulation, despite its tendency to increase operation as the humidity increased and to decrease as the temperature decreased. This means that the heaters worked inappropriately. In particular, the duty cycles of the heaters should have been much higher below 30°C and above 45% humidity. As the maximum inner temperature was outside the ideal range, this inappropriate working range might have been better fit for this system.

Correlations between input and output variables
The following analysis was performed to examine the cause of the difference between the simulation and the actual operation. A heatmap of Pearson's correlation coefficients was drawn based on the hypothesis that the input and output variables were closely associated with each other, in order to investigate potential correlations and identify any design flaws in the fuzzy logic ( Figure 12). This study is ultimately designed to see if the system including LEDs can be controlled as coded. So, correlations were not estimated for LED because each LED should have been operated independently based on the growth stage.
The strongest correlation was between mist and humidity (-0.84), demonstrating a strong negative correlation between the two as the humidifier operated strongly when humidity decreased. Furthermore, mist had a strong positive correlation with temperature and a negative correlation with the heaters (0.67 and -0.55, respectively), that is, when the temperature increased, the humidifier operated to reduce it. Because both mist and heaters were output variables, it could be interpreted that the heaters were on and humidifier was off if the system was heated. In addition, temperature exhibited a strong correlation with humidity and the fan (-0.52 and 0.52, respectively), that is, at lower temperatures, humidity remained high and as the temperature fell, the fan operated less. Considering all of these together, as the temperature increased, the fan and humidifier were operational, while the heaters were switched off in order to cool the cultivator.

FIGURE 9
Surface view of the fuzzy logic simulation. (A) LED (red, green), (B) LED (blue). The variables that were moderately correlated were: temperature-heaters, humidity-heaters, mist-fan, and humidityfan. As the temperature increased, the heaters considerably reduced their operations. The mist and fan were coupled because they were both output variables. As humidity increased, the heaters operated to a greater extent, while the fan was moderately active. Lastly, the fan and heaters exhibited no correlation (correlation coefficient = 0.01).
The following problems were identified based on the correlation coefficients. First, the fan should have been strongly correlated with both the mist and heaters, since in addition to the fan, the mist and heater significantly enhanced their temperature and humidity control. However, Pearson's correlation coefficients indicate that this was not the case (0.37 and 0.01, respectively). This shows that the system entirely missed the correlation between the heaters and the fan. Although the mist and fan exhibited a positive correlation, it was still weak. Ideally, both  coefficients should have been > 0.5. To address this, the rules for the fan, mist, and heaters should be redesigned and applied so that humidity and temperature can be effectively controlled. Second, humidity and the fan had a lower correlation than expected, which was presumably why humidity remained > 65%. This could be addressed by modifying the rules and membership functions for humidity and the fan. Finally, the correlation between temperature and the heaters was far weaker than expected. The heaters and mist had a direct effect on the temperature, which should mean strong correlations with temperature. However, the Comparison of simulated and actual operational results. (A) Temperature-fan, (B) temperature-mist, (C) temperature-heaters, (D) humidity-fan, (E) humidity-mist, and (F) humidity-heaters.

FIGURE 12
Heatmap of correlations between input and output variables based on Pearson's correlation coefficients.
heaters showed a moderate correlation of -0.38 with temperature, while mist showed a strong correlation of 0.57. If the rules and membership functions for temperature and heaters were modified, the desired results could be achieved. Considering these solutions, it is expected that overall performance improvement will be achieved if modifications to the operation of heater and fan are performed.

Conclusion
We successfully developed a fuzzy logic-controlled system for the home cultivation of sweet basil. Specifically, the light quality was controlled by selecting three growth stages, according to the BBCH scale, as an input variable and assigning three lights (red, blue, and green) as independent output variables. Analysis of the simulation and actual operation data was conducted and most variables, except for the heaters, operated as intended. In order to identify potential design flaws in the fuzzy logic, the correlations between the input and output variables were investigated using scatter plots and Pearson's correlation coefficients. As a result, the correlations between the heaters and the fan were the weakest. The fuzzy logic control could be improved with appropriate modifications to the operation of heaters and fan. In future studies, the weakest elements of the fuzzy logic and re-implementation of the control system would be necessary for the improved operation of the system. Furthermore, an RGB camera could be used to detect the plant growth stage as another fuzzy logic input parameter instead of time duration. This could allow for a more accurate adjustment of output variables according to environmental conditions as the speed of plant growth can differ among individuals.

Data availability statement
The raw data supporting the conclusions of this article will be made available by the authors, without undue reservation.

Author contributions
J-SK designed the study and performed lab experiments. WJ and SP made hardware. J-SK and MY drafted the manuscript. MY reviewed and edited the manuscript. All authors contributed to the article and approved the final manuscript.

Funding
This work was supported by the National Research Foundation of Korea(NRF) grant funded by the Korea government(MSIT) (No. 2022R1C1C2005959).