In this article, an optimized PID controller for a fuel cell is introduced. It should be noted that we did not compute the PID controller's coefficients based on trial-and-error method; instead, imperialist competitive algorithms have been considered. At first, the problem will be formulated as an optimization problem and solved by the mentioned algorithm, and optimized results will be obtained for PID coefficients. Then one of the important kinds of fuel cells, called proton exchange membrane fuel cell, is introduced. In order to control the voltage of this fuel cell during the changes in the charges, an optimal controller is introduced, based on the imperialist competitive algorithm. In order to apply this algorithm, the problem is written as an optimization problem which includes objectives and constraints. To achieve the most desirable controller, this algorithm is used for problem solving. Simulations confirm the better performance of proposed PID controller.