Aug 28, 2018 The Arduino Mega 2560 is a replacement of the old Arduino Mega, and so in general reference, it will be called without the ‘2560’ extension. It is usually used for very complex projects. Arduino Mega 2560 Specifications. Arduino Mega – Basic Features. Teensy and Teensy++ have one HardwareSerial port which is available (not used for uploading or the Arduino Serial Monitor). Arduino Mega has 3 extra.
Software Serial Example
Arduino and Genuino boards have built in support for serial communication on pins 0 and 1, but what if you need more serial ports? The SoftwareSerial Library has been developed to allow serial communication to take place on the other digital pins of your boards, using software to replicate the functionality of the hardwired RX and TX lines. This can be extremely helpful when the need arises to communicate with two serial enabled devices, or to talk with just one device while leaving the main serial port open for debugging purpose.
In the example below, digital pins 10 and 11 on your Arduino or Genuino boards are used as virtual RX and TX serial lines. The virtual RX pin is set up to listen for anything coming in on via the main serial line, and to then echo that data out the virtual TX line. Conversely, anything received on the virtual RX is sent out over the hardware TX.
Hardware Required
- Arduino or Genuino Board
Circuit
There is no circuit for this example. Make sure that your Arduino or Genuino board is attached to your computer via USB to enable serial communication through the serial monitor window of the Arduino Software (IDE).
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Schematics
image developed using Fritzing. For more circuit examples, see the Fritzing project page
Code
See also
- TwoPortReceive – Two serial ports that receive data switching from one to the other one when a special character is received.
- MultiSerialMega - Use two of the serial ports available on the Arduino and Genuino Mega.
- Serial Call Response - Send multiple vairables using a call-and-response (handshaking) method.
- Serial Call Response ASCII - Send multiple variables using a call-and-response (handshaking) method, and ASCII-encode the values before sending.
Last revision 2018/05/17 by SM
Introduction
It is possible to chain Arduinos together in such a way as to get communication between the two. Having Arduino-Arduino communication can be useful for many projects, such as having one Arduino to run motors and having another sense the surroundings and then relay commands to the other Arduino. This can be done in several methods, using I2C and Serial, to list a few.
This tutorial will focus on Arduino-Arduino communication through the serial ports (RX and TX).
Schematic
The schematic below shows how to connect the two Arduinos together. This shows two Unos, but if a Mega is used, it can be connected to any of the Serial ports on the Mega as long as that is accounted for in the code.
There has to be a common ground between the two or else it will not function properly. Also, note that TX goes to RX and RX goes to TX.
Coding
When sending things through serial, everything is sent in bytes. These bytes are then read one byte at a time by the other Arduino. When it is just characters being sent through the serial, it is relatively easy to convert from characters to bytes. However, if there are both characters and numbers are going through, this can lead to messing up the data because a number and a character can have the same byte value, but that does not make them the same. Numbers are also tricky because they may not actually fit in the byte.
Simple Code
The easiest way to get around this is to try to avoid using characters and numbers at the same time. This can be done by sending one character across, each with a different meaning. A good example of this comes from the Arduino Physical Pixel tutorial.
Upload the Physical Pixel code, which can be found in the Arduino IDE under: File >> Examples >> Communication, onto one Arduino.
On the other Arduino, upload:
When this is run, the LED attached to Pin 13 on the Arduino with the Physical Pixel code should flash on and off at a frequency of 0.5 Hz. To make sure this is actually the code doing that, the delays can always be changed in the above code.
In this code the job of 'H' was to turn an LED on and the job of 'L' was to turn the LED off. This can easily be applicable to getting various characters triggering more reactions.
However, depending on the application, this may not be enough and more drastic code may be required.
![Arduino mega serial pins Arduino mega serial pins](http://www.sdrobots.com/wp-content/uploads/2014/12/ArduinoSerial_bb.jpg)
Complex Code
For sending data from one Arduino to another in a form that cannot be simplified, there are other options. One option is to turn everything sent from the Sender Arduino into characters and then have the Receiver Arduino read in the characters. The data is actually sent as bytes, but the Arduino can convert from characters to bytes and vice versa.
Sender Code
The sender code changes characters into bytes and, if necessary, it changes number values into characters before turning it into bytes. Below is a sample of the Sender code:
Receiver Code
The receiver will then receive the byte array from the other Arduino and interpret it there. Below is the code for the receiver. Note that this code is intended for a Mega since it will interpret the data received from the other Arduino and then print to the Serial Monitor what it received so that the user can check it. This debugging can be avoided by using an Uno and then printing what was found onto an LCD screen that uses I2C communication.
There is one flaw with this program as it is now. It results in only a character array, so if the integers were desired, they are lost without further work on the data.
Further tutorials have been made to show how this data may be manipulated by splitting strings or getting floats from the character arrays.