The evolution of precision agriculture demands technological solutions that surpass the capabilities of traditional, labor-intensive crop protection methods. In this context, the development of efficient, reliable, and scalable unmanned aerial systems has become paramount. This article presents a comprehensive design for a compact agricultural drone, or plant-protection UAV, built around the STM32F103C8T6 microcontroller. The system is engineered to meet the requirements of small to medium-scale farming operations, offering a reliable, user-friendly, and highly efficient platform for targeted spraying. The core of this design lies in its integrated hardware architecture and sophisticated software algorithms for stable flight and precise control.
The traditional paradigm of agricultural plant protection is increasingly inadequate, characterized by blanket chemical application, significant manual labor, and potential exposure hazards. Modern precision agriculture necessitates targeted, data-driven interventions. The adoption of remote sensing and aerial application technologies represents a significant leap forward. An agricultural drone serves as an ideal platform for this purpose, enabling rapid deployment, precise application of agents (pesticides, fertilizers), and access to difficult terrain without soil compaction. For smaller plots where large machinery is impractical or economically unviable, a compact, intelligent agricultural drone provides an optimal solution. This project leverages advancements in embedded systems, sensor fusion, and control theory to create a robust plant-protection UAV system capable of autonomous stability and responsive manual control, thereby enhancing operational safety and efficiency.
The complete agricultural drone system is architected as a cohesive unit comprising an aerial vehicle and a ground-based handheld remote controller. The primary subsystems include the Flight Control Unit (FCU), the propulsion system, the spraying mechanism, the power distribution network, and the remote controller with its telemetry link.
The Flight Control Unit acts as the brain of the agricultural drone. Its central responsibility is to maintain stable flight attitude (orientation) and execute navigation commands. It continuously reads data from onboard sensors, fuses this information to estimate the drone’s precise orientation, runs control algorithms, and generates precise motor commands. The remote controller sends pilot inputs (e.g., throttle, pitch, roll, yaw) and operational mode commands via a wireless link. The FCU interprets these commands and adjusts its control objectives accordingly. The propulsion system, typically consisting of four brushless DC motors paired with Electronic Speed Controllers (ESCs), translates the FCU’s commands into thrust, enabling lift and maneuverability. The spraying apparatus, a pump and nozzle system, is responsible for dispensing the agrochemical. Its flow rate is digitally controlled by the FCU based on flight speed or manual input. A robust power system manages energy from the main Li-Po battery, providing stable voltage rails (e.g., 12V for motors, 5V and 3.3V for electronics) to all subsystems. The reliability of this power distribution is critical for the safe operation of the agricultural drone.
| Subsystem | Primary Components | Core Function |
|---|---|---|
| Flight Control Unit (FCU) | STM32F103C8T6, MPU6050, NRF24L01, AT24C02 | Sensor fusion, attitude estimation, control algorithm execution, communication. |
| Propulsion & Power | Brushless Motors, ESCs, Li-Po Battery, Voltage Regulators | Generate thrust and provide stable power to all electronic components. |
| Spraying Mechanism | DC Pump, Nozzle, MOSFET Driver Circuit | Precise dispensing of liquid agrochemicals. |
| Remote Controller | STM32F103C8T6, Joysticks/Potentiometers, NRF24L01 | Transmit pilot commands and receive telemetry data. |
Hardware Architecture Design
The hardware implementation is modular, focusing on reliability, real-time performance, and integration. The design centers on the flight controller but extends to the ground station and the application-specific spraying system.
Flight Control Circuit Core
The heart of the flight controller is the STM32F103C8T6 microcontroller unit (MCU). This ARM Cortex-M3 based chip operates at 72 MHz, providing sufficient computational power for real-time sensor data processing and control loop calculations. Its rich peripheral set, including multiple timers for Pulse-Width Modulation (PWM) generation, I2C, and SPI interfaces, makes it exceptionally suitable for an agricultural drone controller. The MCU is responsible for initializing all peripherals, reading sensor data, executing the attitude estimation and control algorithms, communicating with the remote, and outputting control signals to the ESCs and the spray pump.
Accurate and fast attitude sensing is non-negotiable for stable flight. The system employs the MPU6050, a 6-Degree-of-Freedom (6-DoF) Inertial Measurement Unit (IMU). This chip integrates a 3-axis accelerometer and a 3-axis gyroscope. The accelerometer measures proper acceleration (including gravity), which can be used to estimate orientation relative to Earth’s gravity vector. The gyroscope measures angular rate, which can be integrated over time to track orientation changes. However, each sensor has drawbacks: accelerometers are noisy during dynamic motion, and gyroscopes suffer from drift (bias) over time. The fusion of these two data streams in software is key. The MPU6050 communicates with the STM32 via the I2C protocol, providing a low-pin-count, moderate-speed data link essential for the compact design of the agricultural drone’s brain.
Non-volatile storage is handled by an AT24C02 EEPROM chip. This component stores critical configuration and calibration parameters that must persist across power cycles. For an agricultural drone, this includes unique identifier (UID) for radio pairing, calibrated sensor offsets (IMU bias), and tuned parameters for the Proportional-Integral-Derivative (PID) control loops. Storing these in EEPROM allows the system to maintain its performance without requiring recalibration on every startup.
The power architecture is designed for cleanliness and stability. The main drone battery (e.g., 3S Li-Po, ~11.1V nominal) powers the high-current propulsion system directly. A switching regulator (e.g., MP1593) steps this voltage down to a stable 5V rail for peripherals like the ESCs (which often have a 5V BEC), the radio module, and the pump driver. Subsequently, one or more low-dropout linear regulators (e.g., TPS73633) derive a very clean 3.3V supply from the 5V rail for the noise-sensitive digital electronics: the STM32 MCU, the MPU6050 IMU, and the EEPROM. This layered power design prevents noise from the motors from corrupting the sensitive sensor readings and digital logic, which is a common pitfall in UAV design.
| Module | Key Component | Interface to MCU | Primary Function |
|---|---|---|---|
| Main Controller | STM32F103C8T6 | N/A (Core) | System management, data processing, control algorithm execution. |
| Attitude Sensor | MPU6050 (IMU) | I2C (SDA, SCL) | Provide raw accelerometer and gyroscope data. |
| Data Storage | AT24C02 (EEPROM) | I2C | Store calibration data, PID parameters, and system settings. |
| Wireless Comms | NRF24L01+ | SPI (MOSI, MISO, SCK, CSN) + GPIO (CE, IRQ) | Bi-directional communication with the remote controller. |
| Power Regulation | MP1593 (12V->5V), TPS73633 (5V->3.3V) | N/A | Provide stable, clean voltage levels to all electronic components. |
Handheld Remote Controller Design
The ground control station is a custom handheld remote controller. It is built around another STM32F103C8T6 MCU for consistency and development ease. Its primary inputs are analog joysticks or potentiometers, which are read via the STM32’s internal Analog-to-Digital Converters (ADCs). These sticks provide continuous values for throttle, pitch, roll, and yaw commands. Additional switches and buttons are used for operational modes (e.g., arm/disarm, altitude hold, spray on/off). The command data from these inputs is packetized and transmitted to the agricultural drone via an NRF24L01+ 2.4GHz radio transceiver module, which is functionally identical to the one on the flight controller. This creates a robust private telemetry and control link.
Spraying Mechanism Actuation
The plant-protection function of the agricultural drone is realized through an electronically controlled spraying system. A DC diaphragm pump is used to draw liquid from a tank and pressurize it to a nozzle. The pump’s speed, and consequently the flow rate, is controlled by a Pulse-Width Modulated (PWM) signal generated by the flight controller’s STM32. This PWM signal drives a power Metal-Oxide-Semiconductor Field-Effect Transistor (MOSFET) in a switch-mode configuration. The average voltage applied to the pump motor is directly proportional to the duty cycle (D) of the PWM signal.
$$V_{pump\_avg} = D \cdot V_{supply}$$
where \( D \) is the duty cycle (0 to 1). Therefore, by modulating the PWM duty cycle output from the STM32, the flight controller can precisely regulate the spray output, enabling variable rate application—a key feature for precision agriculture. The nozzle selection also plays a critical role in determining droplet size and spray pattern, which are important factors for coverage and drift reduction.
Software System and Algorithmic Core
The software defines the intelligence and capabilities of the agricultural drone. It is structured as a real-time multi-tasking system to handle concurrent operations like sensor reading, attitude estimation, radio communication, and control loop execution reliably.
Real-Time Task Management
To manage complexity and ensure timely response, the software runs on a real-time operating system (RTOS) kernel, such as FreeRTOS or a similar scheduler. This allows the creation of distinct tasks with defined priorities. Key software tasks for the agricultural drone include:
- Sensor Read Task: Periodically reads raw data from the MPU6050 IMU via I2C.
- Attitude Estimation Task: Fuses the raw sensor data to compute the current roll (\(\phi\)), pitch (\(\theta\)), and yaw (\(\psi\)) angles.
- Radio Communication Task: Handles the sending and receiving of packets with the remote controller.
- Control Task: The highest-priority task that runs the PID control algorithms and updates motor and pump outputs.
- Battery Monitoring Task: Checks battery voltage and triggers low-voltage warnings.
This modular approach improves code maintainability and system reliability, which is essential for the robust operation of an agricultural drone in the field.
| Task Name | Priority | Primary Action | Key Output/Data |
|---|---|---|---|
| Sensor Read | High | Poll MPU6050 for accelerometer and gyroscope data. | Raw Accel [ax, ay, az], Raw Gyro [gx, gy, gz]. |
| Attitude Estimation | Very High | Execute Complementary Filter on sensor data. | Estimated Angles: Roll (φ), Pitch (θ), Yaw (ψ). |
| Radio Control | Medium | Decode incoming RC commands; send telemetry. | RC Channels [Throttle, Roll, Pitch, Yaw, Aux]. |
| Flight Control | Highest | Execute cascaded PID loops for attitude control. | Motor Thrust Commands [M1, M2, M3, M4]. |
| Spray Control | Medium | Map RC command or flight mode to pump PWM. | Pump PWM Duty Cycle. |
Sensor Fusion and Attitude Estimation
The cornerstone of stable flight is accurately knowing the aircraft’s orientation. As mentioned, the accelerometer and gyroscope data must be fused. A simple yet effective algorithm for this on a resource-constrained platform like the STM32 is the Complementary Filter. This filter combines the long-term stability of the accelerometer’s gravity-based angle estimate with the short-term responsiveness and dynamic accuracy of the gyroscope’s integrated angle.
The process begins by calculating the tilt angle from the accelerometer. Assuming the agricultural drone is moving relatively slowly (low linear acceleration), the direction of the measured acceleration vector is dominated by gravity. The roll (\(\phi_{acc}\)) and pitch (\(\theta_{acc}\)) can be estimated as:
$$\phi_{acc} = \arctan2(a_y, a_z)$$
$$\theta_{acc} = \arctan2(-a_x, \sqrt{a_y^2 + a_z^2})$$
where \(a_x, a_y, a_z\) are the accelerometer readings. The gyroscope provides the angular rate (\(p, q, r\) for roll, pitch, yaw rates). To get an angle from the gyro, we integrate the rate over time (\(\Delta t\)):
$$\phi_{gyro} = \phi_{prev} + p \cdot \Delta t$$
$$\theta_{gyro} = \theta_{prev} + q \cdot \Delta t$$
The complementary filter merges these two estimates using a weighting factor, \(\alpha\) (typically between 0.95 and 0.98):
$$\phi_{filtered} = \alpha \cdot (\phi_{prev} + p \cdot \Delta t) + (1 – \alpha) \cdot \phi_{acc}$$
$$\theta_{filtered} = \alpha \cdot (\theta_{prev} + q \cdot \Delta t) + (1 – \alpha) \cdot \theta_{acc}$$
This elegant algorithm provides a robust, drift-free, and responsive attitude estimate that is perfectly suited for the computational budget of an STM32F103-based agricultural drone. Yaw (\(\psi\)) is typically derived from integrating the gyroscope’s yaw rate, as there is no constant magnetic field for reference like gravity. For heading hold, a magnetometer could be added in a more advanced design.
Cascaded PID Flight Control
With a reliable attitude estimate, the next step is to control it. The industry-standard method is the Proportional-Integral-Derivative (PID) controller. For an agile multirotor like an agricultural drone, a cascaded control structure is employed. This involves two control loops nested within each other: a fast inner loop controlling angular rate, and a slower outer loop controlling the angle itself.
The outer loop (angle controller) takes the desired angle (e.g., \(\theta_{target}\) from the remote controller) and the current estimated angle (\(\theta_{filtered}\)). It calculates an error and processes it through a PID law to output a desired angular rate (\(\omega_{target}\)).
$$\omega_{target} = K_{P\_angle} \cdot e_{angle} + K_{I\_angle} \cdot \int e_{angle} \, dt + K_{D\_angle} \cdot \frac{de_{angle}}{dt}$$
where \(e_{angle} = \theta_{target} – \theta_{filtered}\)
The inner loop (rate controller) is the high-performance core. It takes the desired rate (\(\omega_{target}\)) and the current measured gyro rate (\(\omega_{gyro}\)). Its PID output is the final command sent to the motor mix algorithm, which distributes thrust to the four motors to achieve the demanded torque.
$$u_{motor} = K_{P\_rate} \cdot e_{rate} + K_{I\_rate} \cdot \int e_{rate} \, dt + K_{D\_rate} \cdot \frac{de_{rate}}{dt}$$
where \(e_{rate} = \omega_{target} – \omega_{gyro}\)
This cascaded structure, often visualized in block diagrams, provides excellent stability and disturbance rejection. The inner rate loop damps sudden movements and handles aerodynamic effects, while the outer angle loop ensures the agricultural drone maintains the pilot’s commanded orientation. The parameters (\(K_P, K_I, K_D\)) for both loops are tuned empirically during flight tests and stored in the EEPROM.
Control Flow and System Integration
The software execution follows a logical pipeline. Upon startup, the system initializes all hardware (MCU clocks, I2C, SPI, PWM timers, ADC) and reads configuration from EEPROM. The main control loop then begins at a fixed high frequency (e.g., 500 Hz). In each cycle:
- The IMU data is read.
- The complementary filter updates the estimated roll and pitch angles.
- The radio task provides the latest pilot commands (target angles for roll/pitch, target yaw rate, throttle base).
- For roll and pitch, the cascaded PID controller processes the angle error and rate error to compute correction outputs.
- For yaw, a single PID loop typically controls the yaw rate directly.
- A “mixer” function combines the throttle base with the roll, pitch, and yaw corrections to generate individual thrust commands for the four motors. For an “X” configured quadcopter, the mixing is:
$$M1 = T + P + R – Y$$
$$M2 = T + P – R + Y$$
$$M3 = T – P – R – Y$$
$$M4 = T – P + R + Y$$
Where \(T\) is throttle, \(P\) is pitch correction, \(R\) is roll correction, \(Y\) is yaw correction, and M1-M4 are the motor commands.
- These motor commands are output as PWM signals to the ESCs.
- Concurrently, the spray pump PWM duty cycle is calculated based on an auxiliary channel from the remote or an automated logic tied to flight mode.
This tightly integrated loop allows the agricultural drone to react instantly to disturbances (like wind gusts) and maintain stable, controllable flight—a prerequisite for accurate spraying.
| Algorithm | Mathematical Foundation | Role in Agricultural Drone | Implementation Note |
|---|---|---|---|
| Complementary Filter | $$\theta = \alpha \cdot (\theta_{prev} + q\Delta t) + (1-\alpha)\cdot \arctan2(-a_x, \sqrt{a_y^2+a_z^2})$$ | Fuses accelerometer and gyroscope data to produce a stable, drift-free attitude estimate. | Low computational cost, ideal for STM32F103. Requires tuning of filter gain (α). |
| Cascaded PID Control | $$u_{inner} = K_{P_r}e_r + K_{I_r}\sum e_r + K_{D_r}(e_r – e_{r\_prev})$$ $$e_r = \omega_{target} – \omega_{gyro}$$ $$\omega_{target} = K_{P_a}e_a + K_{I_a}\sum e_a + K_{D_a}(e_a – e_{a\_prev})$$ $$e_a = Angle_{target} – Angle_{estimated}$$ |
Maintains stable flight attitude. Inner loop (rate) provides damping; outer loop (angle) tracks pilot command. | Requires careful tuning of 6 parameters (P,I,D for angle and rate). Runs in the highest priority task. |
| Motor Mixing | $$M_{1,2,3,4} = f(T_{base}, U_{roll}, U_{pitch}, U_{yaw})$$ | Translates axis-specific correction signals into individual motor thrust commands for a quadcopter. | Geometry-specific (e.g., “X” vs. “+” configuration). Implemented as a static matrix multiplication. |
| PWM Flow Control | $$D_{pump} = k \cdot V_{ground\_speed}$$ or $$D_{pump} = f(AUX_{channel})$$ | Regulates the agrochemical application rate, enabling constant dose per area or manual control. | Can be linked to GPS speed for precision agriculture, or simply to a manual knob on the remote. |
System Integration, Calibration, and Field Validation
The final stage involves bringing all hardware and software components together. After assembling the airframe, mounting all electronics, and connecting the propulsion system, the firmware is flashed onto the STM32 controllers. A critical first step is sensor calibration. The IMU (MPU6050) must be calibrated to find its zero-rate and acceleration biases. This is often done by placing the agricultural drone on a level surface and collecting sensor data over several seconds to compute averages, which are then stored in the EEPROM and subtracted from subsequent readings.
The most delicate process is tuning the PID control gains. This is done incrementally, often starting with the inner rate loop. The agricultural drone is secured (e.g., on a test stand) and the rate P gain is increased until the motors respond crisply to stick commands without oscillation. The rate D gain is then added to dampen any overshoot. The outer angle loop P gain is tuned next to achieve accurate attitude holding. The I terms are carefully added to eliminate steady-state error (e.g., a constant lean under wind). Poor tuning can lead to instability, highlighting the importance of methodical testing for a reliable agricultural drone.
Once basic stability is achieved, functional integration tests are performed. The wireless link is verified for range and reliability. The spray pump is tested at various duty cycles to characterize flow rate. Finally, the complete system undergoes free-flight testing in an open field, starting with basic hover and manual flight, progressing to waypoint navigation or automated pattern flying if such features are implemented. The agricultural drone’s ability to maintain stable flight while carrying a liquid payload and executing spray commands is the ultimate validation of the design.

The transition towards smart and precision farming is inexorable, driven by the need for efficiency, sustainability, and productivity. In this landscape, compact, intelligent aerial platforms play a transformative role. This design exercise demonstrates a complete implementation of a plant-protection agricultural drone based on widely available and cost-effective components like the STM32F103C8T6 microcontroller. By integrating robust hardware design with sophisticated software algorithms—notably sensor fusion via complementary filtering and high-performance control via cascaded PID loops—the resulting system achieves the core requirement of stable, controllable flight. The addition of a digitally controlled spraying mechanism transforms the platform from a simple drone into a purposeful agricultural tool. While this design focuses on fundamental autonomy and control, it establishes a foundation that can be extended with additional features like GPS for position hold and automated swathing, flow sensors for precise application monitoring, and more advanced flight controllers. The successful development and validation of such systems underscore the significant potential of unmanned aerial technology to revolutionize plant protection practices, making them safer, more efficient, and more accessible, particularly for small-scale farming operations.
