Arduino communications

Hello, I'm working on project that need an arduino mega 2560 to communicate with PC via USB and control a 3.5 TFT lcd and do other things(SD card read/write, RTC, etc..) all in one project box.
also i have another two arduino nano each in one separate project box, and each one has it's own functions.
i need the arduino mega2560 to communicate with PC and ask each arduino nano to get some useful informations from it, so Arduino mega act as a master and two arduino nano as a slave devices.
the distance between all arduinos is no more than 0.5 m.
my question is: what is the communication type between the arduinos is the best for my case?

Serial would be easiest but what do you mean by best ?

the best communicationtype depends on the speed and the amount of data that shall be transferred.

It sounds like you are sending every few seconds a short command and get a short answer. And it doesn't matter if the answer to one of the slave rushes in a second earlier or later.
But you have to describe this in detail.

If the description above is right then serial communication via the hardware-uarts of the arduino mega will do
or even some software-serial connections.

If it is possible this way depends on what the Arduino Mega 2560 has to do if he is most busy.
driving stepper-motors needs a real tight timing. That conflicts with communicating huge data-amaounts over three serial interfaces.

So gives an overview of what each device is doing

best regards Stefan

since the Mega has 4 serial interfaces, you could have separate (4) interfaces between the Mega, other Arduinos and PC. over such a short distance, you could directly connect the TX/RX pins (+ Gnd) between the Arduinos.

another approach, is to use the USB/Serial interface to the PC and a single RS-485 interface between the Arduinos. a 2nd serial interface on the Mega would be used for the RS-485 interface. when using 485, the TX needs to be disabled in receive mode.

A master would poll each slave and wait a limited amount of time for a response from the slave before polling the next slave. a PC running Windows would typically not be able to respond as quickly to a poll (2 msec) as an Arduino. using the USB I/F for your needs was make it unavailable for diagnostics. USB/RS-485 adapters are inexpensive

This Arduino to Arduino I2C Tutorial may be of interest.

...R

Thanks for all replays, "the best" i mean i can use i2c, uart, spi.
my project is (Arduino power supply), arduino mega control 3.5 TFT LCD via 8 bit, RTC, SD card, BMS for 3S2P, monitoring voltage/current, the output of the power supply and short/reverse circuit protection,
the other arduino nano used like a DMM to monitor (A,V,O,L,C meter), and the last arduino nano is like (an electronic load device and a battery charger for 1S, 2S, and 3S battery, with voltage and current monitoring.
i need to send every 0.5 second a request to each arduino nano to get a short answer like "request : voltage... answer : 5.6 " so witch better? i2c or UART with separate serial port for each arduino nano?

Hi obaida,

i need to send every 0.5 second a request to each arduino nano to get a short answer like "request : voltage... answer : 5.6 " so I'll use arduino mega with separate serial port for each arduino nano to get informations.

I guess this is just a rough description where quite a lot detials were left out.

If you would like to have good advice you would have to be much more specific and give an overview of the whole thing.

From what you have said so far I would keep the messages as short as you can even if you don't have high-speed-requierements. (maybe you want to speed up things later)

request "Vo" // for voltage

float myVoltage

myVoltage = 5.6;
Serial2.print(myVoltage); // six bytes to send

But I would do it like this

int myVoltage
myVoltage = 56;  // scaled by factor 10 to have a integer 56 representing decimal-point-value 5.6
Serial2.print(myVoltage);  / just two bytes to send

best regards Stefan

all my sensors of voltage and current are i2c "INA219".
from my side i can say for short that every arduino nano make some measurements and do some calculations for a short time "in milliseconds" than set back for another 1 second to do the measurements again or to send back the measurements to arduino mega, if arduino mega sent a request.

so the main idea is I'm trying to make a communication system between arduinos that the speed is not important, the main goal is to make a communication system witch will work properly, and don't get two arduinos talking together at the same time and get uncorrected data.

why not read the i2c temperature sensors directly from the mega instead of thru a secondary arduino?

One big missing piece here is the physical layout and environment. Different communications types like serial, I2C and so on, have different driving impedances and other design aspects which make the distance between stations and the layout of wiring between them, very important differences.

For example, I2C is designed for high speed, multiple devices sharing a line, master-slave protocol in hardware. But although it can be extended to several meters with very careful wiring, it is not aimed at that, so performance degrades at long distances. On the other hand, consider RS485... relatively low speed, multiple devices, with a much, much greater noise immunity. So it can work well over distances up to 2 km in some circumstances.

There are a lot of more subtle aspects, like number of conductors, connector conventions, electrical safety and reliability etc., so you really need to do the research yourself, or else tell us more about the environment in which your system will be installed.

You did specify the distance as 0.5m so serial would be a good idea for you, the only question is how to make the electrical connections. What topology do you want? Star? Bus? You can do a multi-point serial bus with RS485 using polling, giving addresses to the slaves for example. But even TTL level serial can be set up to share lines, using diodes. It all depends on your requirements for reliability, cost, and system flexibility.

It would be helpful if you posted a system diagram, showing the master (I assume the one with the LCD?) and all the slave devices that you expect to ever connect to it, with approximate cable measurements attached. I say this because many people come here and describe a project and then later in the thread, add details of additional hardware that we didn't know about.

If you really only have 2 slaves, you could just connect them to 2 of the Mega serial port and be done. Technically, that would be the star configuration. In that case, the slaves could "talk at the same time" and the data would not be corrupted. In a bus situation, polling is usually the method chosen to avoid data collisions.

each arduino in separate project box can work alone like a completely separate device, also i want them to have the ability to communicate with each other if i want.
I'm thinking of making a bus communication that each arduino can but informations that will be available for arduino mega2560 "the main board" to receive and save it on sd card or send it to PC.
pcb design and cable types depends on witch communication system i want to make.
SPI is good but require 4 data lines, but i2c and UART require 2 data lines, speed is not important, the data per one request is no more than 6 bytes.

  • with each arduino i have I2C sensors for voltage/current and oled display.
    so is there a different between i2c and UART in my case?and witch the easier to program it with Arduino IDE?

Generally, bare CMOS logic signals are not used to communicate between widely separated units. You can do it, but it's not very robust. That is why line buffered wire protocols like USB, RS-232 and RS-485 exist.

Did you research I2C and serial? It's a huge waste of time to explain those here, they are well documented on the web. From the Arduino programming point of view, ease of programming does depend on application... I think I already suggested that serial would be easier for you.

I think reply #1 summarizes the situation quite well.

if use short commands that they can be hold in the serial buffer (64 bytes)
if you define a master who is the only one to initiate communication.
and if you do software-handshaking

the messages will not collide.

best regards Stefan

Thanks again for all replays, the final decision is to use UART as “aarg” suggested with bus system in witch arduino mega2560 control the communications in the bus so non of arduino nano can write anything to bus until arduino mega send request with unique id for each arduino nano.
RS-232 is better but it needs a specific converter ic wich i don’t have it right now.

a serial interface (uart) is not per se a bus-system.
Bussystem means all communication-partners use the same wires.
And the datatransfer-potocol includes adresses to indicate who is the adressant of a message.
If you would like to do this with serial you would have to implement your own "bus-protocol" with the adresses.

It will be easier to use multiple serial interfaces on the Arduino-Mega each serial interface exclusievly for one partner.

You wrote two Arduino Nanos. The Arduino Mega has 4 hardware-serial interfaces. SO up to four partners could communicate at high speed and without any danger of loosing data.

best regards Stefan

You can receive simultaneously from multiple UARTs without a collision, because the incoming data is serviced by an interrupt service routine, and buffered. But you might end up polling anyway just to simplify the main program…

Thanks "StefanL38", the safest is to make separate UART for each arduino nano.
I'll do just like that.