Recent growth in magnetic levitation can be attributed to its ability to minimize friction and disturbance in industries, transportation, aerospace, biomedicine, and magnetic bearings. Due to the magnetic levitation system's nonlinear and unstable nature, control engineers found it exceedingly challenging to design a stabilizing controller. The magnetic levitation system is abbreviated as a maglev system. Using the integral square error criterion, a newly developed metaheuristic algorithm named the COOT algorithm is used to optimize the PID controller parameters. The performance of the proposed algorithm is evaluated using simulation and hardware with several kinds of reference trajectories and compared to the performance of other algorithms, such as the genetic algorithm and the whale optimization algorithm. Based on simulation and hardware results, it was determined that the proposed algorithm performed well with less settling time, rise time, and integral square error.