In this paper, a simple, cost-effective, and efficient electrochemical sensor for molecular imprinting melatonin was established. The molecular imprinted films were formed by in situ electrochemical polymerization using molecular imprinting technology. The modification method, modification time and other parameters of the electrode were optimized. Under optimized conditions, the sensor responds to melatonin concentration in a linear range of 0–100 μM. The detection limit was 0.171 μM. In addition, the sensor has little response to interfering substances, such as uric acid, vitamin B6, vitamin C, and glucose, and can be tested in real samples. The recoveries were 98.73–101.60%.