/AutoHeadlight

A tiny Arduino project to provide an automatic headlight feature

Primary LanguageC++GNU Lesser General Public License v3.0LGPL-3.0

AutoHeadlight

A tiny Arduino project to provide an automatic headlight feature

This circuit/code will allow you to add an auto headlight feature to a car that does not have one. Standard disclaimers apply: I am in no way responsible if you try to do this and screw up your car. There are many ways to do this, but as a coder, I decided to use a programmable bit at the heart of it. The circuit turns on the headlights whenever the ambient light level falls below a programmed level. I had a second goal, which is to indepentdently control the dimming of my navigation screen, so I added a second output to control the nav illumination. If you don't need this feature, just don't wire it up. This write up is not extremely detailed, but I hope it provides enough info if you want to attempt it yourself. If something is not clear, ask and I will try and respond.

The Hardware

I based the design on the Adafruit Trinket (available for about $5). I purchased most of the electronic parts from Adafruit, but you can also get them from Digikey/Mouser/etc. The Trinket samples the value of the light dependent resistor (LDR) and outputs out a 1 (5V) or a 0 (0V) based on the current light level. The trinket’s low current outputs are used to drive transistors, which in turn can drive enough current to switch on a 12V relay. Separate outputs are used for the headlight function and nav illumination function so they can be switched at different light levels. The LDR is part of a voltage divider on input 2 of the trinket. As the light level goes up, the resistance of the LDR goes down, causing the voltage level on pin 2 to go up. The LDR for this circuit is attached to a longer wire pair (about 3ft), so the LDR can be located on the dash while the rest of the circuit is installed near the fuse box. I placed the LDR near the center of the dash. I included a Molex connector on the wires going into my circuit box so I could easily unplug and remove it from the vehicle when I wanted to adjust the program. I used an accessory switch connected to a Key-ON fused circuit to power the auto headlight circut. On my car, the headlight switch connects the circuit to ground to turn on the headlights. You will notice in the circuit diagram that the relay that turns on my headlight does the same. If you simple connect the relay's switch contacts in parallel with your headlight switch, the circuit should be able to turn on your headlights.

If you don't need to control a second illumination source (like a nav display), you may omit these components from the circuit: Q1, the 2.2k resistor connected to Q1, the diode and relay connected to Q1, the 470 ohm resistor and the blue LED.

The Software

If you have not used the Arduino development environment before, check out the Getting Started tutorials on the Adafruit site (https://learn.adafruit.com/introducing-trinket/guided-tour). The programming environment uses “sketches” (mine is below) to program the device. The code is broken into two main parts: setup and loop. The instructions in setup are run one time when the trinket is powered up. The instructions in the loop run repeatedly until power is removed. In my setup function, I configure the pins as inputs and outputs and set the initial state of the output pins. In my loop instructions, I read the value of the LDR voltage and decide to turn the outputs on or off. The loop code does two other things that are easy to do in code, but would be a bit harder in a dedicated circuit: hysteresis and low-pass filtering.

Hysteresis simply means that two different levels are used for the turn-on and turn-off decision. This prevents the circuit from getting into a situation of rapidly turning the lights on and off when the light level is near a single threshold.

The software also applies a low-pass filter for the LDR light level so rapid changes in the light level do not cause rapid changes in the output state. An example of this would be driving next to a series of trees when the sun is low on the horizon. The sunlight on your dash can “strobe” as you pass through the tree shadows. The exponential low pass filter in my code keeps the code size small because it does not require the trinket to use a floating point library. If you filter too much (using a smaller alpha value) the circuit will be slow to react to changing light conditions. If you filter too little, the circuit will overreact to small fluctuations of the light level. The values in my code seem to work well and similar to other auto headlights I have observed.

In my code, the loop runs about 4 times a second. If you change this, you may need to adjust the filter parameters to compensate. If you decide that you don’t like the levels where the headlights or Nav illumination switch on, simply modify the LIGHTS_ON_VAL or NAV_ILL_ON_VAL constants near the top of the code. You can reprogram your trinket as often as you want. There are unused i/o pins on the trinket, so you could expand this circuit to add other features.