RS-485 for Cost-Sensitive Sensor/Output Communications
The simple rulesDon't use radio I've kept away from anything radio-based except where absolutely impossible to run wires. There's just no knowing when some interference machine will arrive: too often it's something like press or police walkie-talkies on grand opening of my event.
Don't use ethernet I used to use ethernet with POE, but changed after having to do a lot of rugged environments (exterior, underwater, near big industrial machines). There's no switch to maintain, no POE voltages. And no arms race of 10BaseT, 100BaseT, 1000BaseT.
Don't use board-type buses Once I had a system (built by others) which was SPI at 2 metres: it just about worked but when we needed to move it to 3 metres ... it never worked. In then end I replaced it with RS-485 as described here.
CAN is great but it solves a difficult problem and so there is a lot of complexity. Serial and RS-485 is absolutely ubiquitous and that will save you time over and over again.
The Simple Rules
- Two-wire half-duplex (never four-wire)
- Either a) One master (PC) many slaves (sensors) or b) One speaker and possibly many listeners
- If more than 20about a dozen slaves, think about a) collisions and b) bus loading
- Power from central
- No local connection at the sensor end
- If you need it, use opto-isolators or relays
- Whatever your protocol, it must have a checksum
- Use it like UDP not TCP
- Run it absolutely as slowly as you can
- At 9600 you'll probably never ever have any problems
- Over 20 metres, pay attention to termination
- If you're outside or underwater or big power nearby, still good but do more homework
- If you need fast (say 1Mbit/sec), still good but do more homework
- If you need isolated, still good but do more homework
- If you need hundreds of nodes, still good but do more homework
I've kept away from anything radio-based except where absolutely impossible to run wires. There's just no knowing when some interference machine will arrive: too often it's something like press or police walkie-talkies on grand opening of my event.
Costs
Futureproof
A further advantage of this approach is that it puts all important compatibility issues in the protocol on the RS-485 line. If in future you can't get the same sensor or CPU, get a different one and recreate. If you use 75176-pinout driver chips you will always be able to get replacements.
And if you have need to interface to something you want isolated, it's easy to get an isolated driver such as the one from Digilent (albeit much more expensive, about £17.00). But without any redesign at all of the basic system, nor recoding. I used to use ethernet with POE, but change after having to do a lot of rugged environments (exterior, underwater, near big industrial machines). There's no switch to maintain, no POE voltages. And no arms race of 10BaseT, 100BaseT, 1000BaseT.
References
- Texas Instruments RS-485 Design Guide PDF
- Texas Instruments AN 1057 Ten Ways to Bulletproof RS-485 Interfaces PDF
- Bob Perrin, "The Art and Science of RS-485", Circuit Cellar, July 1999. PDF
Specifics
- "Two-wire half-duplex" actually means three wires: a twisted pair for the signal and a ground, which is often the DC power ground if the systems are driven from the same power.
- You will see enormous contention about whether you need a shield, but Perrin says you don't unless you have exotic circumstances
- In half-duplex, the output the driver is enabled during transmission.
- On a PC you typically enable the driver with RTS of your RS-232 output. The Linux kernel has an ioctl(8) call for enabling this on serial lines
TIOCSRS485
doc - On a microcontroller you use an IO line for Driver-Enable
DE
. You can always listen (connect Receive-Enable/RE
to ground) or more commonly joinDE
and/RE
and drive from the same IO pin. You might conceivably use an IO pin and drive/RE
but I've never seen it. - There are lots and lots of variant driver chips, but chose one in a 75176 package as it's the most common
- It's a great idea to use 8-pin DIL packages and a socket for the driver so that when someone wires it up wrongly you can replace the blown driver; also you can use more robust driver chips if desired, or low-EMI versions with lower slewing rates.