Nissan Titan Forum banner
1 - 15 of 15 Posts

BWV

· Registered
Joined
·
459 Posts
Discussion starter · #1 · (Edited)
Home Made Auto Headlight Control

Well guys... I posted here that i might build my own auto headlamp controls...

I did this for the pure reasons that, 1) I love to tinker with microcontrollers, 2) I am cheap, and 3)I get bored sitting around on snowy days in Upstate NY. (been looking at snowmobiles...but I don't want to own anything again that i will fix/work on more than I ride...so probably not)

So... I built an auto headlamp controller that with 2 buttons you can set when you want them to turn on, and when you want them off. The device uses a clone of an Arduino Nano microcontroller ($7) a low driver current 4 relay board (2 for 10..so $5) a switching power supply to regulate power to the micro ($4) a photo resistor (5$ for 20...25 cents) a 10K resistor($5 for 100) a couple push-button switches ($5 for 10) and some wire. I built a custom box for it.. but a project box is about $6 on amazon... so about $23 worth of parts and few (too many.. but the goal was to tinker...)hours.

As this truck has a rather complicated light control system... Multiplexed Multifunction switch, body control module, intelligent power distribution module, and a bunch of relays...I decided to hack the control at its source.. the light switch module. Took it apart to see what connected to what... and went from there.
 

Attachments

Discussion starter · #2 · (Edited)
I melted a hole in the switch housing in a nice spot that allowed the case to act as a strain relief.
The 5 wires come from 3 switches... one is independent...on with headlights only... assuming this is one is to enable fogs... the other 2 share a common center, one is on with parking and headlamps, the other is on headlamps only.
So.. that was easy.

Then I wired the 4 relays... one is used to light up the pilot lamp on the switch to indicate proper boot of the device, as well as blink with button presses, the other 3 are independent headlamp switch, parking lamps, headlamps(the last two switches share a common)

The little Arduino checks to see how bright it is 4 times a second, by measuring voltage from a photoresistor/fixed resistor voltage divider system. The photo-resistor or LDR is mounted in a hole drilled in the DS dash speaker grille. It then compares that to a set of numbers stored for on and off values.. and acts accordingly.
It waits 10 seconds to turn the lights off if it gets bright out.. but not before checking again right before it does so.. to avoid streetlight or tunnel type false positives.
 

Attachments

  • Like
Reactions: Mac's Hack Shack
Discussion starter · #3 · (Edited)
It works pretty nice in testing with the garage lights on and off.

I powered the unit from the ignition switch connector (via a fuse of course) so that it only works when the truck is running.
It can easily be disabled by turning off the rocker switch.

Not much more to say about it... When it gets dark... the box fakes the BCM into thinking someone twisted the light switch to on. When it gets light out...it waits to see if its gonna be bright for a while, then turns on.
If you don't like when it comes on... you just wait til its exactly the brightness you want the lights to come on at and push button 1. If you don't like when it turns the lights off.. same deal.. wait til its as bright as you want.. then push button 2.

Now I gotta figure out what to do with that third rocker switch....

Oh.. and I might hook this up to the wipers so if they are on for more than like 20 seconds it turns on the lights... NY is a wipers on/lights on state... just haven't figured out where to grab that signal yet. The controller has like 6 more I/O pins i could use for stuff...
 

Attachments

  • Like
Reactions: Mac's Hack Shack
Discussion starter · #4 · (Edited)
Here is the code I wrote if any of you are micro controller guys...

DISCLAIMER... If you build this device, and it hurts you or anyone else... or costs anyone money because it failed or didn't work safely...or..if your wife gets pissed off because you've been in the garage for a week... it's all on you .. build at your own risk... this is all cheap parts, and code written by a moron... you've been warned.


/*
this sketch is used with a photocell / resistor voltage divider to sense ambient light levels
to control a generic 4 relay board where "low" on the board relay input pin
energizes that relay coil, closing the NO contacts. the photocell is connected between 5v and
an analog pin, the 10k relay is connected from that same analog pin to ground
light levels are read in 1024 levels, then divided by 4 to allow byte size levels(255)
2 momentary swithes are used to set low light on and hi light off values.
light level setting values are written to eeprom each time they are set to survive power off state
if both buttons pressed, code restore default values and writes to eeprom
*/

#include <EEPROM.h> //use eeprom library for storage of levels

int led = 13; //onboard LED pin, used to indicate if lights should be on or off
int relay1pin = 9; //relay 1 used to drive indicator on rocker or dash
int relay2pin = 10; //secondary headlight switch (5/6 from dwg on only with HL) relay 2
int relay3pin = 11; //headlamp sw 1/2 (parking, also on w HL) relay 3
int relay4pin = 12; //headlamp sw 1/3 (headlights) relay 4
int switch1pin1 = 2; //on level setting switch
int switch1pin2 = 4; //on level setting switch - this pin is forced as output to low to be used as a switch "ground"
int switch2pin1 = 5; //on level setting switch
int switch2pin2 = 7; //on level setting switch - this pin is forced as output to low to be used as a switch "ground"
int sw1state = 0; // variable for switch 1 state
int sw2state = 0; // variable for switch 2 state
int onlevel; //threshold level where relays 2,3,4 are turned on (lights on)
int offlevel; //threshold level where relays 2,3,4 are turned off (lights off)
int defaultonlevel = 75; //threshold level where relays 2,3,4 are turned on (lights on)
int defaultofflevel = 175; //threshold level where relays 2,3,4 are turned off (lights off)
int cycletime = 250; // photoresitor sample time in ms
int offdelay = 10000; //delay to wait to resample level and then turn lights off if still above off level in ms
int headlampstate = 0; //state of headlamps 0=lights are off 1=lights are on
int photocellPin = 7; // analog pin that the cell and 10K pulldown are connected to
int photocellReading; // the analog reading from the photocell/resitor voltage divider (light sensor)

void setup(void) { // boot up values
Serial.begin(9600); //for debug
pinMode(switch1pin1, INPUT_PULLUP); //used to pull switch high by default
pinMode(switch2pin1, INPUT_PULLUP); //used to pull switch high by default
pinMode(switch1pin2, OUTPUT); //set pins as outputs
pinMode(switch2pin2, OUTPUT);
pinMode(relay1pin, OUTPUT);
pinMode(relay2pin, OUTPUT);
pinMode(relay3pin, OUTPUT);
pinMode(relay4pin, OUTPUT);
digitalWrite(led, LOW); //turn NANO indicator led off
digitalWrite(switch1pin2, LOW); //set to ground level 0 volts
digitalWrite(switch2pin2, LOW); //set to ground level 0 volts
digitalWrite(relay1pin, LOW); //close relay 1 contact, use for dash indicator
digitalWrite(relay2pin, HIGH); //open relay 2 contact, lights off
digitalWrite(relay3pin, HIGH); //open relay 3 contact, lights off
digitalWrite(relay4pin, HIGH); //open relay 4 contact, lights off
onlevel = EEPROM.read(1); //get threshold level (lights on) from eeprom and set program variable to match
offlevel = EEPROM.read(0); //get threshold level (lights off) from eeprom and set program variable to match
}

void loop(void) { //get to work
photocellReading = analogRead(photocellPin) / 4; //sample light sensor then divide by 4 so values can be stored in single bytes in eeprom
sw1state = digitalRead(switch1pin1); //sample switch position
sw2state = digitalRead(switch2pin1); //sample switch position

// debug info output via the Serial monitor
Serial.print(" Light = "); //For Diagnostics
Serial.print(headlampstate);
Serial.print(" SW1 = ");
Serial.print(sw1state);
Serial.print(" SW2 = ");
Serial.print(sw2state);
Serial.print(" On = ");
Serial.print(onlevel);
Serial.print(" Off = ");
Serial.print(offlevel);
Serial.print(" reading = ");
Serial.println(photocellReading);

delay(cycletime); // wait a little bit each cycle then check switch positions and compare light sensor value to on and off levels
if (sw1state == 0) { //if someone pushed switch one, set on level to current light sensor value
onlevel = photocellReading;
EEPROM.write(1, onlevel); //write current onlevel to eeprom location 1
digitalWrite(relay1pin, HIGH); //blink relay 1 once to acknoledge button press
delay(500);
digitalWrite(relay1pin, LOW);
}
if (sw2state == 0) { // see if someone pushed switch two
if (sw1state == 0) { //see someone also pushed switch one, if so set on and off levels to default, and blink status light 3 times
onlevel = defaultonlevel;
offlevel = defaultofflevel;
EEPROM.write(1, onlevel); //write current onlevel to eeprom location 1
EEPROM.write(0, offlevel); //write current offlevel to eeprom location 0

digitalWrite(relay1pin, HIGH); //blink relay 1 3X to indicate reset to default
delay(500);
digitalWrite(relay1pin, LOW);
delay(500);
digitalWrite(relay1pin, HIGH);
delay(500);
digitalWrite(relay1pin, LOW);
delay(500);
digitalWrite(relay1pin, HIGH);
delay(500);
digitalWrite(relay1pin, LOW);
}
else {
offlevel = photocellReading; // if only sw2 was pressed, set off level to current light level
EEPROM.write(0, offlevel); //write current offlevel to eeprom location 1
digitalWrite(relay1pin, HIGH); //blink relay 1 once to acknoledge button press
delay(500);
digitalWrite(relay1pin, LOW);
}
}
if (photocellReading > offlevel){ //check to see if it is bright nuff to turn or keep lights off
if (headlampstate == 1){ //see if lights are on, if so get ready to shut em off
delay(offdelay); //wait to be sure it wasnt just a momentary bright spot
photocellReading = analogRead(photocellPin) / 4; //read photocell and divide that by 4 again
if (photocellReading > offlevel)headlampstate = 0; //if its still bright enough after waiting.. turn em off
}
}
if (photocellReading < onlevel){ //see if its dark enough to turn lights on
headlampstate = 1; //set variable to say turn em on
}
// logic to actuate relays
if (headlampstate == 0){ // zero means turn them off
digitalWrite(led, LOW); //turn indicator LED on NANA off
digitalWrite(relay2pin, HIGH); //open relay 2 turn off lights
digitalWrite(relay3pin, HIGH); //open relay 3 turn off lights
digitalWrite(relay4pin, HIGH); //open relay 4 turn off lights
}
else {
digitalWrite(led, HIGH); // anything but zero means turn em on... turn on LED on NANO to indicate
digitalWrite(relay2pin, LOW); // close relay 2 to turn on lights
digitalWrite(relay3pin, LOW); // close relay 3 to turn on lights
digitalWrite(relay4pin, LOW); // close relay 4 to turn on lights
}
} //back to the top of the loop
 
  • Like
Reactions: Mac's Hack Shack
And the exit delay timer to shut it off after parking?
 
Discussion starter · #6 ·
And the exit delay timer to shut it off after parking?
I thought about it... my Sequoia had that, and I always thought it a waste of battery power. I park in my garage at night, and even when I used to drive places at night in that vehicle, I'd press lock twice to turn them off almost every time...

That said.. if I want to "light my way" as I walk away from the truck, i can still manually turn on the headlamps and the built in timer will leave em on 45 seconds.

It'd be real easy to add here... just not really sure I want to.

I was thinking' for my next trick I might like the fogs to come on when I unlock the truck...that and the cargo lights coming on as they do would make seeing around the truck pretty nice when approaching in the dark... that might just be a wire from the cargo light and a relay...no "brain" needed
 
  • Like
Reactions: Walrus
Discussion starter · #7 ·
First "real" test today... got up and out early enough for it to still be "dim" out...so they came on... and then they turned off as it became brighter out. I'm happy with it.

When I get some time, I'll do the "lights on with wiper"... I drove in the rain today, and I had to actually reach my hand forward and twist that whole light switch knob all the way to on... a HUGE step backwards!!!
I don't wanna start that s#!t again just because of a little rain...

Kidding aside...
I think I'll take the wiper signal from the output of the wiper relay, as it's energized on every wipe and on hi or low.

I want to avoid headlights coming on for mist, or wash, so I'll have to have it sense multiple pulses or a consistent on level over a period of time. Gonna have to do some figuring for the best logic method... but the thought process has begun.
 
Discussion starter · #9 ·
:) Then I wouldn't have to reach all the way to that other wiper either !!!
 
Discussion starter · #11 ·
This works great, I'm real happy with it.
 
That's really cool man. I like your choice of going through the switch like that. Interfacing with the lighting system in this truck is difficult. When i bought my truck it came with a remote starter already installed. Its setup to turn on the parking lights when its on, but they are dim when running. I'm guessing because the installer didn't fully understand how the lighting system works. I'm gonna do this project when I got some time and I think I'll rewire my remote starter light output into this too. With another relay I could isolate it from the Arduino output, if that's necessary.
 
Discussion starter · #13 ·
Thanks! I have the levels right and the timeouts so overpasses and the like don't trip it... now it's time to get into the wiper part. Need to make it discriminate between rain type wiper use and just cleanin'...guess I could hack the wiper switch same way I did the headlamp switch... not sure yet...trying to keep the OE parts OE
With this CAN and multiplexed switch stuff, it does seem to be easier just to grab signal or switch at the source.
 
It is fun when you have the creativity juices and the right set of skills and knowledge. There is nothing you cannot achieve and you can save money while at it too. All you need are the correct resources and you are on your way to building the masterpiece you need at a fraction of its retail price.
 
  • Like
Reactions: BWV
1 - 15 of 15 Posts