Networking for AVRs - Page 1
Forum section: electronics / beginners

author: bifferos
author_rank: Regular Contributor
timestamp: March 10, 2022, 06:03:01 pm
content: I have cat5 wiring to all rooms, with two cables (and sockets) in every wall box. Most of the time the second socket is unused, so I thought to use these sockets to put a DS1820 in every room. This is fine for temperature sensing but I realised it would be nice to include an AVR with the temperature sensor so I'd have a module in each room with more functions, such as buttons, buzzers, LED displays and so on as well as the temperature sensing function.I could use my own form of power over cat5 to power these modules, I could use two of the 8 wires to transmit 12v, with a 5v regulator on the module, but what protocol should I use to talk to these modules? Is it overkill to use ethernet when I control both ends, and the data rates will be very low? I imagine ethernet, RS485 and CAN bus would all require transceiver ICs. So I wondered if there's some differential protocol I could easily implement with an AVR and some very basic components. Something cheap that would give much better noise immunity than a simple UART but not be overkill. I think the max distance would be about 20 metres.thanks!

author: rstofer
author_rank: Super Contributor
timestamp: March 10, 2022, 07:05:24 pm
content: I know you said AVR and I'm going to suggest the Arduino Ethernet board but maybe there is a way to incorporate it.https://docs.arduino.cc/retired/boards/arduino-ethernet-rev3-without-poeThere's even a version with PoEhttps://docs.arduino.cc/retired/boards/arduino-ethernet-rev3-with-poeA more universal approachhttps://www.digikey.com/en/products/detail/wiznet/WIZ750SR-232/8789615

author: viperidae
author_rank: Frequent Contributor
timestamp: March 10, 2022, 10:20:31 pm
content: I'd suggest to go with CAN, it provides you with a robust physical layer, which at low speeds can go quite far. No need for any sort of hub architecture to connect all your rooms. Built in bus arbitration. Controllers and physical interfaces are also cheap.

author: penfold
author_rank: Frequent Contributor
timestamp: March 11, 2022, 12:05:12 am
content: Quote from: bifferos on March 10, 2022, 06:03:01 pmI guess ethernet would be the most expensive option, but it would (with an external mac+phy IC) be the quickest to set up, it's especially quick to get it going in arduino environment. Very robust and minimal messing around, just a bit more expensive.CAN and RS485 are both relatively cheap physically, it'll depend what protocol you run on top of them as to how much of a faff it becomes. Any "robustness" in RS485 will be purely down to the data protocol, CAN does you some favours in the PHY. No-doubt there'll be an arduino library for some CAN and RS485 data protocols... so probably quit easy either way TBF.I'm assuming you have a bit of a star type network physically, cables leading to the same place? That's not the most favourable setup for RS485, probably also not for CAN (but I think the standard allows it)... worth checking out what data-rates you can get away with - the concern being that a star network with a single driver will get reflections from each leg if not properly terminated OR need a particularly high drive current if it sees all the terminators in parallel.As I was writing the last paragraph it did just jump to mind on the cost side: if you have 5 temperature PCBs per room (assuming it'll be your own design) and one hub/central PCB (using CAN or RS485) - it may be cheaper to accept the incrimental cost of adding the ethernet mac+phy plus rj45 socket per temperature board and buy a POE switch that can also power them avoiding the cost of the HUB PCB. Then with the ethernet option, you could use a RasPi and take advantage of the linux-iness to make a nice web-interface to the network perhaps.

author: bifferos
author_rank: Regular Contributor
timestamp: March 11, 2022, 12:44:45 am
content: Yes, the network is star topology.I've used a couple of the ethernet shields. I used a Nano one and I think the wiznet with a full-size Arduino. They never worked for longer than a few months without a reboot so I wondered if this is a reliable approach. I've had Linux servers running for a year+, also hacked routers tend to do pretty well. What I was doing with AVR was so simple I find it hard to believe it was my code, but it could have been I suppose.I could revisit ethernet and stick to raw ether frames instead of web servers, never tried that, but I still have to work power into this. PoE itself is out because I don't really want to change my 24-port switch for this project, so I want something DIY. I've got some injectors, but they end up making the whole thing bulky.There is some attraction to just hooking up an AVR to one of these adapters:https://www.hobbytronics.co.uk/rj45-breakoutI already have a bunch of these that I got cheap. A couple of caps, a regulator and power is done. I could just go for ridiculously low baud (300 or something) and stick to 5v TTL serial, it may work, particularly if I keep a couple of rj45 lines free for some kind of 'interrupt'. for things like button presses.I have a couple of the MCP2515 can bus modules in the component drawer, but when I look at these they don't seem any simpler than ethernet. I suppose they give me separate screw terminals I can connect to the RJ45 breakout board, so it keeps solder points free for the power lines but that's about it.EDIT: Indeed I was planning on gathering all these modules together and connecting to something running Linux, which would give ethernet access to the whole arrangement. I think it's safe to assume whatever that is will have plenty of IO, it may be Pi + Arduino, just because I find the Pi IO voltage rather limiting.

author: penfold
author_rank: Frequent Contributor
timestamp: March 11, 2022, 08:37:14 am
content: Quote from: bifferos on March 11, 2022, 12:44:45 amInteresting. If its just a reboot it needs, then you can always use a relatively long duration watchdog timer that the host resets with a regular ping. I'm not sure I've left an arduino ethernet board running that long, not had much problem with the ENC28J60 itself; it may be something to do with the arduino library.

author: Ian.M
author_rank: Super Contributor
timestamp: March 11, 2022, 09:58:16 am
content: IIRC RS485 and CAN don't support terminated branches or stubs - only the bus ends should be terminated and unterminated stubs need to be kept short to avoid severely limiting the max data rate.OTOH with a whole spare 4 pair cable to use per endpoint, you can use two pairs (out and back) for data to convert your star topology to a bus, looping out to each endpoint from your central hub location. That puts CAN bus and RS485 back on the table.The central hub can be pretty dumb as the bus master is just another node, so it could even be built without a PCB by punching jumper wires down to the back of a patch panel looping the bus from each jack to the next, and handling the power distribution via an automotive fusebox to feed individually fused power to each jack. Unused jacks would need a loopback plug to complete the bus. You would need terminators for the ends of the bus, but that's just a few resistors on perfboard, jumpered to the two end jacks in the patch panel.'Generic' RS485 transceivers are currently far far easier to find in stock than CAN bus ICs, of which nearly 90% are currently out of stock at Mouser. Worst case you might have to put a couple of footprints on your node PCBs to ease transceiver substitutions. Everything else you need for RS485 can be handled by the AVR UART + an I/O pin for TX enable.AVRs themselves are more of a problem, with nearly 70% out of stock at Mouser and few if any of the more popular ones in stock (e.g. no ATmega328 variant is available). However it seems that Chinese Arduino modules are still reasonably available from suppliers with a high probability of actually delivering, so I'd probably design the sensor nodes around an Arduino Pro Mini, mounted as a daughterboard (that's the ATmega328PB one *without* a USB I/F) footprint, but within the footprint put tracks and pads to let you directly mount an ATmega328PB if you can get any.Note: I'm using Mouser's stock levels as a proxy for general distributor availability because they make it easy to filter by 'in stock' and by 'normally stocked' so gathering statistics doesn't involve mass scraping of individual product pages.

author: Psi
author_rank: Super Contributor
timestamp: March 11, 2022, 11:16:10 am
content: You can actually bit-bang 10Mbit ethernet for simple on/off control using an ATTINY85 with no other parts.Everything is hardcoded for speed, but it works.https://hackaday.com/2014/08/29/bit-banging-ethernet-on-an-attiny85/Do I recommend it, definitely not, but just FYI and for funThe cheapest option is probably go wifi route with an ESP8266.The more fun/interesting option where you learn stuff is probably some kind of differential comms.Just be prepared for a world of pain if you roll your own multi-node protocol. Or maybe a world of fun if you enjoy that.(Some AVR have a multi-node hardware protocol built in, i forget how it works, i think its UART with hardware detection if a packet is addressed to the MCU or not. Might save some time.

author: bifferos
author_rank: Regular Contributor
timestamp: March 12, 2022, 12:38:18 am
content: Quote from: penfold on March 11, 2022, 08:37:14 amThe very common ENC28J60 module is the one I didn't try:https://hobbycomponents.com/wired-wireless/111-enc28j60-ethernet-lan-network-module.Maybe I should give it a go.I used one of these:https://www.amazon.co.uk/diymore-ENC28J60-Ethernet-Arduino-Webserver/dp/B01LANEBAA,and the Wiznet shield for the Uno. Neither of these were geniune shields so they could have been faulty.Quote from: Psi on March 11, 2022, 11:16:10 amI saw that in my searching! It's right up my street. You're talking to the guy who wired a Z8 microcontroller (could have used an AVR, but hadn't discovered them at that time) to a 3c509 ISA ethernet card using an ISA slot I got from an old motherboard and some stripboard and it actually worked!. That certainly finds a use for my RJ45 breakout boards. Crazy stuff.Quote from: Ian.M on March 11, 2022, 09:58:16 amThis is a really interesting idea, and takes me back to the days of thin ethernet. I remember wiring a building with it. Everything stops working with a break in the bus, a good and bad thing at the same time. Making use of the extra cabling in this way to save having point-to-point connections at the 'hub' makes the overall controller far simpler as it's just another node. Nice idea.But I think in the end I may go the 1-wire route. There are slave libraries that I can investigate. I take three lines for that, then leave the other 5 lines as interrupts. I setup individual nodes to choose which interrupt lines to use, and share them in much the same way as an old PC ISA bus did. So my controller needs to know what devices are using what interrupts, but it does give me the ability to respond quickly to button presses on the nodes. Either I slow poll at regular intervals (for stuff like temperature), or when an interrupt line is pulled low, depending on the device I do a read of some register over w1. I may need to read more than one slave device if the interrupt line is shared to find the source of the interrupt.This is quite nice because some nodes will only be temperature, basically an rj45 breakout board with DS1820, maybe even a hacked ethernet cable + DS1820. One node (the doorbell) could be just a dedicated interrupt line and nothing else. It worked (after a fashion) in the early PCs, so could work for me.Using this I can use one of these bus boards to connect all the cables together:https://www.amazon.co.uk/Electronics-Salon-RJ45-9-Way-Board-Connector/dp/B00U2D2GP2And I've only got to find 8 IO ports on my arduino to control the whole thing. Perhaps I can even get away without resistors because I can use the internal pull-ups for my interrupt lines.If the above is inadequate, I think I will investigate some kind of RS485/CAN bus system, looping pairs to make a bus.Thanks for all the replies, a lot of good info here, as ever on this board.

author: Renate
author_rank: Super Contributor
timestamp: March 13, 2022, 06:31:27 pm
content: I think that too many people grab instantly for wireless. In a fixed application I'd rather save the spectrum for something more useful.I've been running a MODBUS (RS-485) system on ATMega32u4's for years now.It works fine. I just connect wires without regard to topology. Ok, my distances are small. If you have bigger distances some thought should be in order.CAN is fine, but I wouldn't bother without hardware that supports it. Ok, you can name any protocol and a Z80 can support it, but it's an uphill slog.

author: max_torque
author_rank: Super Contributor
timestamp: March 14, 2022, 09:36:54 pm
content: The simple "fix" to enable a star topology on a linear network (ie CAN or RS485) is to provide your wiring harness and units with an OUT and RETURN, so each unit effectively sits in the middle of a linear bus!You can use simple plugs with 120 ohm termination resistors in them to physically select/terminate the "ends" of your bus as an where you see fit

author: Doctorandus_P
author_rank: Super Contributor
timestamp: March 15, 2022, 06:51:07 pm
content: Yet another vote for either RS485 or CAN transceivers.The're just small 8 pin chips that turn UART signals into a differential pair.I am aware of the ENC28J60 and thw Wiznet 5600, but those need far more complex hardware and software.There are plenty of networking libraries you can use with RS485 or CAN.A little search in platromio from the command line shows 57 libraries for "rs485, a lot of them are based around modbus.--------------------me@home:~$ pio lib search rs485Found 57 libraries:--------------------For the power supply:Long cables have a significant DC resistance, and will therefore drop voltage over their length (on both the "power" and the "GND" wire!)Therefore it is best to start with a high voltage, (for example 24V) and then use small SMPS modules at your network nodes to make the supply voltage for your microcontrollers.Also read the "10 ways to bulleproof RS485" Application notehttps://html.duckduckgo.com/html?q=10+ways+to+bulleproof+RS485Also note that 100Mbit/s Ethernet only uses 4 wires.This means you can use an CAT5 cable for all of:1. One pair for 24Vdc.2. One pair for RS485 data.3. Two pairs for regular 100MBit/s Ethernet.CAT5 cable and connectors can handle up to about 1Amp of current, and a small Linux computer only needs about 1A@ 5V, and with an SMPS that is about 200mA from 24V.This means you can have a central power supply, a few Linux computers and a whole lot of smart sensor nodes all from the same CAT5 cable.You do need to regard the topology of your network. Ethernet needs switches while a star configuration sort of works for RS485 if the baudrate is low (and your transceivers are slew rate limited), but a star topology should be avoided for RS485 (and CAN).RS485 only defines signal levels (the lowest layer of the OSI model), while CAN defines a complete network stack.AVR controllers do not have a built in CAN hardware peripheral, and doing it in software would be cumbersome.You can just use CAN transceivers with normal UART though.

author: westfw
author_rank: Super Contributor
timestamp: March 17, 2022, 07:00:50 am
content: Unless you have a very big house, your wiring is compatible with "UART + Driver" circuits (which includes RS485, but also plain old RS232.) RS232 is ... extremely widespread and well understood, even if it's recently been overshadowed by "logic level UART protocols." All sorts of "upper level" protocols are common...

author: rteodor
author_rank: Frequent Contributor
timestamp: April 13, 2023, 06:45:34 am
content: While searching if RS485 over Ethernet cables matches in impedance I stumbled over this thread. I am basically in the same situation as OP and I want to share my ideas, maybe they are of use or maybe somebody have something to criticize.The wires are already in place in what I call "pseudo-star" topology. On some of the legs there might be more than one board with sensors, relays etc.So I put a pair of RJ45 and a pair of jumpers so I can configure each board depending if its an intermediate or end board on the star's leg. Everything will be cabled with standard straight Ethernet wires that are easy to test, replace or extend.

author: Siwastaja
author_rank: Super Contributor
timestamp: April 13, 2023, 09:49:30 am
content: CAN or RS485 transceiver IC is a "very simple component", IMHO.CAN is only a simple solution if you use microcontroller with built-in CAN peripheral, like AT90CAN something. If you insist on using AVR, I would not use CAN if I had a full choice over the system because you would probably want to use some of the more popular AVR chips, which do not have CAN.RS485 would work with the UART peripheral you already have available in nearly every AVR MCU. You can come up with a very simple protocol where one master sends poll messages and slaves have unique addresses so they know which message to reply to.If even RS485 transceiver IC is "too much hardware", then I don't know what to suggest. Direct signalling from the AVR IO pins simply is not electrically robust enough in such large networks.

author: westfw
author_rank: Super Contributor
timestamp: April 13, 2023, 04:08:19 pm
content: Someone recently pointed out that LVDS theoretically supports higher speeds and longer cables than rs232, as well as multidrop. Transceivers are pretty cheap, too - comparable to rs232.

author: T3sl4co1l
author_rank: Super Contributor
timestamp: April 13, 2023, 08:24:41 pm
content: LVDS isn't nearly as robust as RS-485; I'm not aware of any receivers with common mode range wider than supplies. And it's not good for as much distance, given the small signal level; but that's a tradeoff with bitrate which is also much higher, and yeah not hard to overcome RS-232 range for given bitrate. For most commercial purposes, you'll want to use it in shielded cable, hence, USB, HDMI, etc. (roughly speaking).That said, actually used it once (not by choice..) over twisted-pair ribbon, in an industrial power application, which shockingly, worked...Tim

author: pqass
author_rank: Super Contributor
timestamp: April 13, 2023, 10:43:20 pm
content: See attached topology diagram. You really can't screw it up as the diodes protect the slave transmitters.Use one pair for  DC power and another pair for RS232 TX, and RX; providing full-duplex.Speed is dependent on the strength of the pull-down resistor which can be supplied from the MAX232 (pin 6; -10V) in the transmitter.The master can poll but slaves may also generate a break signal (holding line low >2 characters) to get attention.All messages should be validated via CRC.See other topologies here:http://www.sampson-jeff.com/article/interface/int.htm

author: barshatriplee
author_rank: Regular Contributor
timestamp: April 14, 2023, 09:07:55 am
content: You can see this:https://forum.digikey.com/t/can-example-atmega32m1-stk600/13039This is about the built-in CAN controller of the ATmegaxxM1 family of microcontrollers. This project achieves CAN communication between multiple ATmega32M1 devices using STK600 development boards. CAN Libraries for ATmegaxxM1 devices in Studio 6 are provided as well.

author: JustMeHere
author_rank: Frequent Contributor
timestamp: April 14, 2023, 08:22:12 pm
content: ESP32 makes this all real simple.