Frequency control, especially when incorporating distributed generation units such as wind and solar power plants, is crucial for maintaining grid stability. To address this issue, a study proposes a method for controlling the connection status of electric vehicles (EVs) to prevent frequency fluctuations. The method utilizes an adaptive neural-fuzzy inference system (ANFIS) and a whale optimization algorithm to regulate the charging or discharging of EV batteries based on frequency fluctuations. The objective is to minimize and adjust the frequency fluctuations to zero. The proposed method is evaluated using a real microgrid composed of a wind power plant, a solar power plant, a diesel generator, a large household load, an industrial load, and 711 electric vehicles. The ANFIS system serves as the primary controller, taking inputs such as electric vehicle and battery status and generating outputs that determine the charging or discharging of the electric vehicles. Several investigations are conducted to assess the effectiveness of this model, and the results obtained are compared with the normal state where electric vehicles only consume power. By implementing this method, it is expected that the connection status of electric vehicles can be optimized to help stabilize the grid and minimize frequency fluctuations caused by the integration of distributed renewable energy sources. This study highlights the importance of automatic frequency control in smart grids and offers a potential solution using ANFIS and the whale optimization algorithm.