How to make two Arduino boards talk to each other

Today we will take a look at how to use serial communication to make to Arduino boards communicate with each other.

 

 

SCHEMATICS:

ARDUINO TRANSMITER CODE:

 

#define button 2

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

}

void loop() {

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

 

 

RECIEVER ARDUINO CODE:

 

#define led 13
int val;
boolean state = false;
void setup() {
Serial.begin(9600);
pinMode(led, OUTPUT);
digitalWrite(led, LOW);

}

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

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