So I'm working on my car and now I've got my sights on the electrical system. I've installed a keyless entry system and a pulse timer for the dome light. Now, I'd like to give the dome light the fade effect all the modern cars have. After looking around, it seems an Arduino would be the most reliable.

I currently own an board similar to the Arduino Nano, specifically the SainSmart Nano. So I've determined that I'd power the board with a constant 12V, use pin 7 as input, and use pin 9 as output.

My code:

int domeLightIn = 7;
int domeLightOut = 9;
int brightness = 0;
int fadeAmount = 5;

void setup() {
  pinMode(domeLightOut, OUTPUT);
  pinMode(domeLightIn, INPUT);


void loop() {
  // Case 1: State of Door: Open/Closed
  // Case 2: State of Switch: On/Off
  // Case 3: State of KES: Active/Inactive

  //Case 1: If door is closed and led is off, brightness remains 0;
  //Case 2: If switch is off and led is off, brightness remains 0;
  //Case 3: If KES is inactive and led is off, brightness remains 0;
  if (digitalRead(domeLightIn) == LOW && brightness != 255){
    brightness = 0;

  //Case 1: If door is open and led is off, brightness will fade in;
  //Case 2: If switch is on and led is off, brightness will fade in;
  //Case 3: If KES is active and led is off, brightness will fade in;
  if (digitalRead(domeLightIn) == HIGH && brightness != 255) {
    for (brightness = 0; brightness < 255; brightness = brightness + fadeAmount) {
      analogWrite(domeLightOut, brightness);

  //Case 1: If door is closed and led is on, brightness will fade out;
  //Case 2: If switch is off and led is on, brightness will fade out;
  //Case 3: If KES is inactive and led is on, brightness will fade out;
  if (digitalRead(domeLightIn) == LOW && brightness == 255) {
    for (brightness = 255; brightness > 0; brightness = brightness - fadeAmount) {
      analogWrite(domeLightOut, brightness);
      delay (35);

I am unsure whether I am using digitalRead() correctly along with wiring the input. So, am I doing this right? or perhaps there is a better way?

  • 1
    Any particular reason why you don't want to use a capacitor and resistor instead? Oct 14 '14 at 20:01
  • Flexibility. I didn't mention this, but I'm also installing footwell lights, perhaps even more accessories. Eventually I'll try different things with it. I'd like to give each a fade effect, but if I can't do it with the arduino, then I'll fall back to capacitors and resistors.
    – hanipman
    Oct 14 '14 at 20:19
  • 1
    Did you've added a PULLUP resistor on the input?
    – Handoko
    Nov 14 '14 at 0:45

You're not using digitalWrite properly. You should do:

if (digitalRead(domeLightIn) == LOW && brightness != 255){

Just a sidequestion. What is pin7 connected to? If it's a 12v signal, you'd have to convert it down to 5v somehow.

Also, the 12v in the car can be up-to 14v, and can have some huge spikes. Not sure if the sainsmart's regulator can handle those spikes. It might die prematurely. It's also just wasting 60% (generating heat!), turning 12v to 5v. I would suggest adding one of those cheap car-phone-chargers.

  • Thanks. And Pin 7 is supposed to be the input signal wire. Messed that up in my code. And I was thinking more along the lines of wiring up a voltage divider to change the input voltage to somewhere between 7V and 12V
    – hanipman
    Oct 14 '14 at 20:25
  • A voltage divider means wasting power twice. Oct 14 '14 at 20:28
  • If I want to power the board, wasting power is inevitable. My car's accessory wire isn't always on. The only wires that I believe are constantly hot are the 12V constants, dash and headlights, and dome light.
    – hanipman
    Oct 14 '14 at 20:52
  • A voltage divider will work. It would only wast a few milli amps, if you use high enough resistors, so not an issue. Not sure what you meant by "between 7V and 12V". Maximum input voltage is 5.5V. So try to get a voltage between 3v (minimal) and 5v, when providing a signal voltage between 12v and 14v.
    – Gerben
    Oct 15 '14 at 15:22
  • 1
    Ah my bad. The 7V to 12V was meant for the wire powering the board.
    – hanipman
    Oct 16 '14 at 18:31

You should consider bringing the voltage down to 5v thats very important, when a car is running as stated above the voltage will increase because the battery is no longer running the electronics, the alternator powers everything and charges the battery.

Instead of trying to control this strait from the Arduino, use a transistor such as the TIP 120 darlington in order to control the existing light or you can put in a new one if you want. This gives you the ability to add more powerful lights or add more lights to the circuit, which the Arduino would not be able to power via pwm.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.