Makarand
Makarand

Reputation: 9

Arduino to Arduino Via Serial

I wish to send serial data from an Arduino Uno to an Arduino Nano via the available UART port.

On reception of data at the Nano end I have to check whether the data matches to "a" or "b". If it does, I have to send data back to the Uno.

I have used the following Code on the Nano side:

void GMP_OutputSerial(void)
{
  if(Serial.available())                                              // Check if serial data is avaiable
  {
    unSerialAvailable = Serial.read();                                // Read the data if serial data is available
    if(unSerialAvailable == 'A' || unSerialAvailable == 'B' || unSerialAvailable == 'a' || unSerialAvailable == 'b')          // Proceed only if available data is equal to A or B
    {
      /*
      if(g_stSensorParms.stAppParms.unFinalDistance > 80 || g_stSensorParms.stAppParms.unFinalDistance < 0)   
      {
        Serial.println("Distance: Out of range");
      }
      else
      {
        */
        Serial.print("Distance: ");                                     // Output Distance text on the serial monitor
        Serial.println(g_stSensorParms.stAppParms.unFinalDistance);      // Output the actual distance on the serial monitor
    }
  }
}

On the Uno side:

void loop()
{
  Serial.write(a);
  delay(1);
}

However, I see no communication happening. Power to the both the boards comes from an external supply = 5V.

TX line of Uno is connected to RX line of Nano and vice versa.

What am I doing wrong?

Upvotes: 0

Views: 1373

Answers (6)

Eng.Mohammad
Eng.Mohammad

Reputation: 37

before uploading the script just remove the RX TX connections and after uploading successful re-connect them and chk it again . it's not that important to set the datarate at this time because even if the datarate between the two chips are not matched(which is most likely to happen) the error you will get is a distorted data ,so you will have transmission but not in the way you want

Upvotes: 0

Itamar
Itamar

Reputation: 76

The codes mentioned above should work properly, I suggest you to create some checksum to your string when you are trying to transmit as the serial connection between the two boards is not stable enough for large amounts of data.

Upvotes: 0

nandha Kumar
nandha Kumar

Reputation: 11

From Arduino UNO

void setup()
{
   Serial.begin(9600);
}
void loop()
{
Serial.print("a");
}

From Arduino NANO

void setup()
{
Serial.begin(9600);
if(Serial.available()>0)
{
char a = Serial.read();
}
}

Upvotes: 1

Md. Hanif Ali Sohag
Md. Hanif Ali Sohag

Reputation: 59

Please declare char a='A' then write it like below.

char a='A';
void setup() 
{

    Serial.begin(9600);

}

void loop() 
{

     Serial.write(a);
     delay(10);
}

Upvotes: 0

Igor Stoppa
Igor Stoppa

Reputation: 333

Did you put a Serial.begin(9600) in the setup function of each board? It would also help if you posted the core running on the UNO. Or at lest confirm that you have verified that it works, for example replacing the Nano with a PC, like the OP wrote. The UNO has leds on both TX and RX lines, so you can easily verify if anything is transmitted, if you are using the "real" serial port. The arduino ide also comes with integrated, easy-to-use serial sniffer.

Upvotes: 0

Keroronsk
Keroronsk

Reputation: 120

In Serial.write(a);, a is equal to 'A'? Or you really mean Serial.write('A')?

p.s. You can actually make cheap "sniffer" for serial port, by wiring 1ft. cable to DB9 connector, plugged in COM-port (or USB to serial adapter), and "see" actual data on the arduino RX\TX lines.

Upvotes: 3

Related Questions