Effects of various molar ratios of monomer (aniline), oxidant (ammonium peroxydisulfate), and dopant (hydrochloric acid) on the polymerization of polyaniline (PANI) were investigated using direct ultrasonic irradiation technique. The effects of varying molar ratio of dopant and oxidant on the structural stability, morphology, and electrical conductivity of the prepared PANI were studied. Firstly, a scheme derived from electrical conductivity point of view, by varying the molar ratio of ammonium peroxydisulfate (APS). As the molar ratio of APS to aniline (ANI) varied from 0.1 to 1.25, the conductivity of PANI reached a maximum of 0.24 S/cm at a ratio of 1. Thereafter, by fixing the optimized molar ratio of APS and aniline the molar ratio of hydrochloric acid (HCl) was varied. The conductivity of PANI increased with an increase of HCl concentration and reached a maximum of 0.5 S/cm at an HCl concentration of 2 M. Finally, the formation mechanism for polymerization of PANI were discussed using Fourier transform infrared spectra, ultraviolet–visible spectra, nuclear magnetic resonance spectroscopy, and X‐ray diffraction spectroscopy. The final product is in protonated form, possessing structural stability and electrically conductive.