A novel electrochemical sensor was constructed based on modification of polyaniline (PANI) and graphitic-phase carbon nitride (g-C3N4) composites. Detection of Cd(II) ions in the aqueous environment using differential pulsed anodic stripping voltammetry (DPASV) technique. Scanning electron microscopy (SEM), X-ray photoelectron spectroscopy (XPS), electrochemical impedance spectroscopy (EIS), contact angle (CA), and Tafel curve analyses were used to characterize the physical and electrochemical properties of electrode. We prepared a new PANI@g-C3N4 composite material by combining the two substances based on their respective advantages. The composite material was applied to electrode detection for the first time, which significantly enhanced the transfer of free electrons on the surface of the electrode, improved the sensitivity of the electrode, increased the adsorption capacity of Cd ions, and significantly improved the detection effect of the electrode. Parameters such as the amount of PANI@g-C3N4 modification, deposition potential, deposition time and solution pH were optimized to determine the best conditions for the detection of Cd(II) ions. Under optimal conditions, our sensor achieves a best signal at −0.78 V (vs. Ag/Agcl electrode), and exhibits a wide linear concentration range of 0.1–140 μg/L with a low detection limit of 0.05 μg/L. The sensor was successful in implementing the identification of real water samples with recoveries ranging from 91 % to 106 %. The relative standard deviation (RSD) is less than 4.31 %. Besides, the sensor has outstanding anti-interference, repeatability and steadiness. The successful application of this sensor provides a new idea for efficient detection of Cd(II) ions in aquatic environments.