Understanding Serial Communication Between Two Arduino Boards

Introduction to Serial Communication

Setting Up the Hardware

Writing the Arduino 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);
}
#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

Leave a Reply

Your email address will not be published. Required fields are marked *