Learn to code

Arduino Home

Lessons 1
Arduino - Getting Started
Lessons 2
Arduino - Basics
Lessons 3
Arduino - Serial Communication
Lessons 4
Arduino - Digital / Analog
Lessons 5
Arduino - Visual Output
Lessons 6
Arduino - Motor Control
Lessons 7
Arduino -LCD Displays
Lessons 8
Arduino -LCD Displays

Serial communication between two Arduino Boards

in Arduino Serial Communication

This tutorial will teach how to send and receive data from one Arduino Board to another Arduino Board. We will blink an LED on an Arduino Uno Board that a second Arduino Uno controls.

In the previous post about the various commands for serial communication, commands are explained that are used in this tutorial.

 In most of the examples, I will use Arduino Uno boards since they are commonly used. However, you can use any board you would like. The technique and commands are the same.

For sending and receiving data, a master and slave device is necessary. Keep in mind that the master sends data and the slave receives data.

Parts you will need

Arduino Uno Rev3 Arduino Uno Rev3 × 2
LED LED × 1
220 ohm resistor 220 ohm resistor × 1
Dupont Wires Dupont Wires × 6
Arduinoplatform is a participant in several affiliate programs. This means that I will earn a commision if you buy a product from the affiliated websites through clicking on the links provided above.

BreadBoard Layout

TM1637 Display Connections

Arduino Master BoardArduino Slave Board
Pin 0 (RX)pin 1 (TX)
pin 1 (TX)Pin 0 (RX)
GND GND
LED to pin 2

Master Code

/*Sketch: Serial Communication between two arduino boards
*/
void setup()
{
  Serial.begin(9600);

}

void loop()
{
  Serial.print(1);
  delay(2000);
  Serial.print(0);
  delay(2000);
}

Slave Code

// Slave: Serial Communication between two Arduino Boards
 
char number  = ' ';
byte LED = 2;
 
void setup() 
{
   pinMode(LED, OUTPUT);
   Serial.begin(9600);
   Serial.println("START");
}
 
 
void loop()
{
   if(Serial.available())
   {
      char number = Serial.read();
      if (number=='0') { digitalWrite(LED, LOW); }
      if (number=='1') { digitalWrite(LED, HIGH); }
      Serial.println(number);
   }
 
}

Code Explanation

The code above is quite simple. The master sends a number to the slave. The master sends #1 first, then waits 2 seconds before sending #0, and starts over again.

One significantly important aspect of using Serial.print() is that numbers are converted into ASCII characters. This means that the master is transmitting the values 48 for 0 and 49 for 1.

The slave Arduino will start by printing a “START” message and then keeps checking incoming data in the loop();

Firstly, a check is made whether there is any data in the buffer.

if(Serial.available())

If there is data in the buffer, a single character is read and copied to the char variable called “number”.

char number = Serial.read();

Since we are only interested in “0” and “1” the next step is to check whether or not the buffer contains these values. An If statement is a suitable solution. The first if statement sends a low signal to the LED connected to pin 2 of the slave board. If the slave board receives a 1 it will send a HIGH signal to pin 2.

if (number=='0') { digitalWrite(LED, LOW); }
      if (number=='1') { digitalWrite(LED, HIGH); }

The 0 and 1 are inside single quotes. Since we are using a char variable and we want to check a single character. You can learn more about when using single quotes or double quotes in the character string lessons.

Since the code checks a char character, we could also use the actual value of the character to check the if statements.

if (number==48) { digitalWrite(LED, LOW); }
if (number==49) { digitalWrite(LED, HIGH); }

Checking the actual ASCII values does not make things easier to read.

What happens if the master device sends characters that are not equal to 0 or 1. Well, the master device can send those values, but the slave board will only act on the 0 and 1

Previous Post
Introduction of serial communication
Next Post
Receiving and Sending multiple characters with serial communication
You must be logged in to post a comment.
Menu