Go Down

Topic: Serial Communication between two Arduino Uno's (Read 190 times) previous topic - next topic

JanKnipper

Hi all, I am a novice in programming Arduino's.

I am trying to use Arduino Uno's to control my model moving bus system (TomyTec). These buses are self driving and are started/stopped by magnets in the road surface. I move a small magnet in the path of the bus or away from the path to stop the bus or to let it go. The magnet is fitted in the arm of a small servo. There are too many servo's to connect to one Arduino so I have two and in future maybe even three Arduino's. So for now  I have two Arduino's.
The Arduino's control the buses with a sort of block system. I am trying to have two Arduino's communicate to exchange block occupancy data.

The program acts like a PLC program and cycles fast without delays to read sensors, execute the logic and write to servo's every cycle.

I first tried to have the two Arduino's communicate using SoftSerial. So SoftSerial sends data every cycle (every 10 mSec or so).
The Servo's went completely crazy and did not stop hunting. After a lot of trying I found that SoftSerial interferes with Servo control. Later I found similar stories in this forum.
I fixed the problem somewhat by only sending a new byte through SoftSerial when something actually changed (in my case that will be every minute or so as the buses move around).
To receive data, it seems that Servo's are not impacted. Not sure.

Alternatively I may have to use the hardware Serial port. However I wonder how to debug the program since the Serial comms are shared with the USB port.  Also when a third Arduino is added to the project, I will need a second serial port anyway.

Does any of you has an alternative solution?
Many thanks!

sterretje

There are alternatives to SoftwareSerial; AltSoftSerial and NeoSWSerial. They might interfere as well (or might not), never looked into that.

Instead of Unos, you can use Arduino's with multiple serial ports. On 32U4 based boards, the HardwareSerial (Serial1) does not interfere withe the USB.

Depending on distance, you can use I2C for communication. One Arduino as master, the others as slave.
You can also look at serial bus systems; e.g RS485 allows to connect multiple serial devices (usually in master/slave configurarion).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

cattledog


Robin2

I am building a system with a similar communication requirement for controlling a model railway. I am using a Mega as the main controller because it has spare HardwareSerial ports. My "slave" units are Atmega328s on veroboard and on them I use Serial for communication with the Mega as they do not normally need to send debugging information. I use my own very limited form of software serial for the occasional debug messages.

The communication between the Mega and the slaves uses a system that is pretty much identical to the 3rd example in Serial Input Basics. You can send data in a compatible format with code like this
Code: [Select]
Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker


...R
Two or three hours spent thinking and reading documentation solves most programming problems.

JanKnipper

Many thanks for y'all prompt replies!  Very helpful indeed.

I will first explore the I2C option. The distance is only a few meters. Problem is that the I2C functionality seems to be dedicated to A4 and A5 ports. I am already using 5 analog input ports so one port short!

For Servo power distribution purposes I am using the GPIO expansion shield from DFRobot. It seems to have another UART port but also something called IIC port.  I'll need to look into the possibilities offered by these.

If any of you is familiar with the GPIO expansion shield from DFRobot, please let me know any tips you might have!

Many thanks
Jan

Robin2

I will first explore the I2C option. The distance is only a few meters.
I think I2C is intended for distances measured in centimetres.

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

Depending on distance, you can use I2C for communication. One Arduino as master, the others as slave.
Emphasis added

I will first explore the I2C option. The distance is only a few meters. Problem is that the I2C functionality seems to be dedicated to A4 and A5 ports. I am already using 5 analog input ports so one port short!
Wrong choice ;) Unless you want to add 'repeaters'.
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

JanKnipper

Thanks Robin and Sterretje,
I will forget about the I2C option.

Next option is the UART port connections provided on the Expansion Shield I am using (GPIO Shield for Arduino V1.0 SKU: DFR0334).  However I have difficulty ( = cannot) finding how this port should be used. It may even be just a repeat of the pins 0 and 1.

Jan

Robin2

Please post a link to the datasheet for the shield

...R
Two or three hours spent thinking and reading documentation solves most programming problems.

sterretje

If it's https://www.digikey.se/product-detail/en/dfrobot/DFR0334/1738-1138-ND/6588560, I don't think that it's going to help you with the serial communication. There are no additional UARTs on that board so all TX/RX pins that you see will be the same as the Uno pins 0 and 1 (as you already suspected).
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

JanKnipper

Thanks a lot Sterretje!

Indeed, that's the shield I am using.
I will do some more experimenting on how to get it going. I suppose I will end-up using the Arduino built-in UART after I am done with debugging the rest of the program.
You mentioned to use RS485 bus to extend things to another Arduino. Do you know of separate shields for RS485 ?

Thanks
Jan

arpeggio

Hello, you can also use the PJON-way to communicate between Arduino's.
Its fast reliable and easy to use, just download the library in the library manager (under sketch/include library)
and you get lots of examples
Dian from the Netherlands

sterretje

#12
Dec 08, 2018, 02:09 pm Last Edit: Dec 08, 2018, 02:10 pm by sterretje
You mentioned to use RS485 bus to extend things to another Arduino. Do you know of separate shields for RS485 ?
To give you an idea what you're looking for: arduino rs485 breakout

Note that a breahout board is not a shield.

And here is a shield
If you understand an example, use it.
If you don't understand an example, don't use it.

Electronics engineer by trade, software engineer by profession. Trying to get back into electronics after 15 years absence.

Go Up