This invention is an automatic controller for switching on and off the headlamps of a vehicle according to the ambient lighting conditions. With just one optical sensor and a controller, the present invention when connected to the relay of the headlamps controls the headlamps accurately and intelligently under all conditions. The controller incorporates a filter (R3C2) and a voltage divider (VR1, VR2, R4) which in turn are connected to a comparator (IC1). The filter (R3C2) removes the alternating component of the output from the optical sensor caused by artificial lighting, while the voltage divider (VR1, VR2, R4) sets a preset threshold intelligently for the activation and deactivation of the headlamps in response to a variety of ambient conditions. The invention also switches off the car lamps when the engine is turned off. This will increase the life of the battery without the drivers having to remember to switch off and on the headlamps.