0
\$\begingroup\$

I would like to use serial comms between atmega328p microcontrollers over lines up to about 20 meters long, at speeds of around 9600, with the first choice of cable being cat5. Opto isolating them seems necessary and after looking around for a bit the 6N137 seems like a decent choice and its easily available. I want to keep the cost down since I'll need quite a few of them (a few tens).

I would put both the isolators (for tx and rx) at the same end so one isolator LED would be driven via its own pair in the 20m cable, which I don't see as a problem, and one opto output (an open drain mos) would drive the other wire (the tx data from the opto module NMOS output to the long wire).

So I have a NMOS open drain driving a 20m long cat5 wire.

Cat5 with a capacitance of 100pF / meter @ 20m should only be 2nF total, and the 6n137 NMOS output can sink 50mA, so I am wondering if I can just connect a 330R pullup resistor at the far end of the wire (the minimum pullup resistance recommended for the 6n137) and input that direct to an atmega328 pin.

Or should I insert a line driver buffer, push pull logic etc there to actively drive the line high? My reasoning is, I will be pulling the line down actively with a sink current of 50mA and a capacitance of 2nF which will result in a plenty fast transition. With a 330R resistor pulling a 2nF line up thats a time constant of 660ns if I remember correctly. For the low baud rate I am aiming for surely thats plenty? How fast could I probably push the speed with a setup like this?

EDIT: Don't want to get bogged down in software considerations, but it was suggested that I should put this additional info in the question rather than just a comment: This is a custom synchronous bit banged protocol, not 9N1 serial with UARTs. It has a separate clock wire. Timing wise its designed to be able to 'wait' mid byte, which is a desired trait. Its a one to many on the master transmit line and a one to one on the master rx lines. The peak bit rate is 9600 but it can slow down to a stop at any point during the communication. That said, really the problem (question) I have is the electrical side of things, and how to keep it as simple as possible electrically (while compensating to a degree in software with redundancy and crc). The actual data transfer requirements are very low, maybe 20 bytes per second total at the master shared throughout all the slaves, in bursts every few seconds.

\$\endgroup\$
4
  • \$\begingroup\$ Have you considered RS-485, which will easily work over Cat 5 at 20 metres 9600 baud? \$\endgroup\$
    – jonathanjo
    Oct 27, 2022 at 16:27
  • 1
    \$\begingroup\$ you can run a quick test with 20 m cable, a pullup resistor, a mechanical switch, a power supply and an oscilloscope \$\endgroup\$
    – jsotola
    Oct 27, 2022 at 16:28
  • \$\begingroup\$ Re 485, yes I have, its a possibility, but it will still need opto isolation, and I'm wondering how simple I can get it. Bear in mind that I will need to make 40 to 60 of these. \$\endgroup\$
    – Pete
    Oct 27, 2022 at 16:57
  • \$\begingroup\$ Everything looked like you wanted to do just UART. Now you have custom clocked bus and protocol so you need four optos and drivers just to communicate between two devices, so for 50 devices you need 200 optos and drivers. Assuming you want to both transmit and receive data on a node. Consider a data protocol which does not need a clock wire and still allows pauses between bits. I think you might have an X-Y problem, you want to do X and have decided to do it in way Y, and asking how to do Y, while we don't know what X is as there might be better ways to do X than Y. \$\endgroup\$
    – Justme
    Oct 28, 2022 at 4:53

4 Answers 4

0
\$\begingroup\$

Typically, when the words "drive cable" and a length longer than a meter is involved, think line driver.

Note RS485 might seem "specialized" but that also uses line drivers. If you're driving a line, use a line driver. A line driver, is designed to drive a line.

Pull-ups on/across a line are asking for very slow transitions and susceptibility to EMI. That may work for lighting an LED, but it's going to cause delays that could interfere with the symbol framing of 9600 baud and thus = zero or corrupted comms.

Another benefit of RS485 as opposed to some other, more esoteric solution is that it is very noise-resistant and scales well. RS485 usually also offers ESD protected wiring as well; something to always consider in any long-wire-that-could-be-handled-at-any-time project.

\$\endgroup\$
3
  • \$\begingroup\$ Thanks, I understand your point. Having looked into RS485, it does look pretty good, and designed for the purpose, but since it can do such long lines, 1200 meters iirc, in electrically noisy environments and still at pretty high speeds I wonder if its overkill for me. IDK. I can handle some degree of errors, since i'll be using an 8 octet frame with 16 bit CRC and some redundancy (sent messages repeated about 4 times more frequently than they are needed). So I can handle some EMI. But if no frames get through intact it won't work. 50% would be fine though. \$\endgroup\$
    – Pete
    Oct 27, 2022 at 17:06
  • \$\begingroup\$ Theres another point that is significant - I should probably put this in the main question but I don't want to muddy the point, which is about the electrical aspect rather than the software. But in fact this is a custom synchronous bit banged protocol, not 9N1 serial with UARTs. It has a separate clock wire. Timing wise its designed to be able to 'wait' mid byte, which is a desired trait. Its a one to many on the master transmit line and a one to one on the master rx line. \$\endgroup\$
    – Pete
    Oct 27, 2022 at 17:15
  • \$\begingroup\$ @Pete please put that information in your question. \$\endgroup\$ Oct 27, 2022 at 22:07
0
\$\begingroup\$

An open-drain driver with a pullup makes rising edges slow (which means you get pulse width distortion) and is sensitive to noise.

Furthermore, the 6N137 requires a power supply for its output, so you will need to add an isolated power supply for the TX isolator.

Both problems can be solved by moving the isolator to the RX end of each signal. (This is what MIDI does.)

The 6N137 is optimized for 10 Mbps signals. In this applications, it's overkill; better use the slower but more robust H11L1, or even a plain phototransitor optocoupler (e.g., xx817).

\$\endgroup\$
1
  • \$\begingroup\$ Know what u mean re putting each opto at the RX end. But, AFAIK, it creates 2 problems. Since RX and TX are taking a pair each in a bundle of 4 pairs (cat 5 cable, but whatever you use, you're unlikely to want to run 2 separate cables), your HV isolation between stations will only be as good as the insulation between pairs in a single cat 5 cable (not much - 1kV is pushing it imo). Along the same vein, there will be significant capacitive coupling between the 2 stations which could affect communication.. If I have missed something here let me know but I can't see a way to avoid these probs. \$\endgroup\$
    – Pete
    Oct 27, 2022 at 22:16
0
\$\begingroup\$

You have essentially re-invented MIDI interface of the 1980s.

It is a 5V open-collector interface, driving a remote optocoupler with shielded twisted pair, good for 31250 bps for at least 15 meters, and 6N137 which is speed-wise more than you need is very commonly used to implement it.

So you can definitely make it work 20 meters at 9600 bps. The only thing you should maybe change is to put an opto on the RX side.

That implements a point-to-point interface. You need one opto per RX terminal.

If you don't need isolation, just use RS-232 for a point-to-point interface, you need one RS-232 chip on both ends of a cable.

If you don't need isolation, but require some robustness, you can use RS-485, and on a plus side, if you don't use point-to-point links, you can use a single RS-485 bus for all devices so you need only one RS-485 chip per device.

\$\endgroup\$
4
  • \$\begingroup\$ Just took a look at midi - considering how noisy some stage environments can be (especially in the 80s with racks of triac dimmers driving tens of kW of lights on stage) that is reassuring that the principle is robust enough to survive that. Not sure what you mean re 'put an opto on the RX side' - do you mean the same as CL below, ie not drive the line with the output of the opto but drive the LED side? As explained below thats got problems for me. I can send TX referenced power to the slave via the signal cable (spare pairs) to power the opto, but I wonder if I need a tougher output stage. \$\endgroup\$
    – Pete
    Oct 27, 2022 at 22:35
  • \$\begingroup\$ The sn74lvc1g34 is push pull @ 32mA sink and source. Probably suitable? \$\endgroup\$
    – Pete
    Oct 27, 2022 at 22:54
  • \$\begingroup\$ @Pete So driver output at one cable end, opto input at one cable end. Two data pairs, four wires. Keeps the isolation too. 74LVC1G34 is likely way better than required, but again commonly seen buffer. \$\endgroup\$
    – Justme
    Oct 27, 2022 at 22:54
  • \$\begingroup\$ Yea thats pretty much what I am imagining. Regarding the need for the buffer though, although it sharpens up the +ve going edge on transmission, is it really needed in a synchronous system with a separate clock wire? As I said earlier, with 330 ohms into 2nF the time constant is 660ns. Compared to a pulse length @ 9600 of 100us that seems pretty quick, The clock will cause the data to be sampled 50us after the transition... Seems like a long time for it to charge the cable capacitance and stabilise, despite the 330R source impedance (and for reflections etc to settle down). \$\endgroup\$
    – Pete
    Oct 27, 2022 at 23:26
0
\$\begingroup\$

Use isolated RS-485/CAN/LVDS transceivers. (CAN transceivers use less power when idle.)

There are transceivers with integrated DC/DC converters, but you can just as well transmit power over the cable.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.