Abstract:Portable near-infrared (NIR) spectroscopic detectors for soil total nitrogen content offer the advantages of rapid analysis, non-destructive measurement, and high efficiency. However, most existing instruments adopt filterbased designs with a limited number of spectral channels, which can lead to the loss of critical information and prevent the implementation of deep learning-based prediction models. With the commercialization of miniature spectrometers, a high-precision soil total nitrogen content detector was developed based on continuous spectral data. The detector primarily consisted of an NIR-R210 miniature spectrometer, a Raspberry Pi, a touchscreen display, and a portable power supply. The spectrometer was used to acquire soil spectral reflectance data, which were processed by a deep learning model embedded in the Raspberry Pi to predict soil total nitrogen content. The prediction results were then displayed in real time on the touchscreen. A total of 600 soil samples were collected from the Shangzhuang Experimental Station of China Agricultural University. The predictive performances of three models (partial least squares regression (PLSR), gated recurrent unit (GRU), and Transformer) were compared. Among them, the Transformer model based on full-spectrum data achieved the best performance, with a coefficient of determination (R2) of 0.89, a root mean square error (RMSE) of 0.19g/kg, and a residual predictive deviation (RPD) of 2.96. Further real-time in-situ field tests showed that the Transformer model maintained high accuracy under field conditions, with an R2 of up to 0.83. This portable device provided an effective solution for real-time soil nutrient detection and precision management in smart agriculture.