# ducphan18/self-balance-car architecture

> Architecture diagram of the GitHub repository ducphan18/self-balance-car, generated by GitDiagram from its file tree, README and a sample of source files (last updated 2025-04-03). It is an AI-made overview: check details against the source.

- Interactive diagram: https://gitdiagram.com/ducphan18/self-balance-car
- Repository: https://github.com/ducphan18/self-balance-car

## Overview

##### Project Overview
- This is an Arduino-based self-balancing car. Its purpose is to leverage sensor data (from the MPU6050, an accelerometer/gyroscope) and a PID control loop to compute and drive the appropriate commands through a motor driver (L298N) to keep the car balanced.
- The project includes custom libraries for sensor communication (I2Cdev, MPU6050), motor control (LMotorController), and the control algorithm (PID) along with associated example sketches.

##### Identifying Main Components (Modules)
a. Hardware Components:
- Arduino Nano (central microcontroller running the self_balance_car.ino sketch)
- MPU6050 sensor
- Motor Driver (L298N)
- Motors (2 gear motors attached to rubber wheels)
- Power supply parts (LM2596 step-down module, battery base with 18650 batteries)
- Connections (wiring and breadboard as per Schematic)

b. Software Components (Code Modules):
- Main Sketch: “self_balance_car.ino” which ties together the libraries to perform the balancing algorithm.
- Sensor Interface: Libraries “I2Cdev” and “MPU6050” (plus its multiple variants and examples) manage all communication with the sensor and process raw data.
- Control Algorithm: “PID” library implements the control loop used for dynamically adjusting motor signals based on sensor feedback.
- Motor Control: “LMotorController” library abstracts the motor control commands sent to the L298N driver.
- Additional support: Example sketches within the libraries, which illustrate usage but aren’t necessarily part of the core functionality.

##### Architectural Relationships & Interactions
- Data Flow:
- Sensor Data Acquisition: The MPU6050 captures orientation/acceleration data and communicates via I2C (handled by I2Cdev) to the Arduino.
- Data Processing & Control: The main sketch processes the sensor data using the MPU6050 library. Then, a PID algorithm computes the necessary adjustment in motor speed to achieve balance.
- Actuation: The motor commands are then passed to the LMotorController, which drives the motors through the L298N.
- Communication: Note the directional data flows – sensor → control algorithm → motor control – with feedback loops inherent in PID control.

##### Final Elements
- All components should be clearly labeled.
- Interconnections and key libraries that implement these functions.

## Diagram (Mermaid)

```mermaid
flowchart TD
    subgraph "Hardware Components"
        A("Arduino Nano"):::hardware
        S("MPU6050 Sensor"):::hardware
        M("Motor Driver (L298N)"):::hardware
        O("Motors"):::hardware
        P("Battery & LM2596"):::hardware
    end

    subgraph "Software Components"
        MA("Main Application (self_balance_car.ino)"):::software
        SI("I2Cdev"):::software
        SM("MPU6050 Library"):::software
        PID("Control Algorithm (PID Library)"):::software
        MC("Motor Control (LMotorController Library)"):::software
    end

    S -->|"I2C_Comm"| SI
    SI -->|"Sensor_Data"| MA
    SM -->|"Library_Data"| MA
    MA -->|"Adjustment_Calc"| PID
    PID -->|"Control_Signal"| MC
    MC -->|"Drive_Command"| M
    M -->|"Motor_Power"| O
    MA ---|"Runs_on"| A

    click MA "https://github.com/ducphan18/self-balance-car/blob/main/Code/self_balance_car/self_balance_car.ino"
    click SI "https://github.com/ducphan18/self-balance-car/tree/main/Code/library/I2Cdev"
    click SM "https://github.com/ducphan18/self-balance-car/tree/main/Code/library/MPU6050"
    click PID "https://github.com/ducphan18/self-balance-car/tree/main/Code/library/PID"
    click MC "https://github.com/ducphan18/self-balance-car/tree/main/Code/library/LMotorController"

    classDef hardware fill:#ADD8E6,stroke:#000,stroke-width:2px;
    classDef software fill:#90EE90,stroke:#000,stroke-width:2px;
```
