A Miniaturized and Low-Cost Near-Infrared Spectroscopy Measurement System for Alfalfa Quality Control

: Food safety and quality are the ﬁrst steps in the food chain. This work reports a miniaturized, low-cost, and easy-to-use near-infrared spectroscopy (NIRS) measurement system for alfalfa quality control. This is a signiﬁcant challenge for dairy farm technicians and producers who need rapid and reliable knowledge of the forage quality on their farms. In most cases, the instrumentation suitable for these speciﬁcations is expensive and difﬁcult to operate. The core of the proposed NIR spectroscopy measurement system is Texas Instruments’ NIRscan Nano evaluation module (EVM) spectrometer. This module has a large sensing area and high resolution, suitable for forage samples. To evaluate the feasibility of the prototype for analyzing agrifood samples, different ways of presenting the sample, intact or ground, were tested. The ﬁnal objective of the research is not just to check the efﬁciency of the proposed system. It is also to determine the characteristics of the measurement system, and how to improve them for alfalfa quality control.


Introduction
The nutritional value of animal feed is essential to quality, safe feed consumption, and animal welfare [1]. In addition to this fact, and due to the great variability in the raw materials used to feed animals, it is necessary to develop strategies focused on the tight control of animal feed products. These should be combined with the research and development of new, simple, economical, and robust methods for monitoring quality and safety parameters [2,3].
Forage is one of the main feed products in animal husbandry and must, therefore, be subject to safety and quality controls. Among the most important quality parameters for forages, the following three can be highlighted [4]. The fiber content is mainly provided by the fodder cell wall, and represents its carbohydrate. The mineral content (ash) gives information about possible contamination with soil, and supplies micronutrients to the diet. It also provides information on the quality of the forage. The third parameter is the protein content, which is extremely important in animal production.
Several important books on NIR spectroscopy are currently available, but some of them are not up to date. Ozaki et al. [5] report a new state-of-the-art textbook on NIR spectroscopy, covering its principles, spectral analysis and data treatments, instrumentation, and applications. In [6], Huck et al. review the fundamental principles of NIRS, and its applicability, regulatory issues, advantages, and limitations in natural product research.
Near-infrared spectroscopy (NIRS) techniques have always been valued and used in food analysis and quality control, due to the speed of analysis, the simplicity of sampling, the non-invasive nature of the techniques, and the possibility of their being implemented in the production line. In this review [7], Shenk et al. introduce scientific and technical reports using the NIRS to evaluate food, agriculture, and forest products.
In addition to the advantages of this technique, the possibility of developing miniaturized NIR systems that are easy to use, and specialized in the quality control of the raw forages used in animal feed, makes it possible to increase quality control (sampling). Thus, Cherney et al. [8] evaluate several hand-held NIR instruments for the precision and accuracy of the currently available calibrations for dry-matter and forage nutritive value. Crocombe [9] outlines the technologies used in portable spectroscopy, and Beć et al. [10] discuss the characteristics of miniaturized NIR sensors in comparison to benchtop laboratory spectrometers.
The use of easy-to-use portable NIRS instrumentation minimizes time losses, because nonspecialized personnel are able to analyse samples on site, and a real-time response is achieved as soon as the analysis is carried out. These characteristics are some of those required in precision farming [11].
Several miniaturized spectrophotometers have been developed and marketed in the last decade, some of which are extremely small, light, and inexpensive. By allowing measurements in the field, at the point of delivery, production, sale, purchase, and use, these spectrometers transform NIRS technology. Table 1 lists some commercial miniaturized NIRSs, and presents their main characteristics, as found on the websites of the manufacturers. This work evaluates the feasibility of a miniaturized and low-cost NIRS measurement system for alfalfa quality control. The core of this system is a NIR spectrometer based on the Texas Instruments NIRscan Nano evaluation module (EVM). It has a large sensing area and high resolution to analyse forage samples. To evaluate the feasibility of the prototype, different ways of presenting the sample, intact (raw) or ground, were studied. This equipment has already been tested for use in liquid sample analysis [12], with a specific cuvette for liquid samples. For this purpose, alfalfa samples were analysed. Alfalfa is one of the main forages used to feed animals, due to its high biomass production, and protein and fiber contents. In summary, this work contributes: • to confirming the efficiency of the proposed NIRS measurement system; • to identifying the qualities of the equipment; and • to looking for aspects to improve and implement in the future. The remainder of this paper is organized as follows: Section 2 reports the materials and methods for analysing dairy farm forage quality. Section 3 presents the results and discussion. Finally, Section 4 contains conclusions and future works.

NIRscan Nano Evaluation Module
The core of the optoelectronic measurement system is the Texas Instruments NIRscan Nano evaluation module (USD 1000 approx.) [13]. The block diagram of this module for reflectance measurements is shown in Figure 1. A slit collects and concentrate diffuse reflections, by illuminating samples at an angle that eliminates specular reflections. Light passing through the slit is collimated, low-pass filtered, and then dispersed into its constituent wavelengths via a reflective grating. Each wavelength is represented by a separate image created by the focusing lens of the digital micromirror device (DMD).
The remainder of this paper is organized as follows: Section 2 reports the materials and methods for analysing dairy farm forage quality. Section 3 presents the results and discussion. Finally, Section 4 contains conclusions and future works.

NIRscan Nano Evaluation Module
The core of the optoelectronic measurement system is the Texas Instruments NIRscan Nano evaluation module (USD 1000 approx.) [13]. The block diagram of this module for reflectance measurements is shown in Figure 1. A slit collects and concentrate diffuse reflections, by illuminating samples at an angle that eliminates specular reflections. Light passing through the slit is collimated, low-pass filtered, and then dispersed into its constituent wavelengths via a reflective grating. Each wavelength is represented by a separate image created by the focusing lens of the digital micromirror device (DMD).
The DMD is controlled by the embedded processor, which turns only certain mirrors on and off at certain times. The width of the DMD columns selected as "on" determines the amount of light directed to the photodetector, as well as the resolution of the system. The DMD columns selected as "off" divert unselected wavelengths away from the photodetector's optical path, to prevent interference. By doing this, they can achieve high signalto-noise ratios (SNRs). An array detector cannot take advantage of the adaptive scanning techniques that can be performed using this type of architecture. Light energy is collected and concentrated, by the collection lenses, onto the single-point InGaAs photodetector. Analog-to-digital converters (ADCs) convert photodetector signals to digital values through transimpedance amplifiers. The DMD is controlled by the embedded processor, which turns only certain mirrors on and off at certain times. The width of the DMD columns selected as "on" determines the amount of light directed to the photodetector, as well as the resolution of the system. The DMD columns selected as "off" divert unselected wavelengths away from the photodetector's optical path, to prevent interference. By doing this, they can achieve high signal-to-noise ratios (SNRs). An array detector cannot take advantage of the adaptive scanning techniques that can be performed using this type of architecture. Light energy is collected and concentrated, by the collection lenses, onto the single-point InGaAs photodetector. Analog-to-digital converters (ADCs) convert photodetector signals to digital values through transimpedance amplifiers.

NIRS Measurement System
The instrument sample window can be enlarged if raw samples with larger particle sizes are analyzed. This will produce reproducible spectra. However, this is difficult when measuring samples directly, as the sample window is very small (10 mm × 10 mm). This problem was overcome through the attaching of a semicircular sample holder to the spectrometer, as shown in Figure 2a. To ensure homogenous results for the sample, 10 measurements are made at each holder position. Figure 2b shows the microcontroller (LOLIN ESP32, USD 10) attached to the spectrometer module that drives the servomotor Appl. Sci. 2023, 13, 9290 4 of 14 (MG90S, USD 10). This servo motor is powered by a 3.7 V to DC-DC converter (Pololu U1V0F5, USD 6). When the start button is pressed, the servo motor rotates, and stops at different positions to take measurements. After rotating 180 • , the holder returns to its original position, and waits for another measurement to take place. When the load button is pressed, the servo motor rotates 90 • to an intermediate position, making loading and unloading easier. When neither of these two actions occur, the microcontroller enters sleep mode.

NIRS Measurement System
The instrument sample window can be enlarged if raw samples with larger particle sizes are analyzed. This will produce reproducible spectra. However, this is difficult when measuring samples directly, as the sample window is very small (10 mm × 10 mm). This problem was overcome through the attaching of a semicircular sample holder to the spectrometer, as shown in Figure 2a. To ensure homogenous results for the sample, 10 measurements are made at each holder position. Figure 2b shows the microcontroller (LOLIN ESP32, USD 10) attached to the spectrometer module that drives the servomotor (MG90S, USD 10). This servo motor is powered by a 3.7 V to DC-DC converter (Pololu U1V0F5, USD 6). When the start button is pressed, the servo motor rotates, and stops at different positions to take measurements. After rotating 180°, the holder returns to its original position, and waits for another measurement to take place. When the load button is pressed, the servo motor rotates 90° to an intermediate position, making loading and unloading easier. When neither of these two actions occur, the microcontroller enters sleep mode.

Forage Samples
For this study, a calibration set of 57 samples of hay or dehydrated alfalfa collected in the north of Spain was utilized. Approximately 200 g of alfalfa was collected during the sampling procedure, to be analyzed using miniaturized NIRs. Prior to the collection of the raw scans, the samples were homogenized. After that, the alfalfa samples were milled, using a domestic spice mill (which is cheap and easy to use), and re-scanned in their ground form. This type of mill does not allow mesh-size setting. The variability associated with this factor appears in the collected spectra, and also in the chemometric results. This is because all spectroscopic information is considered when developing calibrations.
When the NIR analysis was complete, the quality of the alfalfa samples, based on animal feed requirements, was characterized, according to their nutritive value parameters. This was done using laboratory reference procedures. A Van Soest analysis [14] was performed on the neutral detergent fiber (NDF), a gravimetric analysis was conducted on the mineral content (MC), and a Kjeldahl analysis was conducted on the crude protein (CP). The CP and NDF are two of the critical parameters for classifying alfalfa quality. CP values of 19% or higher, combined with crude fiber values of 29% or lower, are related to good forage quality [15]. Table 2 summarizes the statistics for the nutritive parameters of all the samples involved in this study. It includes the range and variability in each

Forage Samples
For this study, a calibration set of 57 samples of hay or dehydrated alfalfa collected in the north of Spain was utilized. Approximately 200 g of alfalfa was collected during the sampling procedure, to be analyzed using miniaturized NIRs. Prior to the collection of the raw scans, the samples were homogenized. After that, the alfalfa samples were milled, using a domestic spice mill (which is cheap and easy to use), and re-scanned in their ground form. This type of mill does not allow mesh-size setting. The variability associated with this factor appears in the collected spectra, and also in the chemometric results. This is because all spectroscopic information is considered when developing calibrations.
When the NIR analysis was complete, the quality of the alfalfa samples, based on animal feed requirements, was characterized, according to their nutritive value parameters. This was done using laboratory reference procedures. A Van Soest analysis [14] was performed on the neutral detergent fiber (NDF), a gravimetric analysis was conducted on the mineral content (MC), and a Kjeldahl analysis was conducted on the crude protein (CP). The CP and NDF are two of the critical parameters for classifying alfalfa quality. CP values of 19% or higher, combined with crude fiber values of 29% or lower, are related to good forage quality [15]. Table 2 summarizes the statistics for the nutritive parameters of all the samples involved in this study. It includes the range and variability in each analyzed parameter. Appendix A includes the individual values for each alfalfa sample included in this study.

Spectral Acquisition
Spectral acquisition requires a scan configuration (see Figure 3). Texas Instruments provides two different scan configurations within the "Factory" settings: "Column" and "Hadamard". analyzed parameter. Appendix A includes the individual values for each alfalfa sample included in this study.

Spectral Acquisition
Spectral acquisition requires a scan configuration (see Figure 3). Texas Instruments provides two different scan configurations within the "Factory" settings: "Column" and "Hadamard". Column scanning selects wavelengths one at a time. With Hadamard scanning, several wavelengths are multiplexed at a time, and the wavelengths are decoded individually. Hadamard scanning collects light, and offers a higher SNR, but it depends heavily on the spectrum being measured, and on the measurement system [15]. Column analysis is more effective for analyzing forage samples, because it is more accurate than other methods of analysis, due to its reproducibility.
Before scan collection, one to five sections can be configured, depending on the type of method (Column or Hadamard), the spectral range (the starting wavelength and ending wavelength), the digital resolution (the wavelength points captured within the defined spectral range) the exposure time (between 0.635 and 60.960 milliseconds), and the number of scans per sample (in this work, 10 scans at 10 different points in the sample).
In this work, all samples were scanned in reflectance mode, using the measurement system shown in Figure 2. Column scanning selects wavelengths one at a time. With Hadamard scanning, several wavelengths are multiplexed at a time, and the wavelengths are decoded individually. Hadamard scanning collects light, and offers a higher SNR, but it depends heavily on the spectrum being measured, and on the measurement system [15]. Column analysis is more effective for analyzing forage samples, because it is more accurate than other methods of analysis, due to its reproducibility.
Before scan collection, one to five sections can be configured, depending on the type of method (Column or Hadamard), the spectral range (the starting wavelength and ending wavelength), the digital resolution (the wavelength points captured within the defined spectral range) the exposure time (between 0.635 and 60.960 milliseconds), and the number of scans per sample (in this work, 10 scans at 10 different points in the sample).
In this work, all samples were scanned in reflectance mode, using the measurement system shown in Figure 2.
In order to increase the sampling window and improve the spectroscopic information for each sample, after the alfalfa samples were homogenized, each was divided into three subsamples and scanned using miniaturized NIRs. Each spectrum is an average of 10 spectra, in a wavelength range between 901 and 1700 nm, with a non-linear path wavelength between 2.9 and 3.9 nm. A total of 30 scans were collected for each alfalfa sample. The precision of the collected spectrum for each sample or signal reproducibility was evaluated for the raw and ground alfalfa. The statistic of the root-mean-square error (RMSE) was calculated. Using (1), it is possible to calculate the RMSE for each sample spectrum. Lower RMSE values are related to reproducible and repeatable spectra.
y a = log 1/R to λ for the average spectrum resulting from averaging a number of scans, and R is reflectance. y b = log 1/R to λ for the average spectrum resulting from averaging b number of scans n = number of spectral data

Spectral Data Processing
To establish a calibration model to quantify alfalfa nutritive parameters, different chemometric strategies were assayed. The software Unscrambler X version 10.4 was employed to find the linear correlation between the NIRS spectra and nutritive parameters. This software takes NIRS spectra and transforms them into a matrix with X and Y variables, defined as the wavelength and reflectance. To detect potential spectral outliers, a principal component analysis was performed on the calibration set, before regression models were constructed using partial least squares (PLSs) [16]. An optimal number of PLSs factors is determined using the Unscrambler X software version 10.4 package, based on the minimum residual variance and 20 segments. Different spectrum mathematical pretreatment strategies were used for NDF, MC, and CP quantification. These approaches were performed using both the full range of equipment, from 900 to 1700 nm, and a reduced one, from 900 to 1600 nm.
To establish a successful model, a combination of pretreatments, including scatter correction with the standard normal variate (SNV), and the first and second Savitzky-Golay derivatives (SG), were tested in this study. Six possible pre-treatments were developed for three parameters, studying two possible wavelength ranges, for both the raw and milled samples. A total of 72 mathematical pre-treatments were assayed.
For the evaluation and selection of the most suitable chemometric model, the following statistics were calculated: the coefficient of determination for calibration (R 2 , see Equation (2)), and the standard error of calibration (SEC, see Equation (3)) [17].
y i are the reference values obtained in the laboratory,ŷ i is the prediction of the model, and n represents the number of samples used in the calibration set.
This parameter provides an average of the typical uncertainty for future sample prediction based on theŷ i and y i values for the ith sample.
For each parameter evaluated, the best mathematical pre-treatment was selected for the raw and ground NIR sample analysis. The criteria for the selection of these pre-treatments were based on the lowest values of calibration standard error (SEC), as well as the values closest to one for the calibration determination coefficients (R 2 ) [18].

Results and Discussion
For an NIRS procedure to be understood, the characterization of the spectrum data obtained using the NIRscan Nano prototype is essential. Figure 4 shows the raw and SNV spectra, plus the first Savitzky-Golay derivative spectra of the alfalfa samples included in the calibration procedure. Within the NIR wavelength range of the NIRscan Nano prototype, we can identify some characteristic bands of forages. According to the bibliography, those bands observed at 1166 nm are related to the protein content of the samples [18], and those observed at around 1350 nm are related to the fiber content [19]. Additionally, around 1400 nm, there is a band that can give information about the moisture content because, at that wavelength, OH bond overtone vibrations are observed [20]. In Figures 4 and 5, a rectangle highlights all the cited wavelengths, and the referenced respective parameters. Hence, in Figure 4b, which is an extended area between 1650 and 1700 nm of Figure 4a, we can observe that some of the collected spectra show a noisy signal at the end of the collected spectra. This noisy wavelength range, as shown in Figure 4c, can be minimized after scattering correction (SNV) is applied, along with other mathematical pretreatments, such as derivatives.  As observed in Figures 4 and 5, between 1600 and 1700 nm, the absorbance increases, and the SNR is lower than in other ranges. It is because the intensity measured at the detector is proportional to the number of DMD mirrors positioned to reflect the incident illumination towards it. As the number of pixels changes, the measured intensity is affected as well, resulting in an increase in noise levels.
Once the spectra were evaluated, the precision of the subsampling procedure for each scanned sample (raw and ground alfalfa) was evaluated [21]. Five samples were randomly selected from the 57 analysed. The RMS value was calculated for both the intact and ground samples with the two ranges proposed (901-1700 nm and 901-1600 nm). The results are shown in Table 3.    After characterizing the spectral signal, the next step was to develop a calibration model. To attempt calibration, it is necessary to build a data matrix including nutritive values (the NDF, MC, and CP) and spectral data. After that, prior to the carrying out of calibrations, as mentioned in the Material and Methods section, different mathematical pre-treatments were applied, for the three parameters, to the raw and ground samples, both for the full range and the reduced range. Partial-least-square regression is used to establish the correlation between the spectra and the assayed parameters. To evaluate the effect of the sample pretreatment on the spectral data set, ground samples were scanned using NIRscan Nano. Figure 5a shows the spectral data set. As can be seen, no differences in the representative bands are observed. Moreover, the extended wavelength range (1650-1700 nm; see Figures 4b and 5b) shows that, after milling the sample pre-treatment, the noisy wavelength range disappears. These data confirm that the spectral quality depends on the sampling procedure (raw or ground). This distorted wavelength range is due to the huge and non-homogeneous particle size of the alfalfa samples. It is worthy of mention that, after the application of mathematical pretreatments, no differences were observed in the collected spectra of the alfalfa samples (see Figures 4c and 5c).
As observed in Figures 4 and 5, between 1600 and 1700 nm, the absorbance increases, and the SNR is lower than in other ranges. It is because the intensity measured at the detector is proportional to the number of DMD mirrors positioned to reflect the incident illumination towards it. As the number of pixels changes, the measured intensity is affected as well, resulting in an increase in noise levels.
Once the spectra were evaluated, the precision of the subsampling procedure for each scanned sample (raw and ground alfalfa) was evaluated [21]. Five samples were randomly selected from the 57 analysed. The RMS value was calculated for both the intact and ground samples with the two ranges proposed (901-1700 nm and 901-1600 nm). The results are shown in Table 3. Once the values have been calculated, two clear trends can be observed. As expected, the RMS values obtained for the intact samples (raw) are higher than those obtained for the ground ones. These results could be due to heterogeneity in the raw alfalfa. The difference was also significant when the entire spectrum was compared or the last 100 nm was suppressed. Table 3 shows that the RMS values ranging from 900 to 1600 nm are lower than the full range of values. These results highlight the influence of the sampling procedure on the spectrum data precision.
After characterizing the spectral signal, the next step was to develop a calibration model. To attempt calibration, it is necessary to build a data matrix including nutritive values (the NDF, MC, and CP) and spectral data. After that, prior to the carrying out of calibrations, as mentioned in the Material and Methods section, different mathematical pre-treatments were applied, for the three parameters, to the raw and ground samples, both for the full range and the reduced range. Partial-least-square regression is used to establish the correlation between the spectra and the assayed parameters. Table 4 summarizes the NIRS models' calibration statistics to quantify the NDF. As can be seen in Table 4, the R 2 values are higher and the SEC values are lower in the chemometric models developed with the reduced wavelength range than in those developed using the full one. In relation to the variability in the results depending on the mathematical pretreatment, it is important to note that the SNV plus the second Savitzky-Golay derivative reached the best calibration statistics for the raw and ground samples. Previous authors [22], after evaluating different commercial portable NIRS instruments to analyze ground forages, obtained R 2 values for the NDF ranging between 0.95 and 0.71, depending on the instrumentation employed. Regarding the SEC values, their results were between 2.85 and 1.21. It is not possible to obtain SEC values lower than 1, because the standard error of the laboratory (SEL) for this parameter is higher than 1.3 [22].   Table 5 summarizes the calibration statistics for the CP. Most mathematical treatments reach R 2 values lower than 0.5 for raw alfalfa samples. This could be related to the heterogeneity in alfalfa forage, with two clearly different parts, the leaf and the stem. The leaf is the part of the plant that contains a protein fraction. However, it is important to remark that using the reduced range, the spectrum mathematical pretreatment of the SNV for the scatter correction, and the second Savitzky-Golay derivative (the same mathematical pretreatment as for the NDF), R 2 values of 0.885, with an SEC of 0.377, were achieved. A typical SEL for reference CP analysis is around 0.210 [22]. Based on the ground samples, and a reduced range (901-1600 nm), the developed models showed statistics around 0.7 or higher, with SEC values between 0.530 and 0.986. Considering these results, it is worth mentioning that, even though the homogeneity in the ground samples gives better calibration statistics, NIRscan Nano reached acceptable values when scanning the raw samples.
Feeding animals with minerals is a common practice; however, if there is an abnormal mineral content, there is a high probability of contamination with soil, which is not desirable in animal feeding systems. To quantify the MC in alfalfa forages, 24 different calibration models have been developed, assaying different mathematical pretreatments of spectrum data. The statistics of the proposed PLS models are shown in Table 6. As stated before, the reduced range gave better calibration statistics than the full one. Comparing mathematical pretreatments, the scatter correction applied after the derivatization procedure increased the R 2 values, and reduced the SEC. The highest R 2 and the lowest SEC values were 0.861/0.219 and 0.867/0.318 for the raw and ground samples, respectively. These NDF, CP, and MC calibration model statistics, obtained using the NIRS measurement system, are like those acquired via commercial portable instruments, using a wavelength range like that evaluated in this work [22,23]. The SEC values are in accordance with laboratory results, and the effect of the sampling procedure has been studied comparatively in this work. As a summary of the obtained results, Table 7 selects the best models obtained for each sampling procedure (raw or ground alfalfa) and parameter. As can be seen, the second derivative is the best of the assayed pretreatments that provide satisfactory results for the nutritive value quantification.

Conclusions and Future Work
In this work, heterogeneous forage (alfalfa) has been selected as a model to evaluate the precision of instrumental measures (spectra collected), and the effects of sampling presentation (raw or ground), on calibration statistics. The results have revealed that homogeneous forage samples (those milled) allow us to achieve better calibration models than those scanned in their raw form (heterogeneous). However, for all sampling procedures, it has been possible to obtain satisfactory calibration to quantify the nutritive parameters.
Through the proposed instrumentation, users can evaluate the forage quality, increase sampling without incurring costs, and obtain results in real time. This is done by avoiding delays related to carrying samples from the farm to the laboratory. Furthermore, this instrument does not require specialized training.
In the future, with the use of internet of things (IoT) tools, data can be sent to the cloud for processing. In this way, they would be accessible from any device. Thanks to storage and processing in the cloud, data are accessible from any site with internet access. This allows the use of data in decisions.