A two-wheeled self-balancing robot. This project utilizes an Arduino-based controller to maintain vertical stability using an IMU sensor and stepper motors.
The robot uses a PID (Proportional-Integral-Derivative) control loop to stay upright. It processes data from an MPU6050 (Accelerometer + Gyroscope) and uses a Complementary Filter to estimate its tilt angle accurately. Additionally, an ultrasonic sensor is included for basic obstacle detection/avoidance logic.
- Microcontroller: Arduino compatible board.
- IMU Sensor: MPU6050 (for tilt sensing via I2C).
- Motors: 2x Stepper Motors (driven using
AccelStepperlibrary). - Distance Sensor: HC-SR04 Ultrasonic sensor.
- Power: External battery source for motors.
The software is organized around a simple Task Scheduler to manage different operations at specific intervals.
Instead of using a complex RTOS, the project implements a lightweight scheduler that triggers tasks based on a tick counter.
- High Priority: Control loop (PID and motor updates) runs every tick.
- Lower Priority: Sensor reading and statistical calculations (Welford's algorithm) run every 40 ticks.
Combining accelerometer and gyroscope data is critical:
- Accelerometer: Provides a stable long-term reference for gravity but is noisy during movement.
- Gyroscope: Provides very accurate short-term tilt data but drifts over time.
- Complementary Filter:
Angle = alpha * (Angle + Gyro_Data) + (1 - alpha) * Accel_Angle. This blends both sensors to get a clean, drift-free tilt estimate.
The core of the balancing logic:
- Proportional (Kp): Corrects the error based on current tilt.
- Integral (Ki): Corrects accumulated steady-state errors.
- Derivative (Kd): Predicts future error to dampen oscillations.
The project implements Welford's online algorithm to calculate the running mean and variance of the angle and distance measurements. This allows for statistical analysis of sensor noise without storing large arrays of data.
Key constants can be tuned in main.ino:
Kp,Ki,Kd: PID gains for balancing.TARGET_ANGLE: The mechanical balance point (default is -90.0).alpha: The filter coefficient (balancing responsiveness vs. noise).
- Connect the hardware according to the pin definitions in
main.ino. - Install the
AccelStepperlibrary in your Arduino IDE. - Upload
main.inoto your board. - Keep the robot steady at its balance point during startup for calibration.