3

Trying to do serial communication between 2 Arduino Unos.

Code:

#include <SoftwareSerial.h>

int ledPin = 13;
int board;   // 1 = Uno. 2 = Mega.
int on_off;  // 1 = On. 0 = Off.
int buzzerPin = 9;
int Ack_tx = 2; //Acknowledgement tx.
int rec;

void setup()
{
  Serial.begin(9600);      // start serial communication at 9600bps
  Serial1.begin(9600);     // start serial1 communication at 9600bps

  pinMode(ledPin, OUTPUT);
  pinMode(buzzerPin, OUTPUT);

  Serial.flush();
  Serial1.flush();

} // End Setup

void loop()
{

  Serial.println("Enter Uno=1/Mega=0 ...");  //Select board
  //  if(!Serial){ board = Serial.read();}
  while(Serial.available()==0) { // Wait for User to Input Data
  }
  board = Serial.parseInt();  //Read the data the user has input


  Serial.println("Enter LED On=1/Off=0 ...");  //Select On or Off
  while (Serial.available()==0) { // Wait for User to Input Data
  }
  on_off = Serial.parseInt();

  Serial.println();


  switch (board) {

  case 0:  //Mega board
    if (on_off == 1) digitalWrite(ledPin, HIGH); //Select Uno LED On
    else digitalWrite(ledPin, LOW);              //Select Uno LED Off
    break;  //End Case

  case 1:  //Uno board
    //Serial.println("Off to Uno...");
    //Send on_off info to Uno
    Serial1.write(on_off/256); //Send the Quotient or "how many times" value
    Serial1.write(on_off%256); //Send the Modulo or Remainder.
    delay(50); //Wait for the serial port.

    while(Serial1.available()<2) //Wait for 2 bytes to arrive
     {
      //do nothing
     } //End While


  byte b1=Serial1.read();  //Read Upper byte
  byte b2=Serial1.read();  //Read Lower byte
  rec=(b1*256)+b2; 

    if (rec == Ack_tx) tone(buzzerPin, 300, 300); //Ack_tx from Uno
    break;
  } //End Switch Case

}  

Error:

Arduino: 1.8.5 (Windows 10), Board: "Arduino/Genuino Uno"

C:\Users\Akshit\Documents\Arduino\sketch_mar06a\sketch_mar06a.ino: In function 'void setup()':

sketch_mar06a:13: error: 'Serial1' was not declared in this scope

   Serial1.begin(9600);     // start serial1 communication at 9600bps

   ^

C:\Users\Akshit\Documents\Arduino\sketch_mar06a\sketch_mar06a.ino: In function 'void loop()':

sketch_mar06a:51: error: 'Serial1' was not declared in this scope

     Serial1.write(on_off/256); //Send the Quotient or "how many times" value

     ^

Multiple libraries were found for "SoftwareSerial.h"
 Used: C:\Program Files (x86)\Arduino\hardware\arduino\avr\libraries\SoftwareSerial
 Not used: C:\Program Files (x86)\Arduino\libraries\SoftwareSerial
exit status 1
'Serial1' was not declared in this scope

This report would have more information with
"Show verbose output during compilation"
option enabled in File -> Preferences.

This is a very basic problem I know, but nothing on the internet is helping.

migrated from electronics.stackexchange.com Mar 6 '18 at 16:55

This question came from our site for electronics and electrical engineering professionals, students, and enthusiasts.

  • 3
    Why do you think "Serial1" should exist? – Ignacio Vazquez-Abrams Mar 6 '18 at 16:55
  • I picked this code off of the internet. I feel it should exist because we would need one serial for communication with the serial monitor of computer and a second one for the actual serial communication between the two arduinos Pls correct me if I am wrong. – Aki Mar 6 '18 at 16:58
  • 1
    So... you saw it in random code and thought it would work everywhere? – Ignacio Vazquez-Abrams Mar 6 '18 at 16:58
  • The code made sense to me. Couldnt think of any changes to make.. Ill try the answer given by Majenko – Aki Mar 6 '18 at 17:00
  • @Aki An Uno has only one hardware serial interface (Serial). What do you expect Serial1 to be? – gre_gor Mar 6 '18 at 17:14
4

The Uno has no Serial1. It only has one serial port, called Serial. To use Serial1 you will need a bigger board such as a Mega2560, or define it as a SoftwareSerial port and use two other IO pins of your choice for it, though at lower baud rates than Serial can run at.

  • I have arduino micro, and I can use serial (form comunication pc )and serial1 (form com. wifi module). – mustafatorun Jan 28 at 15:22
3

You import the SoftwareSerial library but you however never instance an object of the SoftwareSerial class.

You just need to add the line

 SoftwareSerial Serial1(10, 11); // RX, TX

after including the library header (taken from here)

This now creates an object Serial1 on which the operations down there should work the same as with a real hardware serial (API-wise).

Side note: Careful with the wiring. You are using this Serial1 emulated UART to communicate between the boards, so if you're using above pin numbers (D10 = RX, D11 =TX) then you must connected D10 (Board1) -> D11 (Board2), D11 (Board1) -> D10 (Board2), i.e. RX->TX, TX->RX.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.