Arduino-based altitude control circuit for a mini-drone using an ultrasonic sensor and PID controller.
- Overview
- Hardware Components
- Circuit
- How It Works
- PID Controller
- Configuration
- Setup & Usage
- Serial Monitor Output
This project implements a closed-loop altitude control system for a mini-drone. An ultrasonic sensor measures the drone's current height, a potentiometer sets the target height, and a PID controller adjusts motor speed via two ESCs to maintain the desired altitude.
| Component | Description |
|---|---|
| Arduino Uno | Microcontroller |
| Ultrasonic Sensor (HC-SR04) | Measures altitude via echo signal |
| ESC x2 | Electronic Speed Controllers for motor control |
| Brushless Motors x2 | Drone propulsion |
| Potentiometer | Sets target altitude |
| Push Button | Start/stop control |
| Component | Arduino Pin |
|---|---|
| Ultrasonic Trigger | Pin 7 |
| Ultrasonic Echo | Pin 8 |
| ESC 1 | Pin 9 |
| ESC 2 | Pin 10 |
| Push Button | Pin 3 |
| Potentiometer | A0 |
- Start — Press the button to arm the drone. The ESCs go through a calibration sequence on startup.
- Liftoff — Motor speed ramps up gradually for a smooth takeoff.
- Altitude sensing — The ultrasonic sensor fires a 10µs pulse and measures the echo return time, which is converted to a distance in centimetres.
- Target setting — The potentiometer maps to a target height between
min_height(10cm) andmax_height(50cm). - PID control — The controller computes the error between target and measured height and adjusts the PWM signal sent to both ESCs every 100ms.
- Stop — Press the button again to trigger a controlled landing.
The control output is calculated as:
output = (Kp × error) + (Ki × errSum × SampleTime) + (Kd × dErr / SampleTime) + PWM_offset
| Parameter | Value | Description |
|---|---|---|
Kp |
3 | Proportional gain |
Ki |
0 | Integral gain |
Kd |
0 | Derivative gain |
SampleTime |
100ms | Control loop interval |
PWM_offset |
50 | Hover offset to keep drone airborne |
The PWM signal is clamped between
0and180to stay within safe ESC range.
The following constants can be tuned at the top of the sketch:
const int min_height = 10; // minimum target height in cm
const int max_height = 50; // maximum target height in cm
const int ramp_time = 40; // motor ramp up/down speed in ms
int SampleTime = 100; // PID update interval in ms
double Kp = 3; // proportional gain
double Ki = 0; // integral gain
double Kd = 0; // derivative gain
int PWM_offset = 50; // hover PWM value- Wire the components according to the circuit table above.
- Open
mini_drone_altitude_control.inoin the Arduino IDE. - Select Arduino Uno as the board and the correct COM port.
- Upload the sketch.
- Open the Serial Monitor at 9600 baud to observe the ESC calibration sequence.
- Press the button to start the drone.
- Adjust the potentiometer to change the target altitude.
- Press the button again to land.
Warning: Test in a safe, open area. Keep hands clear of propellers during operation.
ESC calibration process
Starting...
Now writing maximum output
Now writing minimum output
Press button to start minidrone
Target distance: 30
Measured distance: 28
Error: 2
PWM: 56
Button Status: 1
Ben Finio, Science Buddies, 2021.
Patricia Munginga, 2023