The discovery and development of photoelectrochemical sensors with novel principles are of great significance to realize sensitive and low-cost detection. In this paper, a new photoelectrochemial sensor based on the in situ formation of p-n junction was designed and used for the accurate determination of mercury(II) ions. Cysteine-capped ZnS quantum dots (QDs) was assembled on the surface of indium tin oxide (ITO) electrode based on the electrostatic interaction between Poly(diallyldimethylammonium chloride) (PDDA) and Cys-capped ZnS QDs. The in situ formation of HgS, a p-type semiconductor, on the surface of ZnS facilitated the charge carrier transport and promoted electron-hole separation, triggered an obviously enhanced anodic photocurrent of Cys-capped ZnS QDs. The formation of p-n junction was confirmed by P-N conductive type discriminator measurements and current-voltage (I-V) curves. The photoelectrochemical method was used for the sensing of trace mercuric (II) ions with a linear concentration of 0.01 to 10.0 µM and a detection limit of 4.6×10(-9)mol/L. It is expected that the present study can serve as a foundation to the application of p-n heterojunction to photoelectrochemical sensors and it might be easily extended to more exciting sensing systems by photoelectrochemistry.