---
title: "Communication between two Arduino"
url: "https://maker.wiznet.io/jaden/projects/communication-between-two-arduino/"
markdown_url: "https://maker.wiznet.io/jaden/projects/communication-between-two-arduino/md"
type: "UCC: User Created Content"
author: "https://arduinogetstarted.com/"
author_url: "https://arduinogetstarted.com/tutorials/communication-between-two-arduino"
editor: "WIZnet"
editor_url: "https://maker.wiznet.io/"
original_author: "https://arduinogetstarted.com/"
original_url: "https://arduinogetstarted.com/tutorials/communication-between-two-arduino"
published: "2022-09-30"
language: "en"
hardware: ["Arduino UNO", "Arduino Ethernet Shield 2"]
likes: 8
views: 1130
comments: 2
source: "WIZnet Makers (https://maker.wiznet.io/)"
---

# Communication between two Arduino

> Communication between two Arduino via Ethernet

Original author: https://arduinogetstarted.com/ (source: https://arduinogetstarted.com/tutorials/communication-between-two-arduino)

## Components

- **Arduino UNO** x 1 ([docs](https://www.farnell.com/datasheets/1682209.pdf))
- **Arduino Ethernet Shield 2** x 1 ([docs](https://docs.arduino.cc/hardware/ethernet-shield-rev2))

## Article

![two arduino led switch button](https://arduinogetstarted.com/images/tutorial/two-arduino-led-switch-button.jpg)

author defined simple protocol:

TCP connection is created betwwen Arduino #1 and Arduino #2

![](https://maker.wiznet.io/upload/ckeditor5/485738656%5F1666018421%2Epng)

this is Arduino #1 code

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/communication-between-two-arduino
*/

// ARDUINO #1: TCP CLIENT + A BUTTON/SWITCH
#include &lt;ezButton.h>
#include &lt;SPI.h>
#include &lt;Ethernet.h>

const int BUTTON_PIN = 7;
const int serverPort = 4080;
ezButton button(BUTTON_PIN); // create ezButton that attach to pin 7;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03};
IPAddress serverAddress(192, 168, 0, 180);
EthernetClient TCPclient;

void setup() {
Serial.begin(9600);
button.setDebounceTime(50); // set debounce time to 50 milliseconds

Serial.println("ARDUINO #1: TCP CLIENT + A BUTTON/SWITCH");

// Initialize Ethernet Shield:
if (Ethernet.begin(mac) == 0)
Serial.println("Failed to configure Ethernet using DHCP");

// connect to TCP server (Arduino #2)
if (TCPclient.connect(serverAddress, serverPort))
Serial.println("Connected to TCP server");
else
Serial.println("Failed to connect to TCP server");
}

void loop() {
button.loop(); // MUST call the loop() function first

if (!TCPclient.connected()) {
Serial.println("Connection is disconnected");
TCPclient.stop();

// reconnect to TCP server (Arduino #2)
if (TCPclient.connect(serverAddress, serverPort))
Serial.println("Reconnected to TCP server");
else
Serial.println("Failed to reconnect to TCP server");
}

if (button.isPressed()) {
TCPclient.write('1');
TCPclient.flush();
Serial.println("- The button is pressed, sent command: 1");
}

if (button.isReleased()) {
TCPclient.write('0');
TCPclient.flush();
Serial.println("- The button is released, sent command: 0");
}
}

and this is Arduino #2 code

/*
* Created by ArduinoGetStarted.com
*
* This example code is in the public domain
*
* Tutorial page: https://arduinogetstarted.com/tutorials/communication-between-two-arduino
*/

// ARDUINO #2: TCP SERVER + AN LED
#include &lt;SPI.h>
#include &lt;Ethernet.h>

const int LED_PIN = 7;
const int serverPort = 4080;

byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
EthernetServer TCPserver(serverPort);

void setup() {
Serial.begin(9600);
pinMode(LED_PIN, OUTPUT);

Serial.println("ARDUINO #2: TCP SERVER + AN LED");

// Initialize Ethernet Shield:
if (Ethernet.begin(mac) == 0)
Serial.println("Failed to configure Ethernet using DHCP");

// Print your local IP address:
Serial.print("TCP Server IP address: ");
Serial.println(Ethernet.localIP());
Serial.println("-> Please update the serverAddress in Arduino #1 code");

// Listening for a TCP client (from Arduino #1)
TCPserver.begin();
}

void loop() {
// Wait for a TCP client from Arduino #1:
EthernetClient client = TCPserver.available();

if (client) {
// Read the command from the TCP client:
char command = client.read();
Serial.print("- Received command: ");
Serial.println(command);

if (command == '1')
digitalWrite(LED_PIN, HIGH); // Turn LED on
else if (command == '0')
digitalWrite(LED_PIN, LOW); // Turn LED off

Ethernet.maintain();
}
}

---

Source: https://maker.wiznet.io/jaden/projects/communication-between-two-arduino/
