Two arduino nano serial communication

I have two arduino nano that I want to use a serial communication between them using TX & RX.

I just want a simple example to get the idea.

For such as the first arduino nano to turns on/off led from the second arduino and the second arduino could turn on/off led from the first arduino too.

Appreciate any help.

have a look at SoftwareSerial

Have you looked at the serial examples that ship with the IDE?

Edit - you didn't say if you need the hardware serial for anything (e.g. the serial monitor). If not, you can use it instead of SoftwareSerial.

Actually I made two PCB circuit (based on arduino nano) for my project, and I assigned TX & RX (pin 0 & 1) to make communication between them. So I can't replace the pins.

All what I want is to send a character from one arduino to the another one to perform some actions (both ways, from the first arduino to the second one and from the second arduino to the first one)

What have you tried? Please post your code.

Have a look at the examples in Serial Input Basics - simple reliable non-blocking ways to receive data. There is also a parse example to illustrate how to extract numbers from the received text.

The technique in the 3rd example will be the most reliable. It is what I use for Arduino to Arduino and Arduino to PC communication.

You can send data in a compatible format with code like this (or the equivalent in any other programming language)

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

...R

aarg:
What have you tried? Please post your code.

I'm requesting a example code because what I tried were just one way communication and not for nano-nano (it was uno-mega) because of that I have no idea about my case.

I told you where to find example code in reply #2.

Just the basic idea for single character
sender

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

receiver

void loop()
{
  if(Serial.available())
  {
    char ch = Serial.read();
    switch(ch)
    {
      case '0':
        // switch led on receiver off
        ...
        ...
        break;
    case '1':
      // switch led on receiver on
      ...
      ...
      break;
  }
}

Can they BOTH initiate a message? Or does only ONE always initiate messages and the other just replies to them?

-jim lee

hsalame:
I'm requesting a example code

What was wrong with the code in Reply #5 when you tried it?

...R

hsalame:
I assigned TX & RX (pin 0 & 1) to make communication between them. So I can't replace the pins.

I don't quite understand this, but it sounds like a seriously bad bit of planning. However, so long as you can disconnect the 0>1 pins. It is quite OK to use hardware serial. You just need to have them disconnected when you upload the programmes. This presupposes that you are not using hardware serial for something we haven't heard about - yet.

sterretje:
Just the basic idea for single character
sender

void loop()

{
  Serial.print('1');
  delay(1000);
  Serial.print('0');
  delay(1000);
}




**receiver**


void loop()
{
  if(Serial.available())
  {
    char ch = Serial.read();
    switch(ch)
    {
      case '0':
        // switch led on receiver off
        ...
        ...
        break;
    case '1':
      // switch led on receiver on
      ...
      ...
      break;
  }
}

I want the communication to be two way. The transmitter could receive a char and the receiver could transmit a char.

jimLee:
Can they BOTH initiate a message? Or does only ONE always initiate messages and the other just replies to them?

-jim lee

Both can transmit and both can receive a char sent from the other arduino

hsalame:
I want the communication to be two way.

Start by getting the appropriate example from the link in Reply #5 working for one-way communication. Then put the same functions (but in the opposite order) into your two Arduinos and you will have two-way communication.

...R

hsalame:
I want the communication to be two way. The transmitter could receive a char and the receiver could transmit a char.

OK, get rid of the delays in the transmitter code and base it on the blink-without-delay example.

void loop()
{
  static unsigned long lastUpdateTime = millis();
  static char cmd = '1';

  if (millis() - lastUpdateTime > 1000)
  {
    lastUpdateTime = millis();
    Serial.print(cmd);
    if (cmd == '0')
    {
      cmd = '1';
    }
    else
    {
      cmd = '0';
    }
  }
}

Now, understand it and you should not have a problem merging the sender and receiver code to get two dimensional communication going.

If you don't understand the code, ask.

Finally I managed to make it work.

Actually in the first arduino I have a connected servo and a button and in the second arduino I have RFID reader and servo.

So when the button in the first arduino click the servo in the second arduino moves, and when the RFID reader in the second arduino reads an authorized tag, the servo in the first arduino moves.

Appreciate all your support guys.

Here is the code:

First Arduino nano:

#include <Servo.h>
Servo myservo;

int button = A6;
int vala;
int a = 0;
int b = 0;
void setup()
{
  Serial.begin(9600);
  myservo.attach(2);
  pinMode(button, INPUT);
  while (!Serial) {
    ;
  }
}


void loop()
{
  if (Serial.available())
  {
    char c = Serial.read();
    if (c == '0' && a == 0)
    {
      myservo.write(90);
      a = 1;
    }
    if (c == '1' && a == 1)
    {
      myservo.write(146);
      a = 0;
    }
  }
      vala = analogRead(button);
    if (vala  > 100 && b == 0)
    {
      Serial.print(1);
      b=1;
    }
    else if (vala  < 100 && b == 1){
      Serial.print(0);
      b=0;
    }
}

Second Arduino nano:

#include <Servo.h>
Servo myservo;
char c  = ' ';
int a = 0;

#include <SPI.h>
#include <MFRC522.h>
#define SS_PIN 10
#define RST_PIN 9
MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
MFRC522::MIFARE_Key key;
byte nuidPICC[4];

String Card_1 = "89687148179";

void setup()
{
  myservo.attach(A3);
  Serial.begin(9600);

  SPI.begin();
  rfid.PCD_Init();
}


void loop()
{
  if (Serial.available())
  {
    char c = Serial.read();
    if (c == '0' && a == 0)
    {
      myservo.write(146);
      a = 1;
    }
    if (c == '1' && a == 1)
    {
      myservo.write(90);
      a = 0;
    }
  }
  Check_For_RFID();
}



void Check_For_RFID()
{
  if ( ! rfid.PICC_IsNewCardPresent())
    return;
  if ( ! rfid.PICC_ReadCardSerial())
    return;

  for (byte i = 0; i < 4; i++) {
    nuidPICC[i] = rfid.uid.uidByte[i];
  }

  printHex(rfid.uid.uidByte, rfid.uid.size);
//  Serial.println();
  rfid.PICC_HaltA();
  rfid.PCD_StopCrypto1();
}

void printHex(byte *buffer, byte bufferSize) {
  String UID = "";
  for (byte i = 0; i < bufferSize; i++) {
    UID += buffer[i], HEX;
    //    Serial.print(buffer[i], HEX);
  }
  if (UID == Card_1)
  {
    Serial.print(1);
  }
  else
  {
    Serial.print(0);
  }
  //  Serial.print(UID);
}

Always so nice to hear about the full project afterwards :frowning:

And one advise, do not use single character global variable names. In a year's time, you will no longer know what they were used for. And searching where they were used in the code is a disaster.

sterretje:
And one advise, do not use single character global variable names

Exactly, I will try to modify it to be able to receive a string. But as for this stage of my work it will work fine with me. Many thanks.

hsalame:

And one advise, do not use single character global variable names

Exactly, I will try to modify it to be able to receive a string. But as for this stage of my work it will work fine with me. Many thanks.

I think you have misunderstood the point that @sterretje was making.

He was referring to the fact that you have stuff like this in your program

char c  = ' ';
int a = 0;

Always give your variables meaningful names and then your code will be easier to understand and you will be less likely to make mistakes.

...R