Design of a Grain-Storage Intelligent Ventilation System Based on STM32

#Smart Hardware#STM32#IoT

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:

  1. 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;
  2. 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;
  3. Humidity interlock: in storage mode, ambient humidity ≥ 75%RH forces shutdown with a prompt, preventing moist air from being drawn into the grain bulk;
  4. 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;
  5. 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

ModuleModelKey SpecificationsRole
ControllerSTM32F103C8T6Cortex-M3 / 72 MHz / 64 KB FlashControl core
Grain tempDS18B20±0.5℃, 1-wire, stainless probeBuried in-bulk temperature
Ambient temp/RHDHT11±2℃, ±5%RHAmbient-mode sensing + humidity interlock
Motor driverTB6612FNGDual H-bridge, PWMFan drive
RTCDS3231I2C, temperature-compensatedTimekeeping for schedules
StorageW25Q648 MB SPI FlashFont, plans, and runtime records
CommunicationESP-12F802.11 b/g/nHTTP API + NTP
DisplayOLED 0.96"128×64, SSD1306Chinese status display

3 Hardware Design

3.1 Pin Assignment

FunctionPinConfiguration
Fan PWMPA0TIM2_CH1, 1 kHz
Motor direction AIN1/AIN2PA1 / PA2Push-pull output
DS18B20PB11-wire, 4.7 kΩ pull-up
DHT11PA8Open-drain + pull-up
W25Q64PA4–PA7SPI1
OLEDPB8 / PB9Software I2C
Keys ×4PA3, PB5–PB7EXTI, 300 ms debounce
Debug UARTPB10 / PB11USART3, IDLE+DMA
ESP12FPA9 / PA10USART1, 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.

System hardware
Figure 3-1 System hardware

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;
}
ΔTGearPWM
ΔT < 2℃00
2 ≤ ΔT < 5℃125%
5 ≤ ΔT < 8℃250%
ΔT ≥ 8℃3100%

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).
Control page
Figure 5-1 Control page
Plans page
Figure 5-2 Scheduled ventilation page
Data page
Figure 5-3 Runtime data page
Settings page
Figure 5-4 Settings page

6 Testing and Analysis

Test ItemMethodResult
Differential gearingHeat until ΔT crosses 2/5/8℃ in turnGears step 1/2/3 correctly with PWM at 25%/50%/100%
Down-gear hysteresisLet ΔT fluctuate around a threshold2℃ down-shift effective, no gear flapping
Humidity interlockBreathe on the sensor to ≥75%RHImmediately forces gear 0 with HUM LOCK, releases automatically
Scheduled ventilationConfigure a due planRuns gear 3 for the set duration; plans survive power cycles
Dual-mode switchingSwitch storage/ambient and auto/manual via menu and appFan stops and state clears on switch; new cycle re-gears per mode
App remote controlWalk through all four pages on the LANControl 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.