Arduino Forum

Using Arduino => General Electronics => Topic started by: jeffez on May 15, 2016, 06:37 am

Title: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 15, 2016, 06:37 am
Hi guys,
I'm looking at having some inputs and outputs between multiple arduinos. It will be across potentially lengthy distances and i'd be looking at using cat5 for cost and minimising interference.

I put together a schematic and wanted to see if it looks correct and if opto isolating is the best way to go. Basically when the pin outputs HIGH on one arduino, the other ones input pin goes to LOW.

This is part of an earlier project i did when i was starting out.
https://forum.arduino.cc/index.php?topic=362113.0

(http://s32.postimg.org/3tla84qup/tinycad_opto_circuit.jpg) (http://postimg.org/image/3tla84qup/)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Grumpy_Mike on May 15, 2016, 06:52 am
That circuit is fine as it goes. I would use 1K on the receiving end and 220R on the sending end.
However it might not solve any distance related problem, for that you need a differential driver and receiver. But that depends on the exact circumstances of your setup.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 15, 2016, 08:03 am
That circuit is fine as it goes. I would use 1K on the receiving end and 220R on the sending end.
However it might not solve any distance related problem, for that you need a differential driver and receiver. But that depends on the exact circumstances of your setup.
Thanks, so i would want more current running from the arduino on the left given the long distance to ground?

Would a capacitor be worth using the help smooth out potential noise from the long distance?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: ricky101 on May 15, 2016, 10:36 am
You really need to define your meaning of  " long distance "

For distances of up to 20 mtrs + then look at using a pair of MAX232 chips
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 15, 2016, 11:38 am
RS-485 (http://cds.linear.com/docs/en/datasheet/1481fa.pdf)


ARDUINO <=> ARDUINO RS485 (https://arduino-info.wikispaces.com/SoftwareSerialRS485Example)


playground.arduino.cc/Code/ModbusMaster (http://playground.arduino.cc/Code/ModbusMaster)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Paul__B on May 15, 2016, 01:01 pm
Thanks, so I would want more current running from the Arduino on the left given the long distance to ground?
No, the gain of the optocoupler is (usually) less than one, likely about 50%, so you need to feed more current into it than it can switch with the output transistor, thus the lesser resistance.  It has no relationship to the distance.

Your diagram is however wrong - the optocoupler should be on the receiving side with the cable feeding the LED.  The cable resistance will merely be part of, but almost certainly much less than the current limiting resistor for the LED.

Would a capacitor be worth using the help smooth out potential noise from the long distance?
Almost certainly not.  Unless your signal is very slow, perhaps 10 baud (https://en.wikipedia.org/wiki/Baud).
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 15, 2016, 01:49 pm
Quote
Your diagram is however wrong - the optocoupler should be on the receiving side with the cable feeding the LED.  The cable resistance will merely be part of, but almost certainly much less than the current limiting resistor for the LED.
 
I was wondering why that didn't look right.... ;D
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 15, 2016, 02:33 pm
You really need to define your meaning of  " long distance "

For distances of up to 20 mtrs + then look at using a pair of MAX232 chips
In this case it might be upto 20 meters.
I currently have an opto relay board running 50 meters away over cat5 straight from the arduino drawing around 5ma per output pin.
Not sure how this works but using the same cable over a shorter distance with opto coupling between two arduinos is going to need more current and possibly even extra chips?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 15, 2016, 02:43 pm
No, the gain of the optocoupler is (usually) less than one, likely about 50%, so you need to feed more current into it than it can switch with the output transistor, thus the lesser resistance.  It has no relationship to the distance.

Your diagram is however wrong - the optocoupler should be on the receiving side with the cable feeding the LED.  The cable resistance will merely be part of, but almost certainly much less than the current limiting resistor for the LED.
Almost certainly not.  Unless your signal is very slow, perhaps 10 baud (https://en.wikipedia.org/wiki/Baud).

Ah i didn't know which side to put the optocoupler! :D
I put it on that side because i was told in another thread that its better to not apply voltage over a long distance wire.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 15, 2016, 03:15 pm
Ive revised the diagram, does this look correct? I set resistor values from Grumpy_Mikes suggestion.

(http://s32.postimg.org/uze7bxomt/tinycad_opto_circuit.png) (http://postimg.org/image/nw6bwbj75/full/)
adult photo sharing (http://postimage.org/)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Paul__B on May 15, 2016, 03:20 pm
That's fine.  All the resistors and wires are in the right places.

Note just by the way, that putting a HIGH to feed the LED will then switch on the transistor and pull the Arduino input LOW.

This is precisely as it should be, the safest way in case of short circuits.  But your sketch logic will of course, have to expect this.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: MarkT on May 16, 2016, 11:51 am
That circuit is fine as it goes. I would use 1K on the receiving end and 220R on the sending end.
However it might not solve any distance related problem, for that you need a differential driver and receiver. But that depends on the exact circumstances of your setup.
An LED at the end of a long cable is differential, surely?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 18, 2016, 01:45 am
I have two opto relay boards connected to the same arduino, one about 50 meters away and the other about 10 meters. Both work well using cat5 and draw only around 5ma per channel. Infact i have two relay channels sharing the same output pin, which i plan to change but hey it still works! :D. My thinking is it should be possible to replicate this reliability using the same/similar circuit as what the relay board uses.

I haven't been able to find a schematic but by the look of the board there isn't any special driver on there?

http://www.ebay.com.au/itm/DC-12V-4-Channel-Relay-Module-Optocoupler-For-Arduino-PIC-ARM-AVR-DSP-HD23L-/131524714841?hash=item1e9f7be159:g:c-oAAOSwhwdVUbW- (http://www.ebay.com.au/itm/DC-12V-4-Channel-Relay-Module-Optocoupler-For-Arduino-PIC-ARM-AVR-DSP-HD23L-/131524714841?hash=item1e9f7be159:g:c-oAAOSwhwdVUbW-)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 18, 2016, 02:26 am
Does the relay turn on with a LOW ?
It might be similar to this:

(http://forum.arduino.cc/index.php?action=dlattach;topic=400705.0;attach=167429)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 18, 2016, 07:20 am
Does the relay turn on with a LOW ?
It might be similar to this:

(http://forum.arduino.cc/index.php?action=dlattach;topic=400705.0;attach=167429)
Thanks yeah it has Active LOW, i would say that must be an 8 channel version of my one. I'll have to find out what value R5 is plus the opto led resistance.
I'm starting to wonder if i have been wrong about the actual current draw from the arduino for each relay channel. I was told in another thread upto 4mA would be used but i may have misunderstood.

Post #29
https://forum.arduino.cc/index.php?topic=362113.15 (https://forum.arduino.cc/index.php?topic=362113.15)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 18, 2016, 11:21 am
LEDS don't have resistsnce. They are not "resistive" devices by nature because they are semiconfuctors. The fact that they conduct current does not mean they are resistive, just like the fact that somedthing that goes up in the air isn't necessarily an airplane.
Leds are NON-linear, because they are diodes, but the diode forward voltage drop is not a standard 0.7 V because they are special diodes with forward voltage in the 2.0 to 4.0 V range.
Their forward current is specified with their forward voltage:

ie;
"V(f) = 2.2 V @ 20 mA"
The current limiting resistor is calculated by
[Vin- V(f) ]/I(f) = (80V- 2.2 V)/0.020 A  = 77.8 V,/ 0.020 A = 3890 ohms
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Wawa on May 18, 2016, 01:32 pm
I'll have to find out what value R5 is plus the opto led resistance.
I'm starting to wonder if i have been wrong about the actual current draw from the arduino for each relay channel. I was told in another thread upto 4mA would be used but i may have misunderstood.
R5 is 1k (102 printed on it). IR LED Vf is ~1.2volt. Indicator LED Vf is ~1.8volt.
So the Arduino has to sink (5 - 1.2 - 1.8 ) = 2volt / 1k = ~2mA.
Some  boards have no indicator LED. Then current is ~3.8mA.
Leo..
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 19, 2016, 05:41 am
So if i was to use a PS2501 Optocoupler
(http://s32.postimg.org/tr9i77dfp/datasheet.png) (http://postimage.org/)
image free hosting (http://postimage.org/)

Going by the datasheet Vf is 1.15-1.4V
So i'm my schematic on the diode side
I = (5-1.15)/220 = 17ma

Is this correct? and if so is 17ma ideal or could i consider a higher resistance?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 19, 2016, 06:04 am
Collector saturation @ I(f)= 10 mA (R = 380 ohms)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 31, 2016, 05:03 am
Thanks this has helped allot and i have since done a new schematic for my project.

I'm now looking at using pwm as some changes in requirements means that simple HIGH/LOW readings wont be enough.

I'm thinking of having one input and one output channel between each arduino and using rj45 ports which would use two of the twisted pairs in the cat5 cable. All using the optocoupling like in the sample schematic with pwm on the output pins.

My questions are,
given the use of an optocoupler and the potentially lengthy distance should i consider setting the pwm output to a slow rate? From this document it says you can set them to output as low as 30Hz and 60Hz depending on which pin you use.
https://arduino-info.wikispaces.com/Arduino-PWM-Frequency (https://arduino-info.wikispaces.com/Arduino-PWM-Frequency)

Also I'm considering to just instead use something like a the suggested RS485. How ever i need to connect to upto 4 other arduinos and with only one pair of serial i/o pins is this even possible?

I'd prefer to stick with the opto and pwm if possible to minimize the use of extra parts and keep everything on the one pcb. Is there an alternative way to deal with lengthy distances other than serial line drivers? Possibly some sort of pwm repeater?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 31, 2016, 05:29 am
Quote
I'm thinking of having one input and one output channel between each arduino and using rj45 ports which would use two of the twisted pairs in the cat5 cable. All using the optocoupling like in the sample schematic with pwm on the output pins.
What protocol is that ?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on May 31, 2016, 11:27 am
What protocol is that ?
Writing an analogue value on a pwm output pin then at the other end using pulseIn() to read the duty cycle and determine the value that was sent. I searched around and haven't found if there's a name for that kind of protocol other than the pwm.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on May 31, 2016, 03:11 pm
How is that any more efficient that putting an (4.7 k/1 uF) RC LPF at the other end and reading it with analogRead ?

Actually, if you want to , there is no reason you can't use RS485 to send your data. I don't know why you chose to send PWM but you can still do that using RS485. Usually, data is sent using a protocol that involves a header, a packet and a checksum but that's beyond my level of S/W expertise. If I just needed to send an 8-bit value I would probably have to do it the same way you are, (because I don't know how to do it any other way , LOL).
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 01, 2016, 07:06 am
How is that any more efficient that putting an (4.7 k/1 uF) RC LPF at the other end and reading it with analogRead ?
It probably isn't. I actually had to look up what you meant by RC LPF which probably says allot. Being a software engineer i went for the solution that was electronically easier to understand but harder on the programming side of things. Somehow i got the impression that reading pwm has to be done manually in code on a digital pin. So maybe i should use an analogue pin read instead then?

http://www.benripley.com/diy/arduino/three-ways-to-read-a-pwm-signal-with-arduino/

Actually, if you want to , there is no reason you can't use RS485 to send your data. I don't know why you chose to send PWM but you can still do that using RS485. Usually, data is sent using a protocol that involves a header, a packet and a checksum but that's beyond my level of S/W expertise. If I just needed to send an 8-bit value I would probably have to do it the same way you are, (because I don't know how to do it any other way , LOL).
If it wasn't for the distance problem i would stick with pwm no worries, as i need to connect to four other arduinos. This way would mean everything fits on the one pcb with just a few optos and resistors.

If i only had to connect to one other arduino not four then RS485 could be better. Though i'm only wanting to send a single integer say 4-5 times an hour, so even then a line driver is probably a bit overkill.

If possible i'd prefer to use pwm but i'm not sure about the distance problem. The arduinos will be in a warehouse upto 100 meters from each other. I can write some error/interference tolerance into the code but for all i know pwm at those distances is practically impossible?

So i'm stuck between both solutions and currently not sure which way to go with! lol



Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Grumpy_Mike on Jun 01, 2016, 09:07 am
Quote
If i only had to connect to one other arduino not four then RS485 could be better.
But RS485 is designed to be chained to multiple devices.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 01, 2016, 03:27 pm
Quote
But RS485 is designed to be chained to multiple devices.
While this is true, it is also true that an arduino can read analog or pwm on one pin and output it on another so really the distance is the deciding factor for which to use.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: Paul__B on Jun 01, 2016, 06:41 pm
But RS485 is designed to be chained to multiple devices.
The OP specifies multiple devices.

Clearly, the proper way to perform this communication is with a serial protocol.  Any other will end in tears.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 01, 2016, 08:51 pm
Quote
Clearly, the proper way to perform this communication is with a serial protocol.  Any other will end in tears.
RS485 output at one end is exactly the same as the input at the other end. Is there any valid reason that input can't be serial ? (as far as I can tell it could be anything, including PWM).
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 03, 2016, 06:18 am
While this is true, it is also true that an arduino can read analog or pwm on one pin and output it on another so really the distance is the deciding factor for which to use.
Since RS485 is reliable at long distances i decided ill probably have to go with that.
I wasn't able to find any definitive info on whether pwm can run stable upto 100 meters. This distances may even need to be more than that.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 03, 2016, 07:05 am
Do you know what the range is for  RS485 ?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 03, 2016, 08:52 am
Do you know what the range is for  RS485 ?
Apparently upto 1200m if you can deal with a slow 100 kbit/s. Still way overkill for what i need
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: MikeLittle on Jun 04, 2016, 06:29 am
Back in '87 we ran two computers over a full duplex 422 driver on a campus phone system. I think the distance was close to a mile between buildings over the wire. Can't remember the BAUD rate. May have been 9600, but not sure. Never had any problems.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 05, 2016, 06:41 am
Back in '87 we ran two computers over a full duplex 422 driver on a campus phone system. I think the distance was close to a mile between buildings over the wire. Can't remember the BAUD rate. May have been 9600, but not sure. Never had any problems.
Looks like distance is definitely no issue then which is great. I have to say the issue now is how much more complex it's going to be to get this to work using these line drivers. Seems like RS485 uses protocols that want a master slave setup connected in a bus. Where as i'm needing more of an adhoc type network where each node is equivalent. This may be a subject for another section of the forum but will see how i go! :)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 05, 2016, 08:04 am
Quote
Seems like RS485 uses protocols that want a master slave setup connected in a bus. Where as i'm needing more adhoc type network where each node is equivalent. This may be a subject for another section of the forum but will see how i go!
The chips don't have any protocol. They send what is sent and receive what was sent to them. Having multiple chips on the same bus demands the existence of a protocol.
Look at the datasheet (http://cds.linear.com/docs/en/datasheet/1481fa.pdf)

You need a 120 ohm termination resistor at each end of the bus to prevent reflected wave. You'll find that with DeviceNet, CANBUS , MODBUS etc...
You haven't provided any design criteria for the bus so no comment is possible. If you had only one sender and one receiver,  I don't know how you would call one a Master and the other a Slave unless the software does not allow the Slave to send. Protocols are created to prevent chaos. There is no rule that says you have to use any specific protocol and only you can say what you need a protocol to do in your application. Whatever you do with your software that prevents chaos is , by definition , a protocol.
Get a couple of these chips and connect leds to their receivers and I think you will see that you can send whatever you want , however you want. If you have multiple devices on the same bus, you must write your software so that each device has an ID and only responds to messages sent to their ID, but somehow I think you knew that.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 06, 2016, 10:52 am
You haven't provided any design criteria for the bus so no comment is possible. If you had only one sender and one receiver,  I don't know how you would call one a Master and the other a Slave unless the software does not allow the Slave to send. Protocols are created to prevent chaos. There is no rule that says you have to use any specific protocol and only you can say what you need a protocol to do in your application. Whatever you do with your software that prevents chaos is , by definition , a protocol.
The basic criteria would be:

The protocol allows for a simple end to end link between two RS485s where both can initiate contact with the other end.

This may have to be a bus containing a master and one slave, as long as the slave can do everything the master can do.

Using software routing and addressing i could then create the adhoc network i need.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 06, 2016, 12:18 pm
I'm also looking into if its possible to achieve this kind of network using a bus based protocol such as modbus. Since the number of connected devices would be dynamic (between 0-4) , is it possible for modbus to have a bus with no slaves as long as i have the termination resistors?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 06, 2016, 01:36 pm
As long as the terminatin resistors are at the extreme ends of the main trunk then it's up to the software, which is not my area of expertise.
Whst is the definition of Master or Slave ?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 06, 2016, 02:23 pm
As long as the terminatin resistors are at the extreme ends of the main trunk then it's up to the software, which is not my area of expertise.
Ah ok, in this case then the idea i had wouldn't work as it meant having both ends of the bus on a small loop on the pcb and connecting slaves to that which would travel long distances.

Whst is the definition of Master or Slave ?
Another name might be primary and secondary. Means one device (the primary/master) controls one or more other devices (secondaries/slaves). This is centralized with one device instigating communication.

Since what i'm needing is any device to instigate communication then what i need is a peer to peer or adhoc type protocol which is decentralized. All devices are equal and effectively can play the role of both a master and a slave device.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 06, 2016, 02:54 pm
Actually i'm glad you mentioned CAN bus as i looked it up and i see it was made with the idea of there being no single master or main controller. This may be exactly what i'd need.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 06, 2016, 03:50 pm
Recall that the purpose of termination resistors is to prevent reflected wave. That's why they have to be at the extreme ends of the main trunk.

Quote
Another name might be primary and secondary. Means one device (the primary/master) controls one or more other devices (secondaries/slaves). This is centralized with one device instigating communication.

Since what i'm needing is any device to instigate communication then what i need is a peer to peer or adhoc type protocol which is decentralized. All devices are equal and effectively can play the role of both a master and a slave device.
I think what I was trying to convey is that the RS485 devices are identical for Master and Slave so there is nothing in the Hardware that makes one a Master or a Slave. Thus only the software knows "who's who ?" and if you look at some protocols there are packets with headers and then device ID numbers and devices only respond to commands sent to their device ID. On DeviceNet devices, there are rotary switches to set the device number (1 -63 I believe). What makes the Master the Master is that it has the device ID "0".

As I mentioned, software is not my area of expertise but I can tell you that any "protocol" you "create", (assuming you don't want to learn an existing protocol) that can identify a device ID and respond to it will work, provided the uController acting as the Master is running software that uses a CASE statement or some other S/W mechanism to determine that an incoming message is from a particular device that it is trying to control.  You can test that yourself with only three arduinos. (one Master, one slave and one 3rd Party )
It's very simple. If the Slave responds and the 3rd Party doesn't then you're in business.

Take a look at this (http://www.cromptonusa.com/rs485_guide.pdf):

Have you read this (http://www.gammon.com.au/forum/?id=11428) ?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 09, 2016, 12:31 pm
Recall that the purpose of termination resistors is to prevent reflected wave. That's why they have to be at the extreme ends of the main trunk.

I think what I was trying to convey is that the RS485 devices are identical for Master and Slave so there is nothing in the Hardware that makes one a Master or a Slave. Thus only the software knows "who's who ?" and if you look at some protocols there are packets with headers and then device ID numbers and devices only respond to commands sent to their device ID. On DeviceNet devices, there are rotary switches to set the device number (1 -63 I believe). What makes the Master the Master is that it has the device ID "0".

As I mentioned, software is not my area of expertise but I can tell you that any "protocol" you "create", (assuming you don't want to learn an existing protocol) that can identify a device ID and respond to it will work, provided the uController acting as the Master is running software that uses a CASE statement or some other S/W mechanism to determine that an incoming message is from a particular device that it is trying to control.  You can test that yourself with only three arduinos. (one Master, one slave and one 3rd Party )
It's very simple. If the Slave responds and the 3rd Party doesn't then you're in business.

Take a look at this (http://www.cromptonusa.com/rs485_guide.pdf):

Have you read this (http://www.gammon.com.au/forum/?id=11428) ?
Some good resources. I'd say if i focus on getting the physical connections correct, i can then work on the code after that. It looks like the whole master/slave thing is mainly a collision avoidance thing.

Given that the rs485s long distance ability is only along the bus and not from the bus out the the devices connected onto it i would need to run 4 buses (4 rs485s) to connect to 4 other arduinos at large distances. 8 if i want full duplex.

I made a schematic to upload here which is just a sample showing 3 arduinos connected up. I wasn't able to fit all the connections onto the space provided on the cad software but the arduino on the top shows how the 4 rs485s might connect up to the one arduino. I haven't found any samples of people doing this kind of set up so i'm a bit in unkown territory.

I'm not sure if connecting all 4 to the tx and rx pins is a good idea or not? Collisions would obviously be an issue but i can work around that even if i write my own system to listen before transmitting etc. Also max current draw from the rx and tx pins could be a concern.

Maybe it would be better if each rs485 had its own serial pins using a soft serial setup?

Sorry i couldn't find an rs485 component in the cad program, so i used something else with 8 pins. Obviously id get the proper one on my main project schematic which i use kicad for. I use TinyCad to put together quick sample schematics for this forum.

(http://s33.postimg.org/ldoll14qz/sample_rs485_connections.jpg) (http://postimg.org/image/ldoll14qz/)

Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 10, 2016, 07:26 am
Take a look at this (https://www.google.com/search?q=rs485+network+example&tbm=isch&imgil=Q5uap1CYN_a_UM%253A%253BGygJ8y51t2mKxM%253Bhttp%25253A%25252F%25252Fltxfaq.custhelp.com%25252Fapp%25252Fanswers%25252Fdetail%25252Fa_id%25252F480%25252F~%25252Ftroubleshooting-rs485-connections&source=iu&pf=m&fir=Q5uap1CYN_a_UM%253A%252CGygJ8y51t2mKxM%252C_&usg=__T1378DpYrii39VjuQJ9e_mzQGVs%3D&biw=1920&bih=935&ved=0ahUKEwjx8ZD82ZzNAhUDwmMKHc79BYYQyjcIPw&ei=30xaV7GGOoOEjwPO-5ewCA#imgrc=kOIeyr_xrY2dVM%3A)

That schematic is not an RS485 network. I don't know what it is but it doesn't conform to anything I've ever seen. You can't have multiple processors connected to the same Tx and Rx pins. The maximum is two, (one at each end). If you want a network, you have to use RS485 network devices, like the MAX485 or the LT1481, and it must conform to standard RS485 network configuration, like the link above. What you have posted is not a valid circuit, and doesn't qualify as anything (AFAIK). The bus connections are on the differential A/B bus , not on the TTL serial side. Each RS485 tranceiver can only be connected to one device or microprocessor on the TTL serial side. The 'bus' is the A/B bus. That is the only place you can have multiple devices connected together. You circuit is invalid because of the following:

1- multiple connections on the 'non-bus' TTL serial side.
2- multiple devices on the Tx & Rx lines.
3-no bus
4-no termination resistors
5-no RS485 transceivers

Look at the DATASHEET (http://cds.linear.com/docs/en/datasheet/1481fa.pdf)

You seem to have completely overlooked the fact that what makes it possible to have multiple devices connected to the bus is that each device has a transmit enable, that is used to disable all but ONE of the devices at ANY given moment. If you have 6 devices on the BUS, only one of them is transmitting at any given moment. The rest are disabled via the DE pin. Read the datasheet.

At this point you should be asking yourself "How do you select the one device that is transmitting ?"
The answer is "That's what the RS485 BUS PROTOCOLS are designed to do. (MODBUS, CANBUS, DeviceNet, DMX512) Without them, you would have chaos and multiple bus collisions.

I suggest you do some homework and research RS485 because what you just posted indicates you are a long way from a working circuit. (as far as I can tell).
There are NO RS485 devices in that schematic An RS485 device is a transceiver like a MAX485 or a  LT1481. There is no such thing as an RS485  network without RS485 devices.

Take a look at the MODBUS Protocol (http://www.cromptonusa.com/rs485_guide.pdf)

Note how there is a slave address, a message length, a start address (Hi, Lo) , data,  an error check and much more. Control of the transmit enable is address dependent. In a way, you could say that the "slaves" are only allowed to "speak" when they are "spoken " to.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 19, 2016, 07:45 am
Take a look at this (https://www.google.com/search?q=rs485+network+example&tbm=isch&imgil=Q5uap1CYN_a_UM%253A%253BGygJ8y51t2mKxM%253Bhttp%25253A%25252F%25252Fltxfaq.custhelp.com%25252Fapp%25252Fanswers%25252Fdetail%25252Fa_id%25252F480%25252F~%25252Ftroubleshooting-rs485-connections&source=iu&pf=m&fir=Q5uap1CYN_a_UM%253A%252CGygJ8y51t2mKxM%252C_&usg=__T1378DpYrii39VjuQJ9e_mzQGVs%3D&biw=1920&bih=935&ved=0ahUKEwjx8ZD82ZzNAhUDwmMKHc79BYYQyjcIPw&ei=30xaV7GGOoOEjwPO-5ewCA#imgrc=kOIeyr_xrY2dVM%3A)

That schematic is not an RS485 network. I don't know what it is but it doesn't conform to anything I've ever seen. You can't have multiple processors connected to the same Tx and Rx pins. The maximum is two, (one at each end). If you want a network, you have to use RS485 network devices, like the MAX485 or the LT1481, and it must conform to standard RS485 network configuration, like the link above. What you have posted is not a valid circuit, and doesn't qualify as anything (AFAIK). The bus connections are on the differential A/B bus , not on the TTL serial side. Each RS485 tranceiver can only be connected to one device or microprocessor on the TTL serial side. The 'bus' is the A/B bus. That is the only place you can have multiple devices connected together. You circuit is invalid because of the following:

1- multiple connections on the 'non-bus' TTL serial side.
2- multiple devices on the Tx & Rx lines.
3-no bus
4-no termination resistors
5-no RS485 transceivers

Look at the DATASHEET (http://cds.linear.com/docs/en/datasheet/1481fa.pdf)

You seem to have completely overlooked the fact that what makes it possible to have multiple devices connected to the bus is that each device has a transmit enable, that is used to disable all but ONE of the devices at ANY given moment. If you have 6 devices on the BUS, only one of them is transmitting at any given moment. The rest are disabled via the DE pin. Read the datasheet.

At this point you should be asking yourself "How do you select the one device that is transmitting ?"
The answer is "That's what the RS485 BUS PROTOCOLS are designed to do. (MODBUS, CANBUS, DeviceNet, DMX512) Without them, you would have chaos and multiple bus collisions.

I suggest you do some homework and research RS485 because what you just posted indicates you are a long way from a working circuit. (as far as I can tell).
There are NO RS485 devices in that schematic An RS485 device is a transceiver like a MAX485 or a  LT1481. There is no such thing as an RS485  network without RS485 devices.

Take a look at the MODBUS Protocol (http://www.cromptonusa.com/rs485_guide.pdf)

Note how there is a slave address, a message length, a start address (Hi, Lo) , data,  an error check and much more. Control of the transmit enable is address dependent. In a way, you could say that the "slaves" are only allowed to "speak" when they are "spoken " to.
I now can see major mistakes i did there, pretty embarrassing :D took me a while to get back due to other work etc. But i think i've now got this right (or at least close). I missed the termination resistors on the previous diagram because the rs485s i was going to use were on a small pcb that already had the resistors on them.

Anyway this diagram i did with max485 chips and would utilise a software serial library to allow tx and rx to work on normal digital pins.
(https://s32.postimg.org/tv5afgpf5/sample_rs485_maxchips_connections_V2.png) (https://postimg.org/image/tv5afgpf5/)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 20, 2016, 05:27 am
I noticed that on this RS485 board which uses the max485 chip, there's extra vcc applied and resistors in extra places.
(http://img.dxcdn.com/productimages/sku_163849_1.jpg)

(http://www.electrodragon.com/w/images/thumb/5/58/Max485_module_schematic1.jpg/400px-Max485_module_schematic1.jpg)

I based my diagram off the schematic in the first page off the datasheet which didn't show these, but that may be because the datasheet only shows a part of the full circuit needed for use with an arduino.  Should i add these into my schematic with the max485s?

Here's another example but there's no 10k resistors on R0 RE DE D1 and the resistors on the other side are only 680 ohms where as on the one above uses 20K ones.
http://www.gammon.com.au/forum/?id=11428
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 20, 2016, 08:51 am
I can't read a schematic the size of a postage stamp. I don't know what you did but that file is resized into oblivion . Start over and don't resize it unless you have to and then only 20% at a time .


Quote
I noticed that on this RS485 board which uses the max485 chip, there's extra vcc applied and resistors in extra places.
There is only one Vcc. The Green screw terminals are for the A & B lines. Also, RS485 convention dictates one termination resistor at each end . You don't need all those other resistors. I've never used them and never seen them used any where else. It won't work with only one termination resistor. I told you it needs one at each end of the main trunk. If you can't post the photo of the schematic then post a link.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 20, 2016, 09:06 am
(https://s32.postimg.org/9b0ggz9o5/sample_rs485_maxchips_connections_V2.png)
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 20, 2016, 09:12 am
I can't read a schematic the size of a postage stamp. I don't know what you did but that file is resized into oblivion . Start over and don't resize it unless you have to and then only 20% at a time .
Not sure what happened there. I didnt resize it. It was supposed to be a link with a small thumbnail image. I've put up a direct link which should show its full size.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 20, 2016, 09:39 am
That schematic looks ok. I would avoid thumbnails .  They are never used on the forum. If you are posting a link then use the LINKS toolbutton to enter a link label, Otherwise just post the schematic like you did. That circuit should work. Have figured out how you are going to manage the communication so both sides don't try to send at the same time ?
The simplest and oldest method is that the first side to send sends an ACK at the end  of the message. The receiving unit sends an ACK (Acknowledge) when it receives one , telling the sender it is ready to receive more. If the message it just received was a command then it sends an ACK before executing the command.  The command could be "SND TEMP DATA".
It would send "ACK TMP", read the temp, and then send it. The other unit would respond wjth "ACK TMP", and then might might follow with "SND MTR SPD". The receiving unit would respond with "ACK MTR SPD" , read the speed , send it and wait. The receiving unit would receive the speed and respond with "ACK MTR SPD". This continues with one always requesting info and the other supplying it until the requestor sends ACK ACK ACK to indicate it is the others turn to request.  The other unit would receive the ACK ACK ACK  and respond with a request like "SND BAT VLTG" . This method can be modified such that either side can send critcal data whdn necessary , like "LOW BAT !" to which the other would respond with "ACK LOW BAT".


 (that's Full Duplex mode by the way) Your schematic prevents full duplex because you tied RE-NOT to DE. According to the truth tables on the LT1481 (http://cds.linear.com/docs/en/datasheet/1481fa.pdf) datasheet, RE-NOT cannot be LOW if DE is HIGH and vice versa, preventing full duplex. Here (http://www.ti.com/lit/ds/symlink/iso15.pdf) is a full duplex chip.
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: jeffez on Jun 20, 2016, 10:55 am
That schematic looks ok. I would avoid thumbnails .  They are never used on the forum. If you are posting a link then use the LINKS toolbutton to enter a link label, Otherwise just post the schematic like you did. That circuit should work. Have figured out how you are going to manage the communication so both sides don't try to send at the same time ?
The simplest and oldest method is that the first side to send sends an ACK at the end  of the message. The receiving unit sends an ACK (Acknowledge) when it receives one , telling the sender it is ready to receive more. If the message it just received was a command then it sends an ACK before executing the command.  The command could be "SND TEMP DATA".
It would send "ACK TMP", read the temp, and then send it. The other unit would respond wjth "ACK TMP", and then might might follow with "SND MTR SPD". The receiving unit would respond with "ACK MTR SPD" , read the speed , send it and wait. The receiving unit would receive the speed and respond with "ACK MTR SPD". This continues with one always requesting info and the other supplying it until the requestor sends ACK ACK ACK to indicate it is the others turn to request.  The other unit would receive the ACK ACK ACK  and respond with a request like "SND BAT VLTG" . This method can be modified such that either side can send critcal data whdn necessary , like "LOW BAT !" to which the other would respond with "ACK LOW BAT".


 (that's Full Duplex mode by the way) Your schematic prevents full duplex because you tied RE-NOT to DE. According to the truth tables on the LT1481 (http://cds.linear.com/docs/en/datasheet/1481fa.pdf) datasheet, RE-NOT cannot be LOW if DE is HIGH and vice versa, preventing full duplex. Here (http://www.ti.com/lit/ds/symlink/iso15.pdf) is a full duplex chip.
Glad to hear i'm on the right track. I thought my last schematic was missing something though because this schematic below which is used on the blue rs485 pcb i showed has more added to it than just the max chip and the termination resistor.
(http://www.electrodragon.com/w/images/thumb/5/58/Max485_module_schematic1.jpg/400px-Max485_module_schematic1.jpg)

As for preventing both sides communicating at the same time i wasn't fully decided on that but i was thinking of using canbus and within that using some kind of three way handshake before sending data, which i think is similar to as you described.

I was planning to use half duplex given the system would only send the occasional message back and forth. How ever perhaps full duplex would allow for the rare case where both ends want to message at the same time. Perhaps i should do it that way? Does using a software serial undo the benefits of full duplex given that software serial can't transmit and receive at the same time?
Title: Re: I/O between arduinos with opto coupling. Sample schematic
Post by: raschemmel on Jun 20, 2016, 03:48 pm
Quote
Does using a software serial undo the benefits of full duplex given that software serial can't transmit and receive at the same time?
I'll have to think about that but I think you already answered it .