This paper presents an optimal controller with integral action based on Takagi-Sugeno (TS) fuzzy modeling of a multivariable nonlinear system to control a two-wheeled inverted pendulum. The T-S fuzzy model was obtained by generating input/output data using Monte Carlo simulations. A Kalman Filter enhances trajectory tracking when estimating the orientation angle, which is problematic due to electromagnetic interference. Furthermore, the Kalman filter is also used to remove the drift effect of the gyro sensor to improve the estimation of the tilt angle. The proposed controller is robust against modeling uncertainties through mass change; it also presents a steady-state error very close to zero, which brings the system to the desired final states. The designed controller was implemented on the Lego Mindstorms NXT 2.0 educational platform using the RobotC programming language.