This research article presents a study on balancing a two-wheeled Segway robot using a Linear Quadratic Regulator (LQR) control system optimized by Genetic Algorithm (GA) and Bacteria Foraging Optimization Algorithm (BFOA). The article outlines the mathematical modeling and dynamics of the robot, detailing the controller design and simulation results that demonstrate the BFOA-LQR controller's effectiveness in achieving minimal overshoot and oscillation. It emphasizes the importance of using optimization algorithms to enhance the LQR controller's performance in stabilizing the inherently unstable Segway robot.