Serial Communication Between Two Arduino Boards
Thursday, April 8, 2021
Hello everyone. In this article, you will find answers to your calls such as communication between two arduino's, and two arduino's serial communication example.
You may want to communicate with two arduino. This may be due to a shortage of input/output pins.
I will use two arduino uno models in this project. You can communicate between any two arduino models you want. Let's take a look at how to communicate step by step between two arduino's.
Step 1: Required Components
- Arduino UNO Board x 2
- Jumper Wires
Step 2: Circuit Diagram
Make a circuit as per the given diagram. Connect both the RX and TX pins of Arduino vice versa (first Arduino’s TX pin to another’s RX pin and first Arduino’s RX pin to another’s TX pin). Also, common the ground pin of both Arduino.
Step 3: Let's Write the Codes
Here we need to write two different codes for the receiver arduino and the sending arduino.
Code For Sender Arduino
- First I made the character definition required to send the word "Hello" to the recipient.
- I started serial communication at 9600 baud rate.
- In the main loop, I have ensured that the word "Hello" is sent with 1 second intervals.
char mydata[5] = "Hello"; //String data. void setup() { //Begin the Serial at 9600 Baud. Serial.begin(9600); } void loop() { Serial.write(mydata,5); //Write the serial data delay(1000); }
Code for Receiver Arduino
- I have defined an empty array to store the retrieved data.
- I started serial communication at 9600 baud rate. Communication speed must be the same on both sides.
- Using Serial.readBytes, I read the data from the sending Arduino.
char mystr[10]; //Initialized variable to store recieved data void setup() { ///Begin the Serial at 9600 Baud. Serial.begin(9600); } void loop() { Serial.readBytes(dizi,7); //Read the serial data and store in var Serial.println(dizi); //Print data on Serial Monitor delay(1000); }
Upload the codes to arduino. It's that easy to communicate with two arduino.