Best practices for long distance communication - Page 1
Forum section: electronics / projects

author: remix
author_rank: Contributor
timestamp: July 01, 2023, 10:18:21 am
content: Hi guys, i want to plan ahead the home automations in my feature new home. KNX is way too expensive for switching my lights on and off.I am using home assistant with custom esp32 and r2040 bords around my current home.Usually i have 2-3 meters of 6 or 8 wire , shielded, non twisted cable like this one:This cable connects my board and the light switches. One wire is ground, the other one is pulled UP through 5.1k ohm and straight into the MCU.For the last couple of years, this design served me well, but at the future new home i want to place the mcu with the relays at the circuit breaker box.I will have power cables for each light coming to the relays and i want the cables for the light switches to come to the mcu as well.The problem is that those light switching cables will be 20,30 even 40 meters long and they might get really close to the 220v power lines at some points.The other issue is that i want to be able to communicate somehow with another MCU on the other end of the line.I guess that the first thing i have to change is the cable. Maybe shielded twisted pairs like the above one or straight to SFTP ?What is the best and easiest way to implement the light switching detection ? EMF and ESD protection ?I was thinking if maybe the higher voltages could help with the interference - (12v, 24v) or even AC 220v and the mcu to detect it somehow when the switch is closed ...And for the long distance communication, again shielded twisted pair and RS-485 maybe ?Thanks

author: Siwastaja
author_rank: Super Contributor
timestamp: July 01, 2023, 10:51:56 am
content: Twisted pair, doesn't need to be shielded even, and primarily CAN, secondarily RS485. RS485 has order of magnitude worse signal integrity in practice than on paper/marketing because of the colossal biasing fuckup; this is corrected in CAN. The simple hardware-level packetization, prioritization and ID system, combined with MCU peripherals having HW filters for subscriptions, is also highly useful concept on CAN. RS485 is still good enough in such simple environment, of course, so not saying it's useless.For on/off signals, e.g. buttons and enable signals etc., one can come a long way by making sure there are no ground loops but the satellite units get their power/ground through that communication cable, that there is no significant voltage drop over the ground signal due to power consumption, and then just RC filter the shit out of the signals. Something like a 100-ohm series resistor + TVS + 470pF which is still good for modest baudrates if you want to run logic level UART or I2C through it.

author: SeanB
author_rank: Super Contributor
timestamp: July 01, 2023, 11:02:35 am
content: Cheapest is to use shielded Cat6 cable, or Cat5 shielded cable, as that gives both a twisted pair, and also shielding, and the cable is very cheap, plus also pull 2 lengths in, as that way you can run a small ethernet connected item, with no extra cabling needed.

author: Infraviolet
author_rank: Super Contributor
timestamp: July 01, 2023, 02:15:56 pm
content: Once you've got the electrical basics sorted out so it does work atleast sometimes for the speed of rising edges your data signals involve...If you have full internal control of the devices at both ends of any link (you have an esp32 or rp2040 as the master and slave device of each connection, rather than say an rp2040 as master to a commercially bought temperature sensor I2C IC which is at the end of a long wire away), then whatever protocol of data you send, include some checksum bytes in the messages. Then you can have the devices on each end only act on mesages with valid checksums, if a few percent, or even a large percentage, of messages get corrupted by interference on the long wires then you'll still only have actions taken based on valid messages, you just might have to send a message several times before a copy gets through uncorrupted, but realy high repetition rates don't sound like something you'd need in home automation applications, it's not like a robot's core computer talking to an ankle joint MCU and needing a valid message in less than a twentieth of a second every time.

author: remix
author_rank: Contributor
timestamp: July 03, 2023, 03:15:02 pm
content: Any recommendations for the board input channels - RC filter or low value resistor in series ?

author: Siwastaja
author_rank: Super Contributor
timestamp: July 03, 2023, 03:59:06 pm
content: I prefer something like R - TVS - R - IO pin, where first R limits clamping current into the TVS, and second R limits current into the MCU protection diodes because the TVS cannot clamp at exact same voltage but must be selected somewhat higher. The TVS can be of old/cheap high-capacitance type (hundreds of pF), and you can add extra capacitance in parallel with it.

author: remix
author_rank: Contributor
timestamp: October 17, 2023, 07:47:47 pm
content: I came across this Paradox alarm system board. The whole idea of this board is pretty close to what i am trying to achieve here.The board supports 4 zones. You can have PIR sensors/REED switches across your house. Tens of meters of unshielded cables that are in close proximity to power cables and ton of interference.You can have different combinations of parallel PIRs and tamper protection.This is are the values of the components that i've measured, based on the board:The first cap C9 is 1206 , 1nF and probably high voltage - 1kv ?The second cap C10 is 0805 22nfHere is the back and front of the board :I know what a spark gap is, but why the curly traces ?P.S. since this is an alarm system, i want to say that i have no interest in reverse engineering the board and/or replicating it.I want to have the same protection/filter for my input pins on my home lights automation board.

author: Kim Christensen
author_rank: Super Contributor
timestamp: October 17, 2023, 08:01:09 pm
content: That style of trace is a meander inductor.

author: p.larner
author_rank: Frequent Contributor
timestamp: October 18, 2023, 12:13:56 am
content: can network?

author: JustMeHere
author_rank: Frequent Contributor
timestamp: October 18, 2023, 01:20:23 am
content: Unshielded CAT5e. You can get some very cheap ethernet chips if low bandwidth isn't a problem. PoE will be an option which will make your life easy. These can do 100m.You can run other protocols over it too like POTS (telephone) and CAN.Run two for every one you think you will need. Conduit will be more expensive, it will make replacing bad cables very easy.CAT 6 is a waste of money and only useful in your server room where you need super fast connections to disk storage, or between hypervisors so they can transfer workloads quickly.As far as main power interfering, cross it at right angles and keep it 1 to 2 studs/rafters away. You main concern isn't EMI, but a voltage spike in mains inducing a voltage spike in your low power network. Your local electrical code will dictate what you actually do.

author: themadhippy
author_rank: Super Contributor
timestamp: October 18, 2023, 01:44:03 am
content: Quotewe been doing that for years with dmx,never had an issue with noise even when running alongside 415v 3 phase cables,or chucking both control and 240v down the same bit of cable.

author: thermistor-guy
author_rank: Frequent Contributor
timestamp: October 20, 2023, 01:30:57 am
content: Quote from: JustMeHere on October 18, 2023, 01:20:23 amOne of the advantages of cat5e and cat6 is the carefully controlled twist in the twisted pairs - makes for good EMI rejection.QuoteYes. Accidents can happen, which damage a cable or outlet. And the spare bandwidth can be useful later.QuoteMaybe. I had unshielded cat6 installed at home years ago, which turned out to be a good decision. Some of those LAN links get saturated often enough,with video and nas transfers, that I'm looking at the price of optical gear. Not buying yet, but looking.

author: jonpaul
author_rank: Super Contributor
timestamp: October 20, 2023, 06:52:35 am
content: Just see this:Try the industry std comm/TX/RX/ref designs for RS-232, RS-485.https://en.wikipedia.org/wiki/RS-485RS-485 RX have built in noise rej and some transient protection.DMX is specific for lighting control and widely used in stage, studio and cinema for lighting control.https://www.sweetwater.com/sweetcare/articles/understanding-dmx/JonPS: See my AES 2017 Berlin talk on "bulletproof Digital Audio Interfaces"

author: Smokey
author_rank: Super Contributor
timestamp: October 20, 2023, 06:59:49 am
content: DMX uses RS485https://en.wikipedia.org/wiki/DMX512

author: uer166
author_rank: Super Contributor
timestamp: October 20, 2023, 08:41:53 am
content: Beware that RS485 only really works if you have permanent fixed direction and an always-on transmitter. The cause is shitty biasing design as mentioned above. If you need bidirectional transmission use CAN or CAN-FD for more speed.

author: Berni
author_rank: Super Contributor
timestamp: October 20, 2023, 09:57:08 am
content: Use CAT5 or CAT6 cable for this.You can have switches on the twisted pairs just fine, the twisted pair even helps keep away the noise. Then just low pass filter it to cleans up any remaining noise. Any interference will have to electrostaticaly or magneticaly couple into the wire and this means it can only cause AC noise without any DC component, so a low pass filter will remove it.Having twisted pair also opens up the door for all sorts other communication buses (since anything long range will need twisted pair). You can do RS485 or RS244 or CAN or Ethernet...etc. A single CAT6 cable can carry up to 10 Gbit/sRS485 can work perfectly fine on a multi-master bus just fine if done properly. But in a lot of cases it is not done properly, leading to its notoriety as being horribly unreliable.

author: Siwastaja
author_rank: Super Contributor
timestamp: October 20, 2023, 06:30:13 pm
content: Quote from: uer166 on October 20, 2023, 08:41:53 amYeah, although it's a bit harsh to say it does not work; RS485isused as multi-drop (single master, multi-slave) bus extensively, and... it usually works. Sometimes even despite abuse (such as leaving GND unconnected, forgetting the biasing, single termination...).All I was saying is that it just has approx. 10 times worse immunity against EMI than "on paper"; people often conveniently ignore the idle level start bit detection issue.But now surely in average home or easy / light industrial conditions even the remaining signal integrity is plenty and thus it will work fine; it's still surely better than, say, one-wire or I2C and people even dothatover long random cables at home! But CAN does better in noisy / demanding conditions, being roughly equal to RS422 / "always-enabled RS485", but with multi-participant capability. Plus added message framing system which, if it happens to match the intended purpose, is pretty handy.

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 20, 2023, 09:36:25 pm
content: I haven't really figured out if the OP was only asking for the low-level side of things (the bus) or the whole shebang along with protocols.Point is, you can use CAN transceivers and twisted pairs and implement any kind of protocol on top of that if that fits you, you don't need to use the whole CAN protocol if you use CAN transceivers.But using CAN transceivers and appropriate twisted pair cables sounds like a reasonable start indeed.

author: Smokey
author_rank: Super Contributor
timestamp: October 21, 2023, 12:20:14 am
content: Quote from: SiliconWizard on October 20, 2023, 09:36:25 pmI just asked that question:https://www.eevblog.com/forum/projects/using-can-transceivers-for-non-canbus-differential-signaling/There are some CAN specific stuff in the hardware to watch out for like "TXD dominant timeout (DTO)"

author: remix
author_rank: Contributor
timestamp: October 21, 2023, 08:37:58 pm
content: Thank you all for your suggestions.The long distance communication seems to be pretty straight forward - Twisted pair cable + transceiver (rs485 or CAN).The current issue that i am researching is the long distance (30m-40m in one direction) light switches and that's why i have posted the alarm system board.I currently have two options:1. Use the alarm design:Its a simple RC filter. Already tried it on a breadboard and under the oscilloscope.It handles the debouncing very well and it will do the job.2. Phototransistor (PT)Not tested it yet, but the PT type can be with a single led.The power supply is 12v at the moment, but i can go for 24v.In the first schematic i am switching the ground i have a low pass filter.In the second schematic i am switching the 12v to the PT. I think that this solution will be more resilient to EMI and noise.I think that the voltage drop over the long distance won't be an issue for the PT if the led resistor is calculated right.Choosing the right cable is a whole other issue. I am planing on using ether KNX cables (2 twisted pairs, shielded ) Or the cheaper alternative - alarm cable (6 or 8 core shielded)What do you guys think, witch option will be best - PT on 12v or 24v or the RC low pass filter and with witch cable ?

author: tychob
author_rank: Contributor
timestamp: October 22, 2023, 12:49:58 am
content: How about CAN for the switches as well? A small microcontroller, 12ish/48ish to 3.3v regulator, and a way to program the address (switches, or just flash the microcontroller) would probably not be perticuarly expensive thing to have. It might even come out cheaper overall if you share the cable for multiple switches and the switched devices on a single bus. It would also add flexibility for things like dimmer encoders in future, and indicators.

author: themadhippy
author_rank: Super Contributor
timestamp: October 22, 2023, 02:46:28 am
content: Quotepersonally neither.I'd use cat 5,at least then youve got the option to upgrade things without ripping holes in the walls,and the cost of a 305M drum of cat5 is about the same price as 300M of 8 core shielded alarm cable.

author: SiliconWizard
author_rank: Super Contributor
timestamp: October 22, 2023, 09:42:54 pm
content: Quote from: tychob on October 22, 2023, 12:49:58 amYeah, small nodes like this sounds like a good, flexible approach. Of course that's more firmware to develop and more sources of potential bugs.

author: Berni
author_rank: Super Contributor
timestamp: October 23, 2023, 05:36:08 am
content: Yep always run at least CAT5, you can reuse it for anything, including running Ethernet around your house to a computer in order to avoid that dreaded WiFiTho having a MCU on the end converting switches to CAN really needs some good reason to do it. Like having a large number of switches on a node (so it provides a significant saving in the number of wires) or perhaps the ability to daisy chain nodes on one log bus (if the physical layout makes more sense for that topology). If not then the MCU is just extra complexity and something to potentially fail.Sensing switches on long wires is not that hard, the signal just needs some protection and filtering to clean it up and then it can go straight into a IO pin.

author: jonpaul
author_rank: Super Contributor
timestamp: October 23, 2023, 06:32:10 am
content: Just seeing these notes now.1/ long runs are not 50m but 1000m. Normal house alarms/remote/Ethernet is in the range 10..50m2/ Alarms esp fire are special as safety and insurance is involved. In some regions like USA a special type of wire is required for fire alarm wiring. Inside air ducts special Teflon wire is used for fire /heat resistance ("plenum)3/ See any alarm controller like Honeywell/Ademco 20-P to see typical interfaces/protection/filters. You can find the panels used on epay $30.4/ CAN bus is specific for vehichel apps and not needed in your application.Jon

author: remix
author_rank: Contributor
timestamp: October 23, 2023, 08:29:47 am
content: I think that i can do both.Leave the inputs with the RC filter and add the CAN transceiver on the board.I spend my weekend looking for CAN Bus devices for home automation and the result is just plenty of dead projects like the SmartBus (s-bus) or the omzlo (nocan) and many more.Found a couple of interesting threads on the Home assistant forum where people do want a wired devices but do not want to use KNX.And the answer is aways - CAN. Home assistant added support for CAN and CanOpenIn some cases, CAN is superior to KNX. Mainly in the transport layer, but CAN lacks the application layer. CanOpen is on solution, but in my opinion, it's not ready yet and way to complicated.Someday someone will came up with an easy to use, reliable protocol on top of CAN and it will become the cheaper alternative to the overpriced KNXAs i said, i will leave my inputs, but will add a CAN transceiver. The bear inputs are good for regular switches, but as tychob said, CAN will be useful for inwall display, dimmer (rotary encoder) or whatever i decide to do in the feature.The cable will be CAT5 or CAT5e

author: Siwastaja
author_rank: Super Contributor
timestamp: October 23, 2023, 09:33:32 am
content: CAN is good because CAN is fairly easy to understand and offers both reliable differential communication and sensible packetization / ID system at once, and that happens to be good match in many different projects. But, once you start adding layers on top of CAN, or use complex specifications made by others, this understandability and simplicity go out of window. That's why you see many dead projects. Simply ignore them. Do not even try to look for inspiration. (And those backed by large bodies, like CANopen, survive, but are nonetheless still unnecessary complications.) Instead, just come up with your own simple message formats. 8 bytes of payload is usually plenty for home automation stuff, and you can easily subscribe to what you need using the filter features of CAN peripherals.As usual, ignore jonpaul. The claim that CAN is somehow vehicle specific is just ridiculous. (Even if it was, then what?)But of course, when you just need on/off switch signal and nothing else, and can afford wiring a pair for each switch, then just RC filter the shit out of it, even without isolation, is just fine really, because wiring will act like a pseudodifferential thing, you won't have any ground loops. Still use CAT5e or even more preferably CAT6 cable! It's cheap, and being twisted pair helps attenuate coupling noise even if it's just a simple mechanical switch. You can later then run Ethernet, CAN or RS485 over it, or wide variety of other stuff. There are really no advantages in using non-twisted cable.

author: peter-h
author_rank: Super Contributor
timestamp: October 23, 2023, 03:21:48 pm
content: A lot depends on who is paying for it, and who is writing the codeI've spent the last 40 years working with RS232 RS422 RS485. It most definitely works, and if correctly wired (e.g. GND connected, not just the 2 wires, so Vcm is defined) then it works perfectly.And it costs very little - under $0.50 per product including the connector. And the software is just driving a UART; can't be simpler. That is why most industrial instruments use RS485 as the basic config port etc.CAN is orders of magnitude more complex, like ETH, USB, etc.

author: Siwastaja
author_rank: Super Contributor
timestamp: October 24, 2023, 06:57:10 am
content: Quote from: peter-h on October 23, 2023, 03:21:48 pmThis is absolute nonsense. Having a framing/CRC/subscription system for you in hardware (MCU peripheral) makes things definitely easier than rolling your own over raw serial, although that isn't too difficult to do either. "Worst case" with CAN is using some too advanced peripheral like the Bosch MCAN that's used in many modern microcontrollers, but it's still pretty much manageable, not any more complex than writing your own packetization over raw serial. (Some older simple CAN peripherals are even easier.) Yes, you have to read the manual, and yes, you will have to write maybe some 40-50 lines of code to initialize the peripheral in correct state and push/pop elements to/from FIFOs correctly.In fact, CAN is so simple to use it is physically impossible for it to be "ordersof magnitude", i.e. over 100 times more difficult than something else. Even a simple GPIO blinking an LED is not 100 times easier, as even that would require writing maybe 3-4 registers and reading 4-5 pages out of reference manual. CAN is about 10 times that.But of course, if all youreallyneed is just a switch, and you can afford pair of wire to each switch, then yes, even CAN or UART or whatever is unnecessary complication.QuoteThis "simple" software usually consist of modbus implementation, careful use of interrupts to generate standard modbus timing (bus receiver on / off with correct turnover times), and this is easily 500 LoC. It's an order of magnitude more difficult than CAN which completely removes all of this hassle and moves it to hardware which can be simply accessed by message FIFOs. And I'm 110% sure if you had to do MODBUS over RS485 as a part of your project involving TLS, we would see countless of threads over many years about weird timing and data corruption issues.Add the fact that 90% of Modbus devices are not really modbus standard compliant and everybody does what they feel like, +1 addressing confusion, biasing, and all shit like that, and RS485/modbus in industrial environment is total PITA compared to something simple like CAN. Which is exactly why CAN is a popular alternative, "the better way of doing it", but MODBUS of course lives strong for legacy compatibility reasons. (My current work involves building automation over RS485/modbus and I would replace it with CAN any day if I could.)

author: Smokey
author_rank: Super Contributor
timestamp: October 24, 2023, 07:28:11 am
content: One crazy thing with CAN is the timing and frequency configuration. I understand part of why it's done that way is flexibility, but it's sort of nuts that essentially the only way to get that close is with a magic calculator. STM32CubeMX doesn't even have that capabilities built in (unless I'm missing something).https://www.kvaser.com/support/calculators/bit-timing-calculator/http://www.bittiming.can-wiki.info/I feel like if the internet went down or those sites died a lot of people would have messed up CAN timing settings.

author: Siwastaja
author_rank: Super Contributor
timestamp: October 24, 2023, 08:13:43 am
content: I always just copied the timing settings out of some online calculator thing, job done. Probably the can-wiki.info link you posted, it even generates the register value to write with most usual CAN peripherals. Yes you can fine-tune the sample point but seeing some conventions use 87.5% and others 75%, I have chosen some random value in between like 80%. Ultimately it doesn't matter. The only thing with timing to remember is, don't use an inaccurate RC oscillator.