ESP32 Based Basic Flight Controller for a Quadcopter
by pallabdashere in Circuits > Arduino
330 Views, 2 Favorites, 0 Comments
ESP32 Based Basic Flight Controller for a Quadcopter

This project aims to design and develop a basic quadcopter flight control system using the ESP32 microcontroller programmed via the Arduino framework. The goal is to create a cost-effective, modular, and beginner-friendly platform to explore the fundamentals of multirotor flight, including sensor integration, motor control, and stability algorithms.The project involves both hardware and software components, where the ESP32 acts as the core flight controller, managing the drone's orientation and movement in real time.
FUNCTIONAL OVERVIEW:
- Reads RC commands using a **FlySky PPM receiver** via the `PPMReader` library.
- Collects real-time sensor data from an **MPU6050 IMU** over I2C.
- Uses a **dual-loop PID controller** (angle + rate) to stabilize roll, pitch, and yaw.
- Computes Kalman filter for estimating stable orientation.
- Outputs **PWM signals** to four ESCs (Electronic Speed Controllers) using ESP32’s hardware timers.
- Monitors **battery voltage** through an ADC pin using a voltage divider and warns the user when voltage drops below safe levels.
- Handles tasks in **parallel** using FreeRTOS: flight control and battery monitoring.
Supplies











List of materials Required for the development and their quantity
- Quadcopter frame - 1(QTY)
- 30 A ESC - 4(QTY)
- A2212/10T-1400KV Brushless Motors -4(QTY)
- 10 inch Propellers - 4(QTY)
- 2200mah 3s 11.1V Lipo battery-1(QTY)
- Fly Sky FS-i6 6-Channel 2.4 Ghz Transmitter-1(QTY)
- FS-iA6 Receiver-1(QTY)
- Wires /connectors
- ESP32 WROON DEVKIT (Microcontroller)-1(QTY)
- MPU6050(Gyro/Accelerometer)-1(QTY)
- Lipo battery charger-1(QTY)
You can buy all these from www.Robocraze.com India or any other source
Gather the Requirements and Connections



This flight controller will support:
- 4 PWM outputs to ESCs (for motors)
- MPU6050 IMU sensor via I2C
- Battery voltage monitor
- PPM signal input from RC receiver
A. ESP32 Core Connectios
- GND to Common ground
- 3v3 to MPU6050
- VIN to Powering from 5v output from one of the ESC
B. MPU6050 Sensor (I2C)
- Connect VCC to 3.3V
- GND to GND
- SCL → ESP32 GPIO 22
- SDA → ESP32 GPIO 21
You can pull-up SDA and SCL with 4.7k resistors to 3.3V if needed.
C. PWM Outputs to ESCs
Use 4 GPIOs to drive PWM outputs using ledcWrite.
ESC -> ESP32 GPIO
- Motor 1 ->GPIO 5
- Motor 2 ->GPIO 18
- Motor 3 ->GPIO 19
- Motor 4 ->GPIO 15
The ESC control pins has three pins together, connect these pins to the ESP32 accordingly
- 5V Output
- Input Signal Wire
- Ground
Use pin headers or JST connectors for output.
D. Battery Voltage Monitor
- Use a voltage divider (11V max down to 3.3V)
- Connect divided voltage to ADC input GPIO 35
- Label this net as VBAT_SENSE
E. PPM Signal Input (from RC Receiver)
- Connect PPM output to GPIO 4 (or any interrupt-capable pin)
Design the Circuit
.jpg)

You can use any tool to design the circuit. I have used the online EasyEDA tool to design the circuit
Downloads
HARDWARE SETUP REQUIRED

1. Microcontroller:
Use the designed circuit board
- ESP32 Devkit V1
- Must support 4 PWM outputs.
2. Sensors:
- MPU6050 (Accelerometer + Gyroscope)
- Connect via I2C (SDA = GPIO21, SCL = GPIO22 or similar).
- Power from 3.3V.
- Calibrated during setup.
3. Motors and ESCs:
- 4 Brushless motors with Electronic Speed Controllers (ESCs).
- Connect the esc to the battery through the XT60 connector
- Connect the motors to the ESCs
- ESC PWM control pins connected to GPIOs:
- Motor 1: GPIO 5 (Anti- clockwise)
- Motor 2: GPIO 18 (Clockwise)
- Motor 3: GPIO 19 (Anti- clockwise)
- Motor 4: GPIO 15 (Clockwise)
- Powered via LiPo battery (3S recommended).
- How to Set Motor Rotation Direction
- A brushless motor has three wires, connected to three wires from the ESC (Electronic Speed Controller).
- Swap Any Two Wires to Change Direction
- If your motor is spinning in the wrong direction, simply swap any two of the three motor wires connected to the ESC.
- This will reverse the motor's rotation direction.
4. PPM Receiver:
- RC Transmitter & Receiver system that outputs a PPM signal.
- Power the RC receiver through an esc(Recomended) although the circuit designed by me powers it from the micrcontroller, you can modify the connections.
- PPM signal to GPIO 4.
- Supports 6 channels.
5. Power and Battery Monitoring:
- Voltage divider circuit connected to ADC pin (GPIO25).
- Uses two resistors (77.6kΩ and 29.4kΩ).
- Monitors input voltage; warns if battery drops below 9V.
6. Optional Peripherals:
- LED on GPIO2 for battery warning.
SOFTWARE COMPONENTS REQUIRED

1. Sensor Handling (IMU):
- Reads gyro and accelerometer data from MPU6050 using I2C.
- Applies Kalman Filter to estimate roll and pitch angles.
- Calibrates gyro offsets during setup().
2. RC Signal Decoding (PPM):
- PPM input is parsed using PPMReader library.
- 6 channels mapped as:
- Channel 1: Roll
- Channel 2: Pitch
- Channel 3: Throttle
- Channel 4: Yaw
- Others: Optional
- If signal is lost for more than 5 seconds:
- Motors are shut down.
- PID states reset.
3. PID Control Logic:
- Dual loop PID:
- Outer loop stabilizes angles (roll, pitch).
- Inner loop stabilizes angular rates.
- PID tuned with adjustable parameters for:
- Roll, Pitch, Yaw rate (PRate, IRate, DRate)
- Roll, Pitch angle (PAngle, IAngle, DAngle)
- PID output drives each motor to maintain desired orientation.
4. Motor Mixing Logic:
Converts PID outputs + throttle into ESC PWM signals for 4 motors:
- Output values are clamped between 1000 (idle) and 1989 (max).
SYSTEM TASK MANAGEMENT (FreeRTOS)
Runs 2 concurrent tasks:
- batteryMonitorTask (Core 0):
- Monitors battery voltage.
- Blinks LED if below threshold.
- flightControlTask (Core 1):
- Reads sensor data and PPM input.
- Executes Kalman filtering.
- Runs angle and rate PID control.
- Mixes motor outputs.
- Sends PWM to ESCs.
Downloads
STEPS TO MAKE IT WORK
1. Assemble Hardware
- Solder and connect all required components (MPU, ESCs, motors, battery, receiver).
- Use a flight-ready frame.
2. Flash Firmware
- Load the code into ESP32 using Arduino IDE.
- Install ESP32 boards dependencies from boards manager
- Install required libraries:
- PPMReader.h
- Wire.h
- freertos/FreeRTOS.h
3. Calibrate Gyroscope
- Happens during startup automatically.
- Keep the drone still during boot.
4. Set PID Values
- Use default values or tune manually.
- Optional: Implement the web interface for live tuning.
5. Power and Arm
- Power drone using LiPo battery.
- Turn on transmitter.
- Slowly increase throttle above cutoff (1050).
- Motors will start spinning at idle (≈1180).
- Drone is now armed.
Readme
/*
===================================================================================
🚀 Open Source Quadcopter Flight Controller – ESP32 Port
===================================================================================
📜 License & Acknowledgement:
This code is inspired and adapted from the **Carbon Aeronautics Flight Controller Manual**, originally designed for the **Teensy 4.0** microcontroller. The core logic and architecture were retained while porting the implementation to the **ESP32-WROOM-DevKit** platform using the **Arduino framework**.
This project was created for **educational and experimental purposes only**. All credits to Carbon Aeronautics for their excellent open-source initiative. I do not intend to use any part of this code for commercial applications.
📡 Key Components:
- **ESP32-WROOM-DevKit v3**
- **MPU6050 IMU Sensor** (I2C)
- **FlySky PPM Receiver** (decoded via `PPMReader` library)
- **FreeRTOS Tasks** for flight control and battery monitoring
- Dual-loop **PID control** for roll, pitch, and yaw stabilization
- PWM motor control using `ledcWrite()` for ESCs
💡 License:
This code is released under the **MIT License**.
You are free to **fork**, **modify**, and **redistribute** this project for personal or educational use.
Please keep this notice intact and credit the original and ported sources.
📧 Contact:
Author of this ESP32 port: **Pallab Das**
Gitbub: https://github.com/pallabdasc1/ESP32_code_for_quadcopter
===================================================================================
*/