The two wheeled self-balancing robot’s body was built based on LEGO robotics for balance control problems of self-balancing robot. Photoelectric sensor was adopted as the inclination sensing element, instead of the traditional inclination sensors and gyroscopes, to reduce costs and avoid the temperature drift. The body composition of this robot was given and the mathematical model of the system was established based on Euler-Lagrange method. Meanwhile, optimal control theory was used to achieve balance of the robot. Finally, the physical experiments results show that the robot has good self balance ability and a certain anti-jamming capability. It is proved that the two-wheeled self-balancing robot’s control method based on photoelectric sensor is reasonable and effective.