Arduino Yun (Master) RS-485 communication with 2 Arduinos UNO (Slaves)

Hi!

I'm doing my master and I need to set up a network of 3 Arduinos:

  • Arduino Yun as master board
  • 2 Arduinos UNO as slaves

Slaves have to be placed 2-3 meters away from master and send readings from sensors. The reason to use Yun board is because I need continuous Internet connection, computing power to pre-process data and on-board storage.

The biggest issue is the length of the cables to connect master and slaves: 2-3 meters is to long for I2C.
I found out that RS-485 can be used for this purpose. But what I've also found is that it's pretty hard to have a proper RS485 communication with Arduino Yun.

I have 3 questions:

  • How do I set up RS485 for Arduino Yun?
  • How do I connect 2 UNOs to single Yun?
  • Is it even possible?

Thank you for any help! :slight_smile:

mityakoval:
I've also found is that it's pretty hard to have a proper RS485 communication with Arduino Yun.

Why? Can't you use a software serial with the Yun?

RS485 is pretty easy. You just need the right kind of driver chips.

mityakoval:
How do I set up RS485 for Arduino Yun?

There usually diagrams in the RS485 chip datasheets. If you can't find a diagram let us know. Or if you want to have us double check a diagram is correct, post a link to the setup you're using.

mityakoval:
How do I connect 2 UNOs to single Yun?

With RS485 driver chips and wire. RS485 uses a balanced pair of wires. The wires should be a twisted pair so both wires are subjected to the same noise. This noise can then be cancelled out by the drive chips.

Sometimes you need a resistor connecting the ends of the twisted pair.

Read up on RS485 on Wikipedia. It explains things pretty well.

mityakoval:
Is it even possible?

I can't think of why it wouldn't be.

Seems strange that it did not occur to you to ask a Yun question in the Yun Forum.

It is actually possible to free up Serial1 on the Yun with a little bit of contortion.

...R

Thread moved.

mityakoval:
I need to set up a network of 3 Arduinos:

  • Arduino Yun as master board
  • 2 Arduinos UNO as slaves

Do they all have to be connected by the same wire, using one port on the Yun? Or can you use two ports on the Yun, each one connected directly to one Uno?

I am currently doing something very similar to this, I've just sent out the board design and waiting for the printed circuit boards to come back. I use two SoftwareSerial ports on the Yun, using pins 8&9 for one port, and 10&11 for the other. Those go directly to RJ-45 sockets, along with power and ground. On the other end, there are Arduino Pro Minis (could've just as easily been UNOs) that connect their RxTx pins 0&1 to the RJ-45 connector. So, pins 8&9 of the Yun go to pins 0&1 of one remote unit, and pins 10&11 of the Yun goes to pins 0&1 of the other unit. I've used RJ-45 sockets just to make them easy to connect with Ethernet patch cables. I've used 15 meter cables with no issues, it can probably go longer without adding line drivers (not that you need to go that long.)

There are some restrictions when using two SoftwareSerial ports - they cannot both listen at the same time. I came up with a simple polling protocol where the Yun is the master, and the slaves don't talk unless spoken to. The code sets one SoftwareSerial to listen, and sends a poll request. After getting a response or a timeout, it sets the other SoftwareSerial to listen, and sends a poll request to that unit. After a response or timeout, it's back to the first.

Even if you go with RS-485, you will have to do a similar polling protocol, as both UNOs won't be able to transmit at the same time. Your code will actually be more complex, since you will need some addressing scheme to select which salve to use during that poll cycle. Also, you must control a data direction line (unless you do it in hardware) to set the line driver to transmit when sending, and set it back to receive when the data has finished actually going out the wire (which is not necessarily trivial to detect in software.)

Whatever wiring method you use, be aware that the SoftwareSerial documentation mentions some restricted pin usage for the Leonardo - the Yun has the same processor as the Leonardo, so the same restrictions apply to the Yun as well.

The maximum Nodes on one RS-485 network is 32. Maximum distance will depend on the applied speed as well as the cable type.
The maximum Modbus Slaves per one multi-drop network is 255.

http://forum.arduino.cc/index.php?topic=359266.msg2486315#msg2486315

sonnyyu:
The maximum Nodes on one RS-485 network is 32. Maximum distance will depend on the applied speed as well as the cable type.
The maximum Modbus Slaves per one multi-drop network is 255.

@sonnyyu, where did you find that diagram?

The RS485 chips I've used have a single pair of AB lines. There is a rx enable pin and a tx enable pin (one is active low so the two pins can be controlled with a single I/O pin).

Depending on the RS485 chip used, the ground line may be optional. The data lines are necessarily referenced to ground, they're referenced against each other.

Most of the RS485 setups I've seen only run a twisted pair of communication lines between the devices.

You might need to use a ground line if your devices are battery operated.

DuaneDegn:
Depending on the RS485 chip used, the ground line may be optional. The data lines are necessarily referenced to ground, they're referenced against each other.

Most of the RS485 setups I've seen only run a twisted pair of communication lines between the devices.

You might need to use a ground line if your devices are battery operated.

You might be able to get away without a ground line, but it is a good idea to have it. True, the data signalling is differential, and the ground is not strictly needed for signalling. But many transceiver chips have a limit to the voltage they can handle between the data lines and the chip's own power supply and ground. If you leave off the ground connection between nodes, and power them with independent power supplies, one node could be floating at a significantly different voltage than the other node, and the voltage between the differential pair and the local power supply might exceed the chip's capabilities. Having the ground can also help with noise immunity, especially if the cables have a shield that is grounded on one end only (the shield should not act as the grounding conductor shown in sonnyyu's picture.

You can get away without the ground in some conditions, but when the system works in the lab but fails in the field, or sometimes works and sometimes doesn't, that might be an indication that a ground is needed.

ShapeShifter:
You might be able to get away without a ground line, but it is a good idea to have it.

Agreed.

I couldn't find the diagram showing how a RS485 system could have a shared ground line without an explicit connection but I thought the text below summed it well (as did your reply).

The connection of ground may happen implicit, because the logic ground of all devices is connected to some global protective ground level. If this is nearly the same on all devices, an extra wire is not required.
If the ground levels are different, without an explicit connection all equalising currents run on the data lines, hence pass the logic circuits for transmit and receive. This can damage the devices, and often does over time.

The above quote is from this page.

I've used ST485 chips in a couple projects. Here's the pinout of the chip.

The RE and DE pins are often connected to a single I/O pin.

Thank you a lot for your help! :slight_smile:

I'm a software guy so everything that requires soldering scares me a bit. I'm afraid to break something.

ShapeShifter:
Do they all have to be connected by the same wire, using one port on the Yun? Or can you use two ports on the Yun, each one connected directly to one Uno?

I can use 2 ports on the Yun to communicate with Unos. And I'm using Ethernet port to connect to the Internet. I was hoping to use one of these modules for RS485:

RS-485 module

Sparkfun USB to RS-485 Converter

With the latter option I think that USB is connected to the Uno and RS485 transceiver connects to the Yun.

And I also assume that there are no problems with getting Arduino data from Linux side.

mityakoval:
I can use 2 ports on the Yun to communicate with Unos.
. . .

RS-485 module

The first link you posted will let you daisy chain the RS485 connections. You wouldn't need two ports to communicate the Unos. You will need a protocol to allow you to identify one of the Unos as the target device.

@DuaneGegn

And what about Sparkfun Converter? Can I connect two of them to Yun with screw terminals, for example, and use USBs to connect to Unos?

mityakoval:
Can I connect two of them to Yun with screw terminals, for example, and use USBs to connect to Unos?

No. That wouldn't work.

Both the Sparkfun board and the Uno are USB slave devices. You can't connect two USB slaves together. You need one to be a host.

mityakoval:
I can use 2 ports on the Yun to communicate with Unos.

Then you don't really need RS-485. While not overly complicated, using RS-485 does introduce some extra work for the hardware and the software. The advantages of RS-485 are:

  • being able to connect several slaves to a single host port
  • being able to send the signals over long wires
  • noise immunity

Since you can use two ports (4 pins) on the Yun, item 1 doesn't apply to you. And since you only need to go 2 to 3 meters, that's not considered a particularly long distance, so item 2 isn't an issue. That only leaves item 3: as long as you are using this in a lab or classroom, and don't need to run it on a factory floor with lots of heavy equipment around, item 3 is likely not an issue.

Given all that, there's really no reason you can't just hook them up directly without any line drivers. The system I mentioned above is hooked up like this: (simple wires between each system, no other chips or interfaces)

  • Yun pin D8 -- slave 1 pin D1 (Yun receive, slave 1 transmit)
  • Yun pin D9 -- slave 1 pin D0 (Yun transmit, slave 1 receive)
  • Yun pin D10 -- slave 2 pin D1 (Yun receive, slave 2 transmit)
  • Yun pin D11 -- slave 2 pin D0 (Yun transmit, slave 2 receive)
  • Grounds on all systems connected together

The only restrictions come from SoftwareSerial: both receive pins must be from the group 8, 9, 10, 11, MISO, MOSI, or SCK (transmit pins can be any pin) and you can't receive from both slaves at the same time (but you wouldn't be able to do that with a shared RS-485 line, either.) So you will need a polling scheme along the times of what I mentioned in my first replay to this thread.

There's nothing wrong with going with RS-485, but it may be overkill for this project, especially when you mention:

mityakoval:
I'm a software guy so everything that requires soldering scares me a bit. I'm afraid to break something.

ShapeShifter:
Then you don't really need RS-485. While not overly complicated, using RS-485 does introduce some extra work for the hardware and the software. The advantages of RS-485 are:

  • being able to connect several slaves to a single host port
  • being able to send the signals over long wires
  • noise immunity

Since you can use two ports (4 pins) on the Yun, item 1 doesn't apply to you. And since you only need to go 2 to 3 meters, that's not considered a particularly long distance, so item 2 isn't an issue. That only leaves item 3: as long as you are using this in a lab or classroom, and don't need to run it on a factory floor with lots of heavy equipment around, item 3 is likely not an issue.

Given all that, there's really no reason you can't just hook them up directly without any line drivers. The system I mentioned above is hooked up like this: (simple wires between each system, no other chips or interfaces)

  • Yun pin D8 -- slave 1 pin D1 (Yun receive, slave 1 transmit)
  • Yun pin D9 -- slave 1 pin D0 (Yun transmit, slave 1 receive)
  • Yun pin D10 -- slave 2 pin D1 (Yun receive, slave 2 transmit)
  • Yun pin D11 -- slave 2 pin D0 (Yun transmit, slave 2 receive)
  • Grounds on all systems connected together

The only restrictions come from SoftwareSerial: both receive pins must be from the group 8, 9, 10, 11, MISO, MOSI, or SCK (transmit pins can be any pin) and you can't receive from both slaves at the same time (but you wouldn't be able to do that with a shared RS-485 line, either.) So you will need a polling scheme along the times of what I mentioned in my first replay to this thread.

There's nothing wrong with going with RS-485, but it may be overkill for this project, especially when you mention:

Thank you, that's really helpful! So what you're saying is that I can use I2C and simple hook-up wires to set up communication between Arduinos?
This system will be placed in an office close to 2 computers. If I understand right the noise you are talking about is electro-magnetic field that can disturb the signal. I don't think there's a really strong one.
The length of the cables will be 3-3.5 meters max.

mityakoval:
Thank you, that's really helpful! So what you're saying is that I can use I2C and simple hook-up wires to set up communication between Arduinos?

NO, that's far too long a distance for unbuffered I2C. I'm talking about using two instances of SoftwareSerial on the master Yun, talking to the hardware serial ports on the remote slaves. The length (and quality) of the wires may affect the maximum speed you can use - if you run into transmission problems, slow it down some.

I have successfully used 19200 baud with 5 meter Cat5 cables (my first post said 15 meter, that was a typo) and I have had zero transmission errors. I used 19200 to be conservative - I haven't yet tested how fast I can go, primarily because the final hardware isn't built yet (my breadboard setup is somewhat fragile) and because I don't really need to go faster than that.

Re-read my first post to this thread (reply #4.) There are more details of this scheme there.

This system will be placed in an office close to 2 computers. If I understand right the noise you are talking about is electro-magnetic field that can disturb the signal. I don't think there's a really strong one.

No, you shouldn't be bad in a situation like that. The worst case is if you had old CRT monitors connected to those computers, that might be the biggest EMI you would find in that environment. I'm talking more about the situation where you're using it on a factory floor and running next to machines with large (tens of horsepower) motors and actuators.

It doesn't hurt anything to try: hook them up with the long lengths of wire and see if you can get them to talk. If it doesn't work well for you, you can always add the RS-485 interfaces - most of the code will be the same in either case. You will still need code on the Yun to poll over the SoftwareSerial port, and code on the remote nodes to listen for poll requests and send/receive data. If it should turn out you need the RS-485 interfaces, you will just remove one SoftwareSerial interface and add the code to manage the transmit/receive direction control lines, but the polling code will stay the same.

Thank you! You've helped me a lot! :slight_smile:

I'm ordering them now. A small question regarding cables:

I need 2 twisted pairs - one cable from pair transmits, the other one – receives. And cables to connect grounds of all boards what makes three of them.

For RS-485, you need a twisted pair for communications, and a ground conductor (and it doesn't hurt to also have a shield that is connected at only one end (you don't want to introduce a ground loop, nor use the shield as a conductor.)

For straight serial data, you need three conductors: transmit, receive, and a common ground. As you propose, it doesn't hurt to use twisted pairs there: Tx/ground in one pair, and Rx/ground in the other. The grounds of the two pairs would be connected together to ground on each end. You shouldn't need an additional ground wire over and above the grounds in the twisted pairs - you could get away with Cat3 cable or regular telephone cable. However, if you use standard network cables (Cat5, Cat5e, Cat6, etc.) you will have four twisted pairs, and it doesn't hurt to use additional pairs as extra grounds.

In my system, I'm using Cat5 cables and RJ-45 connectors, and I'm even going so far as to power the remote nodes through the cables (my remote node power requirements are low, if you need a lot of power at the remote end then powering them through the cable is not a good idea.) The pinout I'm using is one pair for Rx/TX, one pair for a reset signal and ground, one pair for power ground, and another pair for power. So I end up with two wires for power, three for ground, and three signal lines.

The attached shows a highly condensed schematic of what I've done. It's divided into three sections:

  • the Yun Master which uses two Software Serial ports, each going to its own RJ-45 connector (not shown is the power supply where 9V VIN is brought in and converted to 5V for the Yun)
  • the Remote Slave which connects the RJ-45 to the hardware serial pins
  • a Programming Adapter for a remote slave which allows uploading sketches using an FTDI adapter cable. This is the only one that uses the DTR line to reset the remote slave while uploading from the IDE. This lets me program the unit as if I had plugged the FTDI cable directly into it, but it doesn't require me to open up the remote slave's case. This is, of course, completely optional if you have other means of loading code - I'm actually using an Arduino Pro Mini here that does not have a USB interface.

I'm not suggesting that you have to follow this exactly, I'm just giving you an example of a working setup. It's not applicable to all uses and environments, but it should work for your needs. I'm using RJ-45 connectors and Ethernet cables just because they are easy to work with, and relatively inexpensive. There's no reason you couldn't use other types of cables and hard-wire them. I used these breakout boards and connectors when breadboarding the system.

DuaneDegn:
@sonnyyu, where did you find that diagram?
...

http://www.bb-elec.com/Learning-Center/All-White-Papers/Serial/RS-485-Connections-FAQ.aspx