Design of a peanut moisture detector based on STM 32 and MATLAB

doi: 10.9755/ejfa.2018.v30.i10.1833


INTRODUCTION
The nutritional value of peanut is high, and it has the functions of invigorating the spleen and nourishing the stomach, enhancing memory and aging resistance, and so on (Trabelsi et al., 2016).An important factor in evaluating and ensuring the quality of peanuts is moisture content.Peanuts are generally harvested in the late autumn (Wu et al., 2017).The moisture content of the newly harvested peanuts is 45%-50%.If the moisture content is too high, the peanuts will be mildew and produce toxic substances, causing a lot of waste (Zhao et al., 2015).However, too dry grain will cause the loss of commodity grain weight, also increase the consumption of manpower, energy and equipment, and may cause cracks and gelatinization particles to reduce grain quality after grain drying, so the moisture content should be dried to less than 10.5% before safe storage (Zhang et al., 2017).Water is especially important in the component part of the grain crop, and the main standard of the pricing is the moisture content of grain.Therefore, in the process of the harvest, drying and storage of peanuts, the scientific and accurate control of the moisture content of peanuts is important to ensure the high quality of peanuts and save the cost of drying (Bell et al., 2018;Cui et al., 2018;Hassan et al., 2018;Omymen et al., 2017).
According to whether the original state of the tested object is changed during the measurement process, the methods for grain moisture measurement can be divided into two categories: lossy detection and non-destructive testing.Lossy detection means that the sample needs to be physically crushed or the original state of the sample needs to be changed by chemical reaction when it is detected.While, non-destructive testing does not destroy the character of the sample and it detects the moisture content of the sample by its chemical, physical and optical properties (Liu, 2017;Wang et al., 2018).The commonly used methods for measuring the moisture content of peanuts at home and abroad include drying method, microwave method, nearinfrared method, neutron method, impedance method, and capacitance method (Anastassova et al., 2018;Khaleel et al., 2018;More et al., 2017;Sautrot-Ba et al., 2018).The drying method is a method to measure moisture according to the lost weight of the grain after heating.It has high detection The measurement accuracy and repetition rate of the existing peanut moisture detector are poor.To solve this problem, this paper designs a capacitive peanut moisture detector based on the capacitance method.Firstly, the structure of coaxial cylindrical capacitive moisture sensor is optimized.STM32 chip microcomputer is used in the main control chip, and the change in capacitance is transformed to change in frequency by capacitance detection circuit.The temperature and quality of peanut kernel are measured by DS18B20 temperature sensor and weighing sensor respectively.The influence of particle size, moisture content, temperature and volume density on the output differential frequency of capacitance sensor are studied by using the differential frequency method.In the range of moisture content of 4.3%-19.1% and temperature of 10°C-40°C, a mathematical model for the relationship among moisture content, temperature, and differential frequency is established.The prediction test of peanut kernel's moisture content shows that the measurement error of moisture content is within ±0.5%, indicating that the instrument has high measurement accuracy and repeatability when it is used to detect the moisture content of peanut kernels, and has a wide application prospect.
precision, but it needs to cut the peanut kernels into thin slices, the operation process is tedious and the drying time is long (Liu, 2017;Song et al., 2016).The microwave method is fast and suitable for on-line detection, but the detection results are easily affected by the density and shape of the tested samples (Feng et al, 2016;Wu and Han, 2016).The near infrared method is not in contact with the measured samples, and the measurement range is wide (Bai and Zhou 2018;Liao and Chen, 2016).It is suitable for detecting surface moisture.It is difficult to measure the real moisture content in the sample, and the price is more expensive, so it is difficult to be popularized.The advantage of the neutron method is that the sensitivity of is extraordinary for the high moisture section (Zhao et al., 2016).The detection will not interfere with the normal operation of the material and do not need to destroy the material structure, but the count ratio will cause great difference due to the external factors such as the origin, to a great extent, it is influenced by the factors such as the shape density of the measured grain, and the calibration is more troublesome, so it cannot be widely used; The impedance method is fast and accurate, but it is not suitable for on-line detection.The capacitance method has the advantages of fast response and low cost.It is widely used in the development of water detector in the market (Cui and Yan, 2017;Yang et al., 2017).
Foreign scholars have made some progress in the study of moisture content detection of peanut kernel (Cerullo et al., 2017).Microwave, near infrared and capacitance methods are used, but the domestic research has not been reported.In the commercialized moisture detector, the market share of foreign products is high.In China, the LDS series moisture detector manufactured by Shanghai Qingpu Oasis Detection Instrument Co., Ltd.adopts a structure of coaxial cylindrical capacitance sensor, which has poor repeatability and low accuracy when measuring the moisture content of peanuts; The GMK-303U grain moisture detector manufactured by G-won company of Korea can measure the moisture content of peanut kernel in the range of 4.8%-14.7%,with an accuracy of ± 0.5%.The PM8188 type of capacitive moisture detector in Japan can be used to detect the peanut kernel samples with moisture content of 4%-15% (Basu et al., 2018;Taki et al., 2018).The instrument is easy to operate, but the measurement results error of some samples are as high as 2%-3% compared with the standard drying method.The GAC2500 high-precision grain moisture analyzer manufactured by DICKEY-john Corporation adopts three parallel plate structures and can measure approximately 500g of peanut kernels at a time with an accuracy of 0.1%.However, the volume of instrument is large and is not convenient to carry (Antohe et al., 2016).
The detection of peanut kernel moisture content based on capacitance method is simple and sensitive, but the measurement results are affected by many factors.Therefore, the structure of coaxial cylinder capacitive sensor is optimized (Basak and Gajbhiye, 2018;Camacho and Martinez Morales, 2017).The effect of the grain size, moisture content, temperature and volume density on the output differential frequency of the capacitance sensor is studied by the design test system and the experiment.The theoretical basis is provided for the development and optimization of the capacitive moisture detector for peanut kernel (Ahamed et al., 2017;Zhang et al., 2018).

Detection device design
The structure of the detection device for peanut kernel moisture is shown in Fig. 1.The detection part of the device is mainly composed of capacitance sensor, temperature sensor, weight sensor and base.The plate material is aluminum alloy, the diameter of the structure is 5cm, the outer diameter is 8.4cm, and the cylinder height is 11cm.DS18B20 temperature sensor produced by Dallas Semiconductor Company is used to detect sample temperature (Huang et al., 2016;Li et al., 2015).The sensor has strong anti-interference ability, high detection accuracy, and the measuring temperature range is -55°C±-125°C, with an accuracy of ± 0.5 C; The weighing sensor is LCS-D1 parallel beam sensor, which is produced by Shanghai Tianhe Automation Instrument Co., Ltd., and the sensitivity of the sensor is (2 ± 0.1) mV/V.

Hardware circuit design
The block diagram of the hardware system is shown in Fig. 2. The system hardware mainly consists of the main control chip (STM32 microcontroller), the capacitance sensor, the weighing sensor, the temperature sensor, the A/D conversion module, the display module, the serial communication module and the power supply module.STM32 microcontroller is produced by STMicroelectronics.It has many external interfaces, and has many advantages such as low power consumption and excellent real-time performance.The load cell uses a strain gauge load cell with parallel beam; the temperature sensor is a DS18B20 digital temperature sensor; the hardware system display module is a true color liquid crystal display (TFT-LCD).The display module is a touch screen function, with high sensitivity and low power consumption; The serial communication module is the CH340G module.Through this module, the collected information is sent to the host computer for storage.The user can directly query the measurement data on the computer and perform processing analysis, which greatly improves the efficiency (W.B. 2017).

Capacitance detection circuit
The principle of the capacitance method for detecting the moisture content of peanuts is that the difference in the moisture content of the sample placed between the sensor plates leads to a change in the capacitance value, and the moisture content of the measured sample is indirectly obtained based on the change in the capacitance (Yu, 2017;Wu et al., 2016).Capacitance sensors can be classified according to different electrodes.The dielectric constant is different due to the different moisture content of grain, resulting in different capacitance changes.It is found that there is a linear variation between the capacitance change of the sensor and the dielectric constant of the material (Ye and She 2018).A cylindrical sensor is used in this paper.Because the capacitance value of capacitance sensor is very small, generally it is picofarad grade.When designing, the classical analog circuit is used to transform the change of micro capacitance into frequency change.The designed capacitance detection circuit is shown in Fig. 3.In the detection circuit designed by a cylindrical capacitive sensor that detects the moisture content of peanut kernel, the moisture content of the peanut kernel which is poured into the sensor is different and the frequency of the circuit output is different.The frequency of the sensor when the sample is not poured into the sample and the frequency when the sample is filled are measured respectively, and the moisture content of the sample is indirectly obtained according to the difference of the frequency (Gao, Y. et al., 2017).

Temperature detection circuit
The dielectric properties of peanut kernel are affected by the temperature change.According to a large number of related experiments, the relative dielectric constant value of peanut kernel becomes larger with the increase of temperature.DS18B20 temperature sensor is used to detect the temperature of peanut kernel.The structure of DS18B20 mainly includes memory and control logic, program setting register, temperature sensor, parasitic power, ROM and single bus interface, memory RAM, TH and TL, 8 bit CRC generator and so on.The advantage is that the sensor is a product using 1-Wire bus technology.The 1-Wire bus technology can reduce the use of I/O port, the structure is simple, the temperature value can be converted to digital in a very short time, that is, the speed is faster, and it has the power down protection function.The designed temperature detection circuit is shown in Fig. 4 (Mahmud, N . et al., 2018)

Weight detection circuit
In order to study the influence of volume density on the detection results, the weighing sensor is used to measure the quality of peanut kernels.The weighing sensor is a weight value obtained by measuring the electrical signal converted from the weight.The weighing sensor used in this design is a double-hole strain type structure with parallel beam.Its advantages are high measurement accuracy, large measurement range, good frequency response characteristics, and easy implementation of miniaturization, integration, and species diversity.Because the output signal of the weighing sensor is very small, the HX711 module is used for analog to digital conversion and amplification (Antohe et al., 2016;Sunet 2017).The design of the weight detection circuit is shown in Fig. 5.
The 24 bit A/D converter chip HX711 is a chip specially designed for the high precision weight sensor.As an A/D conversion chip, HX711 has the external circuit that the other type of chip must have.It has the advantages of short response time and good resistance to disturbance.The chip also has two optional differential inputs, an automatic reset circuit for power on, and simple digital control and serial port communication.The HX711 clock oscillator does not need to provide redundant analog power supply and no other devices.The main advantage of the simplified circuit is that in programming, it can save part of the space of program memory, making the initialization process greatly simplified.Whether from the hardware or the software, it is a relatively good choice.

Serial communication circuit
Serial communication refers to a communication method for bit-by-bit transmission of data information between an external device and a computer through an address, a signal line, a control line, and so on.The CH340G module used is a USB to TTL chip, which features 500mA high sensitivity overcurrent protection and 3.3V regulator IC.The test data is transmitted to the host computer by wired transmission through the module.The detection data is transmitted to the host computer in a wired manner through the module.

Software design of moisture detector
The main function modules of the moisture detection system software are shown in Fig. 6.
The software part mainly completes the initialization of each module, the collection of temperature, weight and output frequency information, the calculation of moisture content, and the display and preservation of information.
In actual testing, the designed moisture content detector for peanut kernel is electrified, and a certain amount of peanut kernel samples are taken with a fixed container.The samples are dropped into the container in a free falling way at the top of the sensor port.After a few seconds of stability, the weight, frequency, temperature and moisture content on the TFT-LCD display are read.Clicking on the send icon on the screen, the data can be sent to the host computer, the parameters of the serial port assistant on the host computer are configured, and the sent data are saved in the format of a text file.The information display interface is shown in Fig. 7.

Materials
The test materials used in this paper are large shelled peanuts from Zaozhuang City, Shandong Province.Peanut samples are removed for impurity treatment, and the damaged and mouldy peanuts are removed.The intact and uniform peanuts are selected as the test samples.The final samples of peanut kernel is shown in Fig. 8.

Preparation of samples
According to the requirements of determining the moisture content of peanuts in the national standard method for grain and oil moisture determination (Zheng et al., 2016;Li et al., 2016), the moisture content of peanuts after peeling is measured by the drying method, and the initial moisture content of the peanuts is about 7.3%.In order to obtain peanut kernel samples with different moisture gradients, the shelled peanut samples after removing the impurities are divided into 10 sealed bags.By adding an unequal amount of water to each peanut sample, a sample with a higher moisture content is to obtain, and then add a small amount of water each time and shake the sample bag at intervals to fully absorb moisture; Peanut samples with lower moisture content are obtained by natural drying of samples (Li and Mao, 2016;Tu and Bian 2017).After peeling the peanut samples, the moisture content of 10 peanut kernels is measured by drying method.The moisture contents of peanut kernels are 4.3%, 5.6%, 7.3%, 9.5%, 11.1%, 12.6%, 13.6%, 14%, 16.6% and 19.1% respectively.

Determination of moisture content
The moisture content of peanut kernel is detected by standard drying method.The temperature of the drying box is set to 130°C.The quality of the sample is weighed with electronic balance before drying, and then the sample is dried in a dry box until the quality of the peanut kernel is constant and the quality of the sample after drying is recorded.According to the quality change before and after drying, the moisture content of each sample under wet base standard is calculated.

The method of measuring the size of grain
A certain amount of peanut kernel is taken with a fixed volume container.The peanut kernels are cut into the 1/2, 1/4, 1/8 and 1/16 size of the original grain by a knife.The other grain size peanut kernel samples are obtained to change the volume density of the peanut samples and measure the output frequency of the peanut kernel size respectively (Islam, R. et al., 2018.)Temperature measurement method The heat movement of water molecules inside grain is affected by temperature.The higher the temperature is, the more intense the positive and negative charge movement is, and the more obvious the polarization effect is when the applied electric field is affected.In general, the higher the detection temperature is, the larger the capacitance of capacitance sensor is, and the larger the output differential frequency, indicating that the moisture content is higher.
The temperature value of the peanut kernel is read by DS18B20 temperature sensor at 10°C, 20°C, 30°C and 40°C, respectively.When the temperature of the peanut kernel sample is basically the same as the temperature of the drying box, the output frequency of the capacitance sensor under different temperature conditions is measured.

Measurement method of volume density
At the time of testing, the sample is obtained by the fixed volume sampling method.At the fixed height above the capacitance sensor, the sample of peanut kernel fall into the test unit of the capacitance sensor, and the quality of the peanut kernel samples under the condition of free

RESULTS AND ANALYSIS
The relationship between the grain size and the difference frequency Fig. 9 is the effect of grain size of different peanut kernels on the difference frequency.According to the graph, the grain size of the same variety of peanut kernel is different, and the differential frequency values produced are different.Generally, the grain size of peanut increased and the differential frequency increased.Peanut kernels are heterogeneous materials, but peanut kernels are generally regarded as homogeneous dielectric materials to study their dielectric properties.When the size of the peanut kernel is different, the arrangement mode between the poles of the capacitance sensor is different, and the gap between the peanut kernel particles in different arrangement is different, so the effective dielectric properties of the measured moisture content are not the same (Nagarasu and Manimegalai 2017;Lewis et al., 2017).

The relationship among different moisture content, temperature and differential frequency
In the process of the test, the constant volume sampling method is adopted.The volume density of the same height is basically unchanged, and the output frequency of the sensor at different temperatures and moisture content is measured.Fig. 10 shows the influence of different moisture content on the output differential frequency of capacitance sensor at different temperatures.It can be seen from the graph that the differential frequency value increases with the increase of temperature and moisture content, and the change is more significant at high temperature.Because of the increase of temperature, the movement of polar molecules inside peanut kernel is accelerated, and the relative dielectric constant increases (Atikuzzamman, M. et al., 2018.)The moisture content, differential frequency and temperature are represented as three dimensional Figures, as shown in Fig. 11.The X axis is moisture content, the Y axis is

The relationship between the volume density and the differential frequency
Grain bulk density, or bulk density, refers to the weight of an object in unit volume.It is a comprehensive index that reflects the quality, shape and size of grain.In general, if the grain is full and small, or the grain is low in moisture, the bulk density will be large, vice versa.Fig. 12 shows the effect of different volume density on the differential frequency at room temperature of 20°C.From the figure, we can see that the differential frequency value of peanut kernel samples increases with the increase of bulk density, and the differential frequency trend is more obvious in the high moisture content stage.This is due to artificial changes in the quality of the peanut kernels in the sensor.The quality of the peanuts has increased, and the peanuts have become more dielectric, which is reflected in the change in the differential frequency.Afterwards, experimental studies on the relationship between bulk density and differential frequency are conducted under other temperature conditions, and it is also found that this rule is met.
In the test, if the fixed volume sampling method is used in the test, the peanut kernel samples are dropped from the fixed height to the internal test unit of the cylindrical capacitance sensor, and repeated measurements are carried out.The result shows that the volume density remained the same.Based on this situation, in the moisture detection system standard, for the same peanut variety, the impact of bulk density is ignored, and other influencing factors are selected for analysis.

The establishment and test of modeling
Multiple regression analysis can be roughly regarded as two categories according to the number of variables and the number of independent variables.One is one to many, that is, there are many independent variables, and the number of variables is unique; the other case applies when the number of dependent variables and independent variables is not unique.In the moisture measurement model, the target is often restricted by many factors, and the quantity rule is nonlinear.Polynomial regression is a kind of curve regression model, which can be converted into a basic regression issue.In polynomial regression, if the order greater than three, it may cause the generation of the singular solution.So the general application of quadratic regression is solved.
The moisture content detection model of peanut seeds is obtained through fusion of experimental data.Each sensor has a cross sensitivity, that is, the sensor will not determine the value of the output because of a particular parameter, and the value will change accordingly when other parameters are changed.If this happens, the accuracy of detection will be affected.For the same peanut kernel, the size of the peanut kernel is kept constant, and the volume sampling method is adopted in the experiment.The same group of peanut kernels are tested and averaged for several times to reduce the effect of bulk density on moisture content measurement.Therefore, the mathematical modeling for three parameters of the difference frequency, temperature and moisture content is finally carried out.In this experiment, the output differential frequency, temperature and moisture content of peanut kernel are analyzed.The best model of peanut kernel moisture content detection is obtained, which improved the detection precision of peanut kernel moisture content.
The experimental data are introduced into the Matlab command window, and the regress analysis command of Matlab is used to analyze the experimental data by multiple regression analysis.Finally, the parameters of the regression coefficient, residual standard difference and residual value are obtained.Then, the binomial regression analysis is carried out with the rstool function.The mathematical models of the moisture content detection of  (Hawkins et al., 2016;Dai et al., 2017).Using MATLAB software for multiple regression analysis, four mathematical model fitting parameters are obtained.The obtained parameters are shown in Table 1.By comparing the parameters in the table, it is found that the complete quadratic model is the best-fitting equation, and the residual map corresponding to the complete quadratic model is shown in Fig. 13.
Complete quadratic model: Where, M is the calibrated value of moisture content of peanut kernel, % F is the output differential frequency, kHz T is the sample temperature, °C In the range of moisture content 4.3%-19.1%,the peanut kernel samples with different moisture contents are randomly prepared.The moisture content is detected in the range of 10°C -40°C, and the value of moisture content is calculated with the mathematical model obtained.Fig. 14 is a comparison of the moisture content value calculated using the complete quadratic model with the actual moisture content value measured by the baking method.The correlation coefficient between the two is 0.9959 and the root mean square error is 0.2695, which shows that the complete quadratic model can better predict the moisture content of peanut kernel.

Prediction of moisture content of peanut kernel
The mathematical equation of the moisture content of the peanut kernels is written into a single-chip microcomputer, and the moisture content of the measured sample is calculated based on the measured temperature and frequency values.The peanut kernel samples with different moisture content are randomly prepared, and the moisture content of peanut kernel is tested with the designed moisture detector at different temperatures.Table 2 compares the moisture value predicted by the  designed moisture detector and the actual moisture value obtained by the drying method.It is known from the data in the table that the measurement error of the moisture detector is within 0.5% and can meet the need of detection.

CONCLUSION
A capacitive moisture meter for detecting the moisture Zhao, et al. content of peanut kernel is designed, and the structure of the coaxial cylindrical capacitance sensor is optimized.The reliability of the detection system is high, and the accuracy and repetition rate of the detection results of the peanut kernel's moisture content of are further improved.
Using the differential frequency detection method, the effect of the size of peanut, moisture content, temperature and volume density on the output frequency of capacitance sensor is studied.The mathematical model is established by multiple regression analysis method, the different mathematical models are fitted and analyzed with the assistance of MATLAB software.A complete quadratic model is used to establish a predictive model for the moisture content of peanut kernels, which provided a reference for the experimental study on the influencing factors of capacitance measurement accuracy.
The predicted moisture content of the peanut kernel moisture detector is compared with the actual moisture content obtained by the drying method, and the results show that the absolute value of the measurement error is less than 0.5% in the range of moisture content 4.3%-19.1% of peanut kernel and the temperature of 10°C-40°C, which meets the requirement of detection.

Fig 2 .
Fig 2. Diagram of system hardware

Fig 3 .
Fig 3. Capacitance detection and conversion circuit

Fig 9 .
Fig 9. Influence of kernel size on differential frequency

Fig 13 .
Fig 13.Residual plot of complete quadratic model