Design of a Grain-Storage Intelligent Ventilation System Based on STM32
Abstract
To address grain bulk heating and mold growth during storage, this paper presents the design and implementation of an intelligent ventilation control system for grain storage based on STM32. With the STM32F103C8T6 as the control core, a stainless-steel DS18B20 probe buried inside the grain bulk measures grain temperature while a DHT11 measures ambient temperature and humidity. A gear-based target-temperature-differential strategy (quantized proportional control) drives a DC fan through a TB6612FNG driver; when ambient humidity reaches 75%RH or above, ventilation is automatically interlocked to prevent moist air from entering the grain bulk and causing moisture absorption and condensation. The system supports two sensing modes (storage / ambient) and two running modes (auto / manual); eight scheduled ventilation plans and runtime records are persisted in an external W25Q64 Flash. The ESP12F exposes an HTTP API and performs NTP time synchronization, and a companion React Native mobile app connects to the device directly over the LAN, covering control, scheduling, data curves, and settings. Tests show that gear transitions respond accurately, hysteresis is effective, and the humidity interlock is reliable, meeting the requirements of grain-storage ventilation.
Keywords: STM32; grain-storage ventilation; temperature-differential gearing; DS18B20; humidity interlock; React Native
1 Introduction
During storage, the grain bulk continuously accumulates heat from respiration and microbial activity. Because a grain bulk has large heat capacity and poor thermal conductivity, its internal temperature can run 5–10℃ above ambient air temperature — measuring room temperature alone reveals nothing about the actual grain condition, and failure to ventilate in time can quickly lead to mold. Mechanical ventilation is the industry-standard countermeasure, yet conventional ventilation equipment still relies largely on manual experience for start/stop decisions, without comprehensive judgment across "grain temperature — air temperature — humidity."
The design idea of this work is to take the internal grain-bulk temperature as the primary decision variable and replace continuous regulation with gear-based target-differential control: the larger the differential, the higher the fan gear, and the fan stops once temperature falls near the target — a natural match for the large-heat-capacity, slow-response nature of a grain bulk. A humidity interlock rule is also introduced, upgrading the system from a mere "temperature-controlled fan" to a multi-parameter decision system. In addition, the ESP12F and a mobile app provide remote monitoring and control over the LAN, with no dependency on any cloud platform, so the system remains usable even without internet access.
2 Overall System Design
2.1 Functional Requirements
The system is required to provide:
- Dual-sensor temperature measurement: in storage mode the decision variable is the DS18B20 reading (probe buried in the grain bulk); in ambient mode it is the DHT11 room temperature; both modes can be switched at any time;
- Gear-based ventilation: ΔT = measured temperature − target temperature (target adjustable 15–40℃ via keys), driving 0/25%/50%/100% PWM gears per the mapping table, with down-gear thresholds shifted 2℃ lower to form hysteresis;
- Humidity interlock: in storage mode, ambient humidity ≥ 75%RH forces shutdown with a prompt, preventing moist air from being drawn into the grain bulk;
- Scheduled ventilation: eight plans persisted in flash; at the scheduled time the fan runs at gear 3 for N minutes regardless of the differential and humidity interlock;
- Local interaction and remote control: Chinese OLED menu with four keys locally; the mobile app controls the device remotely and views runtime data curves through the ESP12F HTTP API.
2.2 System Architecture
The system consists of six parts: the STM32F103C8T6 controller; sensing (DS18B20, DHT11, DS3231 RTC); actuation (TB6612FNG + DC fan); storage (W25Q64); display and interaction (OLED + 4 keys); and communication (ESP12F). The main loop runs one "sample — decide — actuate" cycle every 2 seconds. The ESP12F runs independent firmware, serving an HTTP API to the app on one side and talking to the STM32 over a serial @-protocol on the other, with NTP time synchronization.
2.3 Component Selection
| Module | Model | Key Specifications | Role |
|---|---|---|---|
| Controller | STM32F103C8T6 | Cortex-M3 / 72 MHz / 64 KB Flash | Control core |
| Grain temp | DS18B20 | ±0.5℃, 1-wire, stainless probe | Buried in-bulk temperature |
| Ambient temp/RH | DHT11 | ±2℃, ±5%RH | Ambient-mode sensing + humidity interlock |
| Motor driver | TB6612FNG | Dual H-bridge, PWM | Fan drive |
| RTC | DS3231 | I2C, temperature-compensated | Timekeeping for schedules |
| Storage | W25Q64 | 8 MB SPI Flash | Font, plans, and runtime records |
| Communication | ESP-12F | 802.11 b/g/n | HTTP API + NTP |
| Display | OLED 0.96" | 128×64, SSD1306 | Chinese status display |
3 Hardware Design
3.1 Pin Assignment
| Function | Pin | Configuration |
|---|---|---|
| Fan PWM | PA0 | TIM2_CH1, 1 kHz |
| Motor direction AIN1/AIN2 | PA1 / PA2 | Push-pull output |
| DS18B20 | PB1 | 1-wire, 4.7 kΩ pull-up |
| DHT11 | PA8 | Open-drain + pull-up |
| W25Q64 | PA4–PA7 | SPI1 |
| OLED | PB8 / PB9 | Software I2C |
| Keys ×4 | PA3, PB5–PB7 | EXTI, 300 ms debounce |
| Debug UART | PB10 / PB11 | USART3, IDLE+DMA |
| ESP12F | PA9 / PA10 | USART1, IDLE+DMA |
The circuits are integrated on a custom PCB (STM32 core-board socket + TB6612FNG + ESP-12F + OLED + 4 keys + AMS1117-3.3 regulator), as shown in Figure 3-1.

4 Software Design
4.1 Main Loop and Two-Stage Temperature Sampling
The main loop uses a 100 × 20 ms polling structure to complete one control cycle every 2 seconds, under the constraint that no blocking within a single cycle may exceed the ESP command timeout (400 ms). The worst offender is the DS18B20 12-bit conversion (up to 750 ms), so a non-blocking two-stage scheme is used: each cycle reads the result of the conversion initiated in the previous cycle and immediately starts the next conversion, blocking for only about 2 ms per cycle. This preserves the sampling cadence without causing the app's polling to hit "STM32 not responding."
4.2 Gear-Based Target-Differential Control
The decision variable is ΔT = measured temperature − target temperature. Up-gearing responds immediately (no hysteresis); down-gear thresholds are shifted 2℃ lower (GEAR_HYST) to prevent rapid gear flapping near a threshold:
/* Up-gear by the up thresholds; down-gear by thresholds shifted 2°C */
static uint8_t Gear_Calc(float dt)
{
uint8_t up = 0;
if (dt >= GEAR_UP_3) up = 3;
else if (dt >= GEAR_UP_2) up = 2;
else if (dt >= GEAR_UP_1) up = 1;
if (up > g_fan_gear) return up; /* need to gear up: act now */
uint8_t down = 0; /* gear down: with hysteresis */
if (dt >= GEAR_UP_3 - GEAR_HYST) down = 3;
else if (dt >= GEAR_UP_2 - GEAR_HYST) down = 2;
else if (dt >= GEAR_UP_1 - GEAR_HYST) down = 1;
return down;
}
| ΔT | Gear | PWM |
|---|---|---|
| ΔT < 2℃ | 0 | 0 |
| 2 ≤ ΔT < 5℃ | 1 | 25% |
| 5 ≤ ΔT < 8℃ | 2 | 50% |
| ΔT ≥ 8℃ | 3 | 100% |
Gear 1 at 25% already clears the motor's roughly 15% start-up dead zone (the driver additionally provides Motor_EnforceMinPwm() dead-zone compensation as a fallback), guaranteeing reliable start at the lowest gear.
4.3 Humidity Interlock and Scheduled Ventilation
In storage mode, when DHT11 humidity reaches 75%RH the system forces gear 0 and displays HUM LOCK; it releases automatically when humidity falls back — the DHT11's integer-only humidity naturally forms a hysteresis band. Scheduled ventilation plans are stored in the W25Q64 sector at 0x7E0000, each entry containing a weekday mask, trigger time, and duration in minutes; at the trigger time the fan runs at gear 3 for N minutes regardless of the differential and the humidity interlock, then the previous mode resumes. Timekeeping is provided locally by the DS3231, with the ESP12F periodically correcting drift via NTP.
4.4 Runtime Records
Every 10 minutes the system appends a 16-byte runtime record to the W25Q64 (starting at 0x7D0000), covering periodic samples, scheduled-ventilation start/stop, manual on/off, and gear changes, which the app fetches to draw curves and statistics.
5 Mobile App Design
The app is developed with React Native (Expo) and connects to the ESP12F directly over LAN HTTP with no cloud dependency, remaining usable in offline or demonstration environments. It has four pages:
- Control: a large card for the measured temperature, target ± adjustment, gear visualization, auto/manual switching, and manual on/off (Figure 5-1);
- Plans: adding, enabling, and deleting scheduled ventilation plans (Figure 5-2);
- Data: reads runtime records from device flash and plots grain/ambient temperature and humidity curves over 1 h / 6 h / 24 h windows with min–max and average statistics (Figure 5-3);
- Settings: device IP configuration and connection test, sensing-mode switching, device time and NTP status (Figure 5-4).




6 Testing and Analysis
| Test Item | Method | Result |
|---|---|---|
| Differential gearing | Heat until ΔT crosses 2/5/8℃ in turn | Gears step 1/2/3 correctly with PWM at 25%/50%/100% |
| Down-gear hysteresis | Let ΔT fluctuate around a threshold | 2℃ down-shift effective, no gear flapping |
| Humidity interlock | Breathe on the sensor to ≥75%RH | Immediately forces gear 0 with HUM LOCK, releases automatically |
| Scheduled ventilation | Configure a due plan | Runs gear 3 for the set duration; plans survive power cycles |
| Dual-mode switching | Switch storage/ambient and auto/manual via menu and app | Fan stops and state clears on switch; new cycle re-gears per mode |
| App remote control | Walk through all four pages on the LAN | Control takes effect immediately; curves and stats match serial telemetry |
Analysis: gearing with hysteresis strikes the intended balance between prompt response and avoiding frequent start/stop cycling; the humidity interlock and scheduled ventilation elevate the system from single-loop temperature control to multi-parameter decision-making; and the two-stage sampling keeps the device responsive to app polling at all times.
7 Conclusion and Future Work
This paper designed and implemented a grain-storage intelligent ventilation control system based on STM32, completing dual-sensor temperature measurement, gear-based target-differential control, humidity interlock, scheduled ventilation, runtime records, and full mobile-app remote control, with the whole system integrated on a custom PCB. The control strategy matches the thermal characteristics of a grain bulk, and the engineering implementation is complete and low-cost.
Future work can proceed in two directions: adding multi-point grain temperature sensing (multiple DS18B20 probes) to capture the temperature field inside the bulk; and feeding back ventilation effectiveness (the post-ventilation ΔT decay rate) to adaptively tune the gear thresholds, further aligning with industry thresholds from standards such as LS/T 1202.