I'm looking to connect two Arduinos, one Mega and one Nano. The idea is that the Mega sends data to the Nano, which then activates various automation on my model railroad. I've decided to use serial, as I have a library which can push a data struct over it. I've used this in previous wireless communication projects.
This time, however, the serial connection between the two Arduinos is wired, instead of wireless. I've done a bit of googling and reading, and I believe that in order to get this serial connection to work, I need to do the following:
Connect a GND pin from the Nano to a GND pin on the Mega
Connect the Nano's RX port to the TX of one of the hardware ports on the Mega
Connect the Nano's TX port to the RX of the same hardware port on the Mega
Code it up
However, I do have one question that I haven't found a satisfactory answer to. Both Arduinos have separate power supplies. The Nano is being powered by a 'wall wart' at 9v, and the Mega is from a repurposed laptop power supply which is run through a voltage regulator to get it down to 5v. (A second regulator provides 15v for model train track power off the laptop supply.) As such, will connecting a common ground between these two Arduinos cause problems?
As such, will connecting a common ground between these two Arduinos cause problems?
No, it will make it work.
This is a common problem for people new to electronics. Circuits are called 'circuits' because the electrons have to have a complete circuit back to where they came from. The electrons from the Mega to the Nano through the serial connection have to get back to the Mega, and the same the other way round with the Nano. If they can't then it won't work. Connecting the ground of one to the ground of the other allows the electrons to get back.
The obvious question is what else is the Mega doing and could you use it to do the automation of your model railroad instead of passing that task to the Uno ?
The common GND makes the circuits like talk the same language, often TTL type, 3.3V or 5.0V. Try to skip the GND and it will be like speaking English and to a Chines, and the opposite.
If You use very long cables, on a modell railroad, some more effort might be needed.
The Mega is already acting as the base station for my wireless DCC system, and running automatic dispatching for operating sessions. The Nano is already set up as an animation and automation controller, it's just that this time, it needs to know which locomotive is running.
Which locomotive that is running must be known by dispatching controller.
I have some experince in running a large modell railroad but I use "iTrain" made by Berrous in the Netherlands.
So, just come on...
The Mega is the base station for a handheld DCC system that I built myself, a few years ago (which talks to a Micro in the controller via an XBee). It does know what loco is running. Thing is, it's already doing enough.
So I'm planning to have it push the data about which loco is running to the animation controller, which will handle layout animation and automation based on that.
One other question. Would connecting the ground of the mega to the ground of the power supply to the nano also provide a common ground for any other Arduinos on the same power supply as the nano?
I mean, if I connect the ground of the Mega as close to the wall wart powering the Nano as possible, rather than directly to the Nano itself, then that should provide a common ground between the Mega and any additional Arduinos comingcoming off the wall wart. Is that correct?
I ask as the supply to the Nano goes through a voltage regulator circuit.
There is a difference that should be considered...
Signal ground vs electrical ground
If your reference is on the wrong side of any isolation e.g a power supply, that’s the wrong ground.
The ground pins on the Arduino can safely be regarded as signal ground and should be as closely wired to match the associated ‘signals’
Doing otherwise can introduce voltage differences and noise... neither of whivh are desirable.
I started a couple of BEGINNERS: threads in the Introductory Tutorials section that approaches this subject. It’s pretty good, but some contributors have muddied the facts a little, which makes it harder for learners,
"I ask as the supply to the Nano goes through a voltage regulator circuit."
The ground is probably common down stream of the wall wart, which probably has a transformer. You can check the ground in different parts of the circuit with a multimeter.
I mean, if I connect the ground of the Mega as close to the wall wart powering the Nano as possible, rather than directly to the Nano itself, then that should provide a common ground between the Mega and any additional Arduinos coming off the wall wart. Is that correct?
Yes, should be fine.
In one sense it does not matter where you connect them, if they are connected somewhere then they are connected. While that is most likely to be OK in this situation it is not always the case that they can be connected anywhere because you have to consider that there is current flowing in the various ground wires, wires have resistance which gives voltage drop according to Ohms Law, meaning the voltage on a long ground wire won't be the same at both ends. The simple answer is a star connection, which means a single point at which all the grounds meet. Don't pull your hair out over this, just keep the cables short and connect everything to everything else.
Thank-you everyone for the answers. After considering all the replies, I've realised that there's a different option which would be a bit simpler in implementation. My reason for asking about the common ground was because I have a third Arduino, which is the animation controller for the other side of the layout. In terms of wire run, this would be approximately 4.5m (15 feet) away from the source of the serial signal.
I read that anything over about 3m (10 feet) isn't good for the RX/TX serial signal. However, there is already an existing communications channel which both animation Arduinos can already use; the DCC track bus. I've found a circuit that allows the Arduinos to listen to the DCC signal and interpret it. It's listed here: DCC Sniffer – Packet Analyser with Arduino | rudysmodelrailway. In the comments at the bottom of the page, there are two modifications that I found I had to make to get the circuit to work. The first is to use a 2.2K resistor in place of the 1K on the schematic (https://rudysmodelrailway.files.wordpress.com/2014/07/schematic1.png). The second is that line 233 in the example code needs to be adjusted. Once I did this, the example code worked on both my programming/test track and my layout. So now I just need to adapt the code to trigger the automation.