Arduino running a sketch is making a sketch run on another Arduino nearby


I use Arduino's on my toy train layouts. I have one which we will call accessory Arduino. It's purpose is to run several accessories on the layout. The accessories are actuated by relays. The other Arduino we will call Crossing signal. It's purpose is when a train goes over the IR sensor it actuates a relay which blinks the crossing lights and turns on an mp3 player with a bell sound.

Here is my problem. The Crossing signal Arduino is being actuated (that is running it's sketch) when I am running the sketch for the Accessory Arduino. The accessory Arduino is activating AC loads i.e., motors, solenoids. When it does it makes the crossing signal flash and ring it's bell.

There is no connection between either Arduino. They are both on separate power supplies. If on a common supply it happens all the time on separate supplies less frequent. The loads are on a common power supply. I tried having each Arduino's loads on separate power supplies and it makes no difference.

I've tried moving the Arduino's apart with mixed results. The relays have flyback diodes. To prevent transient spikes.

I am st a real loss as to what to look for or do next. Any suggestions or assistance is appreciated.

Power fluctuations are somehow resetting or triggering the Arduino(s).

It seems very likely that the common load power supply is not adequate, or you have ground loops. The cure is (1) to use adequate and adequately filtered power supplies, (2) star grounds and (3) circuit isolation.

In severe cases people use optocouplers for communications, so that there is no common ground or other electrical coupling.

Avoid using separate branches of the AC power line for different parts of the circuit as that can introduce additional noise and other ground loops.

The common load power supply is a 300 watt transformer. The circuit is +18V to the common on the relays. No separate branches ... one lead from transformer to relay common(s). The NO relay contact to the accessory, i.e., motor driven whistle, solenoid actuated water tower, etc. the return from the accessory goes back to the -18 V (or as it is called Base) to complete the circuit.

So I think I have eliminated ground loops. The returns are for the most part separate leads going back to the Base. So in effect a star arrangement.

Post a schematic in CAD or a picture of a hand drawn circuit in jpg or png- ( and, please NOT a pretty Fritzing drawing)

How are you powering the Arduinos?

One Arduino is powered with a repurposed atx PC power supply. The other a walwart. Let me do some drawing tonight and post a jpg.

Here is the schematic. When sketch for Arduino on left runs it causes the one on the right to run. For example an accessory on left is running and lights on the crossing will flash a few time. Two separate systems. Nothing connected to each other.

I only throw this out there because I don't see it in the schematic. I don't see any diodes across the relay coils. Is it possible that when a relay opens that is creating a voltage spike that is creating a transient signal in the other system inadvertently?

These are the standard Arduino relay modules off Amazon or Ebay they have the diodes. Schematic is to show workings of circuit

Relays used >>

Right on. I got nothing then. Sorry.

accessory crossing schematic.png

Two problems here.

One is that your diagram implies you have relays directly connected to the Arduino. That is simply not workable. :astonished: If as you say, you are using relay modules, please illustrate these so we can determine possible problems.

The+ second is that you suggest you are running the Arduinos on 9 V. That is also not workable. :astonished: The Arduino requires regulated 5 V power. :roll_eyes:

This is one of the rare cases where it would probably be useful to see a photo of the Arduinos in place so we can see how the wiring is arranged. Wires (and track) can act as antennas.

Have you inputs without pullup or pulldown resistors? Floating inputs are very susceptible to interference.

Simple Image Posting Guide

This is one of the rare cases where it would probably be useful to see a photo of the Arduinos in place so we can see how the wiring is arranged.

If - and only if - it is adequately illuminated, which may be difficult inside a building.

Are the gnds of BOTH Arduinos connected together?
How long are the power wires?
Have you got bypass capacitors?

Your circuit shows no input devices to the controllers, are there any and can you include them please?

Is there a reason you are using two controllers, where one would easily do the job?

Tom… :slight_smile:

A few comments on the relay modules, which will apply to most of the common opto-isolated relay boards:

Remove the jumper on the 3-pin header that connects VCC and JD-VCC (or similiar designation)
Provide 5v to the relay board at the 3-pin header by connecting the power supply to JD-VCC and GND - DO NOT use the 5v and ground from the arduino.
Connect from the arduino 5V pin to the VCC of the 10-pin header (the header with the relay inputs).
Connect from the arduino data pins to the relay input pins.
DO NOT connect a ground from the arduino to the relay board. The arduino is driving LEDs inside the opto-couplers, no ground is needed for this, and if you do connect a ground it creates a common ground point between the arduino and the power supply for the relays.

It is preferable to use separate power supplies for the arduino and the relay board.

The arduino is receiving 9 v via the barrel jack. I know that relays connected to an arduino will not work .This was a simple block schematic diagram.

Using two controllers as one controls accessories. The other is for a crossing signal Two different tasks . The accessory arduino runs a script that activates various accessories i.,e whistle, coal loader, as a script. The other arduino has IR sensors that detect a train and then flash teh lights and ring a bell. Two different functions.

Power wires are about 3-4 feet long.

Arduinos are not grounded together.

Arduino / Relay wiring : arduino 5v is connected to relay vcc . arduino data pins are connected to relay input pins. Using power from arduino to power relays so GND is connected arduino to relay.

Inputs have pull up resistors

I think I have isolated and potentially solved the problem.
The arduino that runs the accessories also allows for the accessories to be run manually with their own push buttons. If I let the arduino run the accessories there is no problem. However if I use the manual push button it causes the problem i.e., crossing signal lights flash and bell rings. So I think the problem is caused by a voltage transient caused by arcing of the contacts on the button. I've tested numerous times with not problems it I let the arduino run the accessories but I get the problem if I use the manual push button.
So I think the answer is a diode or TVS across the push button contacts to eliminate the arcing.

A capacitor across the contacts would be worth a try. Then rewrite the sketch for one Arduino. Doing lots of different things at the same time is a "walk in the park" for an Arduino. Even if you run out of pins, you just use a single bigger one, rather than two. It will also make your life simpler when writing routines etc.

Thanks AJLElectronics
The sketchs work fine so it's not a programming issue. The problem as best I can isolate is the one button causing an arc and wires being too close to each other. So I will try an RC snubber across the offending button and moving arduino, wiring , etc away from wiring of the other arduino that is running the accessories.

What value of pullup do you have on the swiches? May be worth keeping the impedance down on the inputs.

If you have long wire runs it may help to implement them as twisted pairs with signal and GND.

Under no circumstances twist two signal wires together - it will not work (I learned that the hard way :slight_smile: )


Arduinos are not grounded together.

Have you tried connecting both controller gnds together?
Tom... :slight_smile: