Having problem with reed switches, relay board, and model train.

I could sure use some help here! I've been beating my head against the wall trying to figure out this problem. I have an Arduino Uno R3 hooked up to a Sainsmart 4-relay board. The relays switch 6V DC out to a model train track that's broken into 3 sections (stations). There are 3 reed switches spread out on the train track that are then hooked up to the Arduino inputs. These correspond to the 3 sections of train track. The relays are all energized at start-up, thus allowing the model train to run. My code waits for a reed switch to close, then turns off the associated relay, removing voltage from that part of the train track causing the train to stop at the station.

So now the problem. When there are no trains on the track, everything works fine. I can manually trigger each reed switch and the Arduino sees the switch closure and triggers the associated relay. However, as soon as I put a train on the track, the Arduino sees phantom random reed switch closures. I've checked the reed switches, and they aren't actually closing even though the Arduino thinks they are.

I have the reed switches hooked up to the Arduino as if they were push-buttons (using a resistor). I originally had them hooked up without the resistor using the Arduino's internal resistors, but I was having the same problem as described above, so I hooked them up as switches to see if that would help, and that's where they are now. See attached picture "switch.jpg" for how the reed switches are hooked up. I've also attached the source code.

I'll be really grateful for any help. If anything, you can have some free train controller software :-).

-tom

switch.jpg

TrainController.ino (6.24 KB)

That looks correct, however, I suspect what is probably happening is the wires to those switches run near other wires with current in them and you are picking up noise giving false triggering (what doesn't show in your drawing is how far the switch is away from the Arduino etc.). You can use a series resistor (say 5k) to the input of the Arduino and then have a .1ufd (experiment) capacitor from the input pin to ground. That effectively creates a low pass filter that takes off some of the spikes assuming that is what is getting you. Something else that can also work fairly well is when you get a "closure", measure it 3 or 4 times in a row in a loop - if all 4 times show closed, then it is probably valid and not a noise pulse. The down side of the lp filter is that it will delay the response time a bit. You may need to experiment to find the values that work best. Typically, when you have any kind of sensor remote from the processor you need to consider some sort of signal conditioning on the input.

A 100n capacitor from Arduino pin to ground could fix this.
Leo..

Thanks for the help guys! I put the reed switch reading code into a loop of 4 passes and only considered the switch closed if it was closed 4 times in a row. That fixed it! I'm happy.

Beware the electrical "noise" flying around due to the simple little DC motors in the trains. I played with a train set controlled by micro-electonics ages ago.... and never completely beat this problem. Ideas welcome!!

Also, a basic thing, but you DO have "the diode" on the relay coils, don't you?....