Developing effective sensing method for trace analysis of ampicillin (AMP) is urgent and significant due to its residue possess serious threats to human health. Herein, a p-n heterojunction, on the basis of p-type BiFeO3 nanoparticles coupled n-typed ultrathin graphite-like carbon nitride (utg-C3N4) nanosheets, has been designed and synthesized via a simple electrostatic interaction strategy. Such p-n heterojunction has two advantages: one is capable to narrow the band gap of photoactive materials from 2.20 eV of BiFeO3 down to 2.04 eV of BiFeO3/utg-C3N4, leading to improve the efficiency of visible light utilization; and the other is to facilitate the charge separation rate, resulting in the boosted photoelectrochemical (PEC) performance of BiFeO3/utg-C3N4. Under visible light illumination, the photocurrent of the resulted BiFeO3/utg-C3N4 was 7.0-fold enhanced than that of pure BiFeO3 nanoparticles, and indeed 2.3-fold enhanced comparing to BiFeO3/bulk-C3N4. Based on excellent PEC properties of BiFeO3/utg-C3N4, an on-off-on PEC aptasensor was successfully fabricated for ampicillin (AMP) determination with highly selectivity and sensitivity. The fabricated PEC aptasensor exhibited excellent PEC performance with a broad linear in the range from 1 × 10-12 mol L-1 to 1 × 10-6 mol L-1 as well as a low detection limit of 3.3 × 10-13 mol L-1 (S/N = 3), and also good feasibility in real sample. The excellent analytical performance indicated that PEC aptasensor on the basis of the visible light driven BiFeO3/utg-C3N4 heterojunction can provide a promising biosensor platform for sensitive detection AMP in food and environment analysis.