Learning Arduino for beginners EP#13 wired serial communication between two Arduino boards

In this tutorial we will take a look at how you can make two Arduino boards communicate with each other via wired serial connection.

 

 

ARDUINO CODE:

 

transmitter:

 

#define button 13

void setup() {
Serial.begin(9600);
pinMode(button, INPUT_PULLUP);

}

void loop() {

if (digitalRead(button) == LOW)
{
Serial.write(100);
delay(500);
}
delay(100);
}

 

receiver:

 

#define led 13
int val;
int state;

void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);

}

void loop() {
if (Serial.available() > 0)
{
val = Serial.read();

if (val == 100 && state == 0)
{
digitalWrite(led, HIGH);
state = 1;
delay(250);
}
else if (val == 100 && state == 1)
{
digitalWrite(led,LOW);
state = 0;
delay(250);
}
}
}

Advertisements