Wire up for relays using ground as switch trigger

I wired up the Nani using pins 12 and 13 to the relay board. I then wired pin 3 as ground to the switch and back to ground -5v. When I depress the switch the ground is shorted and on the loop function I cycle the relays. This works fine and dandy. The problem is the relays will cycle sporadically without the switch being pressed.

I used a better power supply the second time around but that still didn't help. It did however seemingly get rid of the overheating problems, so far.

For a production application like mine where the relays power the headlamps of a car this in unacceptable as the lights may come on on their own while the vehicle is in use.

Any ideas what is causing this interference? My wiring is solid, no shorts. Any responses are much appreciated.

Thanks

dhtmldude:
I wired up the Nani using pins 12 and 13 to the relay board. I then wired pin 3 as ground to the switch and back to ground -5v. When I depress the switch the ground is shorted and on the loop function I cycle the relays. This works fine and dandy. The problem is the relays will cycle sporadically without the switch being pressed.

I used a better power supply the second time around but that still didn't help. It did however seemingly get rid of the overheating problems, so far.

For a production application like mine where the relays power the headlamps of a car this in unacceptable as the lights may come on on their own while the vehicle is in use.

Any ideas what is causing this interference? My wiring is solid, no shorts. Any responses are much appreciated.

Thanks

You asked, "Any ideas what is causing this interference?"
Yes, the problem is either in your hardware or in your software or both.

You didn't include a schematic or your code so I can only guess.

Your description of your switch connections sounds suspect or maybe you need tu use INPUT_PULLUP.
it sounds like your pin is floating when not activated.

Here is a good tutorial on how it should be connected and used.
Gammon switch tutorial

Hutkikz:
You didn't include a schematic or your code so I can only guess.

Your description of your switch connections sounds suspect or maybe you need tu use INPUT_PULLUP.
it sounds like your pin is floating when not activated.

Here is a good tutorial on how it should be connected and used.
Gammon switch tutorial

This is what's happening:
The internal pull-up is around 50K and thus does not draw much power. It is very useful because it saves a part (the resistor) and some wiring. However over long cable runs the pull-up may be too weak to counter noise being picked up on the wire.

Thank you for that link. I followed the literature to where it said that over long runs there may be ghost signals, so I soldered a 10k resistor to +5v and the other end to the pin. The pin and ground go to the switch. This should avoid the "floating pin" phenomena I was experiencing due to the long run in the ground and pin to the switch which the internal pull up isn't capable of handling.

Thanks again.

A 10k ohm pullupon the pin isn't doing the trick. The pin still becomes ground on its own. How low can I go with the resistor?

5v/20mA=250ohms

I probably would go no lower than say 330 ohms to give a little safety margin.
Likely 4k7 or 1k would work.
keep it as high as possible to save power and stress on the pin.

Hutkikz:
5v/20mA=250ohms

I probably would go no lower than say 330 ohms to give a little safety margin.
Likely 4k7 or 1k would work.
keep it as high as possible to save power and stress on the pin.

Thank you. So the purpose of the resistor is to gently pull the state of the pin towards the positive side, because otherwise the pin would be neither negative or positive and cause it to float. The resistor is capable of transferring a certain amount of current to the pin to do this. The various grades of resistors transfer various grades of current, from .5mAh to XmAh all proportionate to the resistors Ohm rating. Yes?

Also I understand a 1k resistor, 1kilo Ohm, but what is a 4k7. Is that 4.7KiloOhm?

I'm probably learning this all wrong :stuck_out_tongue:

You got it.

Less resistance==more current/stronger pull

Stronger pull ==more noise resistant but higher power usage.

Yes on the 4k7 resistor.

Hutkikz:
You got it.

Less resistance==more current/stronger pull

Stronger pull ==more noise resistant but higher power usage.

Yes on the 4k7 resistor.

Thank you! I want to post a video, how do i do that?

A 1k resistor isnt helping the problem either. the lights are still going off on their own. The resistor is wired from the pin to the +5. The pin is also wired to the switch which is wired to ground (-5v). and the lights are still going off. The program below:

is there any other way to trigger the program that to constantly monitor the state of the button? Isnt there something like a OnPinHigh(pinNumber) or something similar like in the javascript event model? Or perhaps a way like wiring the pin to +5 when its off and -5 when its on? I would need a three prong switch. Im just tired of fixing a water leak with duct tape instead of re-sealing the joint with abs pipe sealant.

int buttonState = 0;
int buttonPin = 3;
int lightDelay = 90;

// the setup function runs once when you press reset or power the board
void setup() {
  pinMode(12, OUTPUT);
  pinMode(13, OUTPUT);
  
  Serial.begin(9600);
      
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT_PULLUP); 
   
  //initialize the buttonPin as output
  digitalWrite(buttonPin, HIGH);   
}

// the loop function runs over and over again forever
void loop() {

  buttonState = digitalRead(buttonPin);

  if (buttonState == HIGH) {
    digitalWrite(13, LOW);
    digitalWrite(12, LOW);                                                                                                                                            
  } else {
    for (int i=0; i<8; i++){
      digitalWrite(13, HIGH);
      delay(lightDelay);
      digitalWrite(13, LOW);
      delay(lightDelay);
      digitalWrite(12, HIGH);
      delay(lightDelay);
      digitalWrite(12, LOW);
      delay(lightDelay);
    }
  }
}

ok well let's do a test and see what's really going on.

Put this right after the digitalRead() like this :

buttonState = digitalRead(buttonPin);
Serial.println (buttonState);

then run the sketch and open serial monitor .

it should print a 1 when not pressing the button and a 0 when pressed.

if it still has noise issues it will change on it's own without being pressed.

how far away is this button?

Edit: +5v to -5v == 10v == fried arduino

Hutkikz:
ok well let's do a test and see what's really going on.

Put this right after the digitalRead() like this :

buttonState = digitalRead(buttonPin);

Serial.println (buttonState);




then run the sketch and open serial monitor .

it should print a 1 when not pressing the button and a 0 when pressed.

if it still has noise issues it will change on it's own without being pressed.

how far away is this button?

Edit: +5v to -5v == 10v == fried arduino

ok. ran the test in the serial monitor. all 1's show up because i dont know when its going to happen. the switch is about 6 ft away from the board when it occurs (its in the car) but i did see it go off on its own once when it was just sitting there before i soldered the 1k on it.

I forgot that we were dealing with a car. that is a noisy environment indeed.

So let's try some other noise suppression techniques.

First a 6' run is a big antenna, if you twist the switch wires together creating a "twisted pair" it can greatly reduce the noise picked up.

Next a decoupling capacitor can also help reduce noise and gives the added benefit of debouncing

I think a 0.1uF cap connected between the pin and ground should be good.

Since I have an upcoming project for my car that could have an even longer run I am now even more thoroughly engaged in helping solve your problem

EDIT: How are you powering the arduino?

Hutkikz:
I forgot that we were dealing with a car. that is a noisy environment indeed.

So let's try some other noise suppression techniques.

First a 6' run is a big antenna, if you twist the switch wires together creating a "twisted pair" it can greatly reduce the noise picked up.

Next a decoupling capacitor can also help reduce noise and gives the added benefit of debouncing

I think a 0.1uF cap connected between the pin and ground should be good.

Since I have an upcoming project for my car that could have an even longer run I am now even more thoroughly engaged in helping solve your problem

EDIT: How are you powering the arduino?

Sounds good. I bought a power transformer unit on amazon. Its a monoblock aluminum heatsink with the components sealed inside a recess on the back. It takes 12v and pumps out 5v. I measured this using a multimeter. Its non adjustable. THe one i was using before, the adjustable kind on a pcb, overheated and melted the hot glue with which the nano was attached to the case. I have since removed the power unit from the case with this new unit and I am fastening the nano using double sided tape.

The cables (2 of them) are already ran to the cab, i think i twisted some part of the length. THe grommet through which they go have other wires but the majority of the run is under a fender and in through the engine bay, away from interference. somehow i dont think this is interference.

The debouncing cap is worth a try. currently i have the rig on a table inside with a breadboard connected to it where a led sits awaiting the program to run on its own. i modified the program so when it runs regardless of whether the contacts are together or this phenomena occours the led will come and stay on. so far nothing which is why i brought the car element up.

this has to be proven not to go off on its own. its been twice now that i take it out and see the tail lights in the car in front blink and be forced to pull over to remove the fuse. sucks.

I was afraid that the twisted pair would be problematic. I believe it would of been the most effective solution.

The alternator and ignition system(40Kv pulse's) are the primary sources of EMI, both located in the engine bay. So I think your mistaken about being away from interference.

If I understand correctly, the nano+relays are located at the front of the car with the switch in the cabin?

If so and we can't manage reduce interference on the switch wires to acceptable levels. it may be necessary to move the nano and relays inside the cabin and use longer 12v wiring which is much less susceptible to interference.

I wrote a ten paragraph post that got swallowed when i hit post, here is take 2.

Once create a twisted pair out of the wires running into the cabin and hook up a test led that comes on and stays on when the circuit is triggered, and run the car in the garage for a couple of hours with the door open to prove that it solves or doesnt solve the problem we'll know if its the emi in the engine bay which would be good cause i could start using the lights right then and there. Otherwise plan B. It consists of the following:

  • wiring a wifi transciever to the nano
  • installing a pi with a wifi dongle in the car

What it would do is instead of having hardware buttons running from the nano to the cab, the buttons would go on the pi in the car. The pie would send an http request to nano where the nano would run low level code to fetch data from the http request such as the method (eg. ?method=runlights) and parameters (&duration=1sec). The loop on the nano that caught the request would call a sub that runs the lights (eg. void runLights(int duration). The sub would run the lights, nothing new here, however when the runLights program is done it would call a sub that could echo a call back to the pi's running web server through low level code again and the pi would receive such request knowing that the circuit is either on or off.

--- PI -> 192.168.0.2/handler.jsp?method=runlights&duration=1sec -> NANO -> runLights() ->
--- runLights() -> 192.168.0.1/handler.jsp?status=1&method=runlights
--- runLights() -> trigger relays
--- runLights() -> 192.168.0.1/handler.jsp?status=0&method=runlights
--- PI -> toggle status whether be it via LCD screen web app or 4 line display LCD

No more wires from the bay to the cab. Problem solved.

Hopefully the twisted pair and the decoupling cap will take care of it.

I have no experience with wifi yet, but when I first joined here I posted a question asking
about using wireless between 2 arduino's in a car. All the answers I got were that the metal structure
of the vehicle + the EMI from the engine would cause to much interference to be reliable.

I had intended on trying for myself anyway but that project got shelved for the time being.
so if you do try it I would like to hear the results.

Hutkikz:
I forgot that we were dealing with a car. that is a noisy environment indeed.

So let's try some other noise suppression techniques.

First a 6' run is a big antenna, if you twist the switch wires together creating a "twisted pair" it can greatly reduce the noise picked up.

Next a decoupling capacitor can also help reduce noise and gives the added benefit of debouncing

I think a 0.1uF cap connected between the pin and ground should be good.

Since I have an upcoming project for my car that could have an even longer run I am now even more thoroughly engaged in helping solve your problem

EDIT: How are you powering the arduino?

The twisted pair is done. I only have a 1uF and 2uF and 10uF and some 102, 104 ceramic disc types and some film types from an amplified i gutted. Would the 1uF work?

Also here is a video of it working... https://youtu.be/Drmru_Ullt4

I put the 1uF capacitor from pin to ground, added a led and code that turns it on when the ground is closed (switch) and installed it and now have the car running. If this doesnt fix it I dont know what will.