Master-Slave Communication Between Two Arduino Boards Using UART

When an embedded project has to handle sensors, displays, motors, and communication at the same time, using a single microcontroller can make the hardware and software more complicated. A practical approach is to divide the workload between multiple microcontrollers and allow them to communicate with each other.

In this project, two Arduino UNO boards communicate using UART (Universal Asynchronous Receiver/Transmitter). One Arduino works as the Master, while the other acts as the Slave. The Slave Arduino collects temperature and humidity data from a DHT11 sensor and reads an analog value from a 10 kΩ potentiometer. The Master requests this information, displays the readings on a 16×2 LCD, controls the speed of a DC motor, and sends commands to control an LED connected to the Slave.

This project demonstrates how a simple UART command-response system can be used to divide sensing, processing, and control tasks between two Arduino boards.

 

Understanding Master-Slave Communication

In a master-slave architecture, the Master Arduino controls the communication process. It sends a command to the Slave, and the Slave performs the requested operation before sending a response back.

In this example, the Master can request temperature, humidity, and analog data from the Slave. It can also send commands to switch the Slave-side LED ON or OFF.

The Slave is responsible for acquiring the sensor information. It continuously reads the DHT11 temperature and humidity values and the analog value from the potentiometer, then stores the latest readings. When the Master requests a particular value, the Slave retrieves the corresponding data and transmits it through UART.

UART communication between the two Arduino boards requires three connections: TX, RX, and GND. The TX pin is used to transmit data, RX receives data, and GND provides the common voltage reference required for communication.

 

UART Connection Between Two Arduino Boards

The hardware UART pins of the Arduino UNO are used for communication.

The connections are:

Master Arduino Pin 0 (RX) → Slave Arduino Pin 1 (TX)

Master Arduino Pin 1 (TX) → Slave Arduino Pin 0 (RX)

Master Arduino GND → Slave Arduino GND

The TX and RX lines are crossed because the transmitter of one Arduino must connect to the receiver of the other.

The grounds of both Arduino boards must be connected together. This common ground is important because UART signals are interpreted relative to the ground reference.

 

Master Arduino Connections

The Master Arduino is responsible for the LCD interface and motor control.

16×2 LCD Connection

The 16×2 LCD is connected to the Master Arduino in 4-bit mode. In this configuration, only LCD data pins D4 through D7 are used.

The LCD connections are:

LCD Pin 1 (VSS/GND) → GND

LCD Pin 2 (VDD/VCC) → Arduino 5V

LCD Pin 3 (VEE/Vo) → Variable pin of 10 kΩ potentiometer

LCD Pin 4 (RS) → Arduino Pin 12

LCD Pin 5 (R/W) → GND

LCD Pin 6 (E) → Arduino Pin 11

LCD Pin 7 (D0) → GND

LCD Pin 8 (D1) → GND

LCD Pin 9 (D2) → GND

LCD Pin 10 (D3) → GND

LCD Pin 11 (D4) → Arduino Pin 9

LCD Pin 12 (D5) → Arduino Pin 8

LCD Pin 13 (D6) → Arduino Pin 7

LCD Pin 14 (D7) → Arduino Pin 6

LCD Pin 15 (LED+) → VCC through 220 Ω resistor

LCD Pin 16 (LED−) → GND

The 10 kΩ potentiometer connected to the LCD's VEE/Vo pin is used to adjust the display contrast.

 

L293D Motor Driver Connection

Arduino pin 3 is connected to the Enable 1 pin of the L293D motor driver. A PWM signal from this pin is used to control the motor speed.

 

The L293D connections are:

L293D Pin 1 (Enable 1) → Arduino Master Pin 3

L293D Pin 2 (Input 1) → 5V

L293D Pin 3 (Output 1) → Motor

L293D Pin 4 (GND) → GND

L293D Pin 5 (GND) → GND

L293D Pin 6 (Output 2) → Motor or N/C

L293D Pin 7 (Input 2) → GND

L293D Pin 8 (Motor Supply) → External 5V power supply

L293D Pin 9 (Enable 2) → N/C

L293D Pin 10 (Input 3) → N/C

L293D Pin 11 (Output 3) → N/C

L293D Pin 12 (GND) → N/C

L293D Pin 13 (GND) → N/C

L293D Pin 14 (Output 4) → N/C

L293D Pin 15 (Input 4) → N/C

L293D Pin 16 (VCC) → Arduino Master 5V

 

Pin3 of the Arduino Uno is used to control the Enable pin of the L293d IC. The motor is connected to the Pin 3 and Pin 6 of the L293D IC.

The motor is powered from an external 5V supply through the L293D rather than directly from the Arduino. This is important because a DC motor can draw significantly more current when starting or operating at higher speeds, which can cause the Arduino supply voltage to drop and potentially reset the board.

The ground of the external motor supply should also be connected to the common ground of the Arduino system.

 

Slave Arduino Connections

The Slave Arduino handles the DHT11 sensor, potentiometer, and LED.

The UART connections are:

Slave Arduino Pin 0 (RX) → Master Arduino Pin 1 (TX)

Slave Arduino Pin 1 (TX) → Master Arduino Pin 0 (RX)

 

The DHT11 is connected as follows:

DHT11 Data → Slave Arduino Pin 2

 

The potentiometer is connected as follows:

Potentiometer variable pin → Slave Arduino A0

Potentiometer one side → Slave Arduino 5V

Potentiometer other side → Slave Arduino GND

 

The LED is connected as follows:

LED positive side → 5V

LED negative side → Slave Arduino Pin 8

 

The Master controls this LED by sending ON or OFF commands to the Slave.

 

Finally, connect the grounds of both Arduino boards and the external 5V motor power supply together:

Master Arduino GND → Slave Arduino GND → External 5V Supply GND

 

The Slave Arduino reads the temperature and humidity values from the sensor and keeps the latest readings available for the Master.

 

How the Analog Input Works?

A 10 kΩ potentiometer is connected to the Slave Arduino's A0 analog input. The potentiometer produces a variable voltage depending on its position.

As the potentiometer is rotated, the voltage applied to A0 changes. The Arduino's ADC converts this voltage into a digital analog reading.

The Slave periodically reads this value and stores it. When the Master requests the analog value, the Slave sends the stored reading through UART.

The Master can then use this value for both display and motor-speed control.

 

Master-Slave Communication Protocol

The communication between the two boards follows a simple command-response protocol.

The Master first sends a command containing a start character, command identifier, and end character. The Slave receives the bytes, identifies the command, performs the requested operation, and returns a response.

For example, the temperature request follows this structure:

Command → START_CHAR   TEMP_CMD   END_CHAR

Response → START_CHAR   TEMP_RSP   STATUS   BYTE1  BYTE2  BYTE3  BYTE4  END_CHAR

 

For humidity:

Command →  START_CHAR    HUMIDITY_CMD    END_CHAR

Response → START_CHAR    HUMIDITY_RSP    STATUS BYTE1 BYTE2 BYTE3 BYTE4    END_CHAR

 

For the analog/speed value:

Command → START_CHAR    SPEED_CMD    END_CHAR

Response → START_CHAR    SPEED_RSP    STATUS BYTE1    END_CHAR

 

The Master can also control the LED on the Slave.

 

To turn the LED ON:

Command → START_CHAR    LED_ON_CMD    END_CHAR

Response → START_CHAR    LED_ON_RSP    STATUS END_CHAR

 

To turn the LED OFF:

Command → START_CHAR    LED_OFF_CMD    END_CHAR

Response → START_CHAR    LED_OFF_RSP    STATUS END_CHAR

 

The start and end characters help the Slave identify the boundaries of each command. The status byte can be used to indicate whether the requested operation was successfully processed.

 

Master Arduino Software

The Master program initializes the UART interface and the LCD. It then communicates with the Slave using the defined command-response format.

The basic sequence is to send a command, wait for the Slave response, validate the received data, extract the required value, and then update the appropriate output.

For example, after receiving temperature and humidity data, the Master updates the LCD. When the analog value is received, it can be displayed and used to determine the motor-control output.

The Master can also compare the received analog value with a predefined threshold. In this demonstration, when the speed value is greater than 50, the Master sends an LED ON command to the Slave.

 

Slave Arduino Software

The Slave program initializes the UART interface, DHT11 data pin, analog input, and LED output.

The Slave periodically reads the DHT11 and analog input and stores the latest values. When bytes arrive through the UART interface, the Slave processes them as part of the incoming command. Once a complete command has been received, the command is checked for validity.

If the command is valid, the Slave performs the requested operation and sends the corresponding response to the Master.

This allows the Slave to perform data acquisition independently while responding to requests from the Master.

 

Overall Working Principle

The complete operation can be summarized as a continuous request-and-response process.

The Slave acquires temperature, humidity, and analog data and keeps the latest values ready.

The Master requests the required information through UART.

The Slave receives the command, retrieves the requested value, and sends the response.

The Master receives the response and uses the information to update the LCD or control the motor.

For LED control, the process works in the opposite direction. The Master sends an LED ON or OFF command, and the Slave changes the state of the LED and returns a status response.

This creates a simple distributed control system in which the two Arduino boards perform different tasks while communicating through a common serial interface.

 

Conclusion

This project demonstrates how two Arduino UNO boards can be combined to create a simple master-slave embedded system using UART communication.

The Slave handles sensor acquisition from the DHT11 and potentiometer, while the Master manages the LCD, motor control, and high-level commands. The command-response protocol keeps communication organized and allows the Master to request specific information or control an output on the Slave.

The source code is available at by Play with Circuit:

https://playwithcircuit.com/master-slave-communication-between-two-arduino-boards/

License
All Rights
Reserved
licensBg
0