Surface modification of nitrogen and sulfur co-doped carbon quantum dots (N, S-CDs) were performed using cysteine and polyethylenimine as raw materials. The prepared N, S-CDs exhibited excitation-independent in the range of 300–380 nm. Furthermore, mercury(II) ions (Hg2+) can effectively quench the fluorescence intensity of the N, S-CDs. Based on this, we developed a fluorescence sensor with high sensitivity and selectivity to detect Hg2+. Under optimized conditions, the sensor showed good linearity in the range of 0–500 nM, and the limit of detection is 9.2 nM. Further, the sensor showed high sensitivity to Hg2+ in lake water and rice samples. The recovery of the Hg2+ in lake water and rice samples ranged between 98.2 % and 109.5 % with a relative standard deviation below 5.8 %. With outstanding sensitivity and selectivity, the fluorescence sensor provides a promising platform for monitoring Hg2+ in real samples.