Introduction to Serial Communication
Serial communication is a fundamental method for data exchange between devices. In this blog post, we will explore how to set up serial communication between two Arduino boards. This is a crucial skill for projects where multiple Arduinos need to share information.
Setting Up the Hardware
To establish serial communication between two Arduino boards, you will need two Arduino boards and jumper wires. Connect the A0 pin of the Master Arduino to the A1 pin of the Slave Arduino, and vice versa. Additionally, connect the ground (GND) pins of both boards to ensure a common ground.
Master (Transmitter) Arduino | Slave (Receiver) Arduino |
A0 | A1 |
A1 | A0 |
GND | GND |
Writing the Arduino Code
After setting up the hardware, the next step is to write the Arduino code. For the transmitting Arduino, use the mySerial.begin()
function to initialize serial communication and the mySerial.print()
function to send data. Similarly, for the receiving Arduino, use mySerial.begin()
to initialize serial communication and mySerial.read()
to receive data.
In the example show below master arduino sends letter ‘H’ and ‘L’ to slave arduino using mySerial.print()
function. At the same time Serial.print()
function prints letter ‘H’ and ‘L’ in serial monitor of master arduino. Using pins other than Rx Tx of arduino for serial communicaton allows us to communicate serially as well as use serial monitor of arduino.
At receiver end, arduino receives letter ‘H’ and ‘L’ using function mySerial.read()
and prints it to serial monitor of slave Arduino using function Serial.print()
.
Master (Transmitter Code):
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A0, A1); // RX, TX
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
Serial.print('H');
mySerial.print('H');
delay(1000);
Serial.print('L');
mySerial.print('L');
delay(1000);
}
Slave (Receiver Code):
#include <SoftwareSerial.h>
SoftwareSerial mySerial(A1, A0); // RX, TX
int incomingByte;
void setup()
{
Serial.begin(9600);
mySerial.begin(9600);
}
void loop()
{
if (mySerial.available() > 0)
{
incomingByte = mySerial.read();
if (incomingByte == 'H')
{
Serial.print('H');
delay(1000);
}
if (incomingByte == 'L')
{
Serial.print('L');
delay(1000);
}
}
}
Troubleshooting Common Issues
When setting up serial communication between two Arduino boards, you might encounter some common issues. Ensure that your baud rates match on both Arduinos. Double-check your wiring connections to make sure TX and RX pins are correctly connected. Finally, verify that your code is uploaded correctly to both boards.
By following these steps, you can successfully set up and troubleshoot serial communication between two Arduino boards. This opens up numerous possibilities for advanced projects involving multiple microcontrollers.
Checkout how to Shrinkify Your Arduino Projects https://ihrprojects.com/shrinkify-your-arduino-projects-arduino-as-isp/
Visit our YouTube channel https://youtube.com/@ihrprojects