This paper focuses on modelling the thermal behaviour of buildings and designing an optimal control algorithm for their HVAC systems. The problem of developing a good model to capture the heat storage and heat transmission properties of building thermal elements such as rooms and walls is addressed by using the lumped capacitance method. The equations governing the system dynamics are derived using the thermal circuit approach, and by defining equivalent thermal masses, thermal resistors and thermal capacitors. In the control design part, we have introduced a control algorithm which is composed in Pontryagin's Minimum Principle controller. The optimal tracking problem is solved in Pontryagin's Minimum Principle controller where the interconnection of all walls are taken into consideration. The Pontryagin's Minimum Principle controller 0minimizes a quadratic cost function which has two quadratic terms. One takes into account the comfort level and the other represents the control effort, i.e. the energy consumed to operate the HVAC system. Simulation results for a single room example show energy savings using this control algorithm over a conventional PI controller.