Issues with UART Serial between AStar 32u4 and Arduino MKR 1010

Howdy,
Just having some issues getting communication between an A-Star 32u4 ULV (Arduino micro clone) and a Arduino MKR WiFi 1010

This is the code I am using on both:

   void setup() {
      // Open serial communications and wait for port to open:
      Serial.begin(57600);


      Serial.println("Goodnight moon!");

      // set the data rate for the SoftwareSerial port
      Serial1.begin(4800);
      Serial1.println("Hello, world?");
    }

    void loop() { // run over and over
    Serial.println(Serial1.read());
    Serial1.write(random(512));
      }

They’re connected together by RX and TX ports and same ground - I had previously been using a logic level shifter between the two but found this was causing major issues.

Currently, the A-Star outputs the random numbers (sent from the MKR) correctly. However, the MKR constantly outputs “-1” on the serial monitor.

Any thoughts?

Please use the .available method of either serial or softwareserial before reading data. In a tight loop such as in your code, data will only be available at best once in every couple of milliseconds, or after about 2000 cycles.

Software Serial reference here (with examples):

https://www.arduino.cc/en/Reference/softwareSerial

2 Likes

Thanks for that - I had forgotten about that limitation.

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);


  Serial.println("USB Serial...");

  // set the data rate for the Serial1 port
  Serial1.begin(4800);
  Serial1.println("Serial Port 1...");
}

void loop() { // run over and over
  if(Serial.available()){
    Serial.println(Serial1.read());
}
    
    if(Serial1.available()){
    Serial1.write(random(512));
    }
  }

That’s how I have changed the code - Is this correctly changed to work? (I will test it later today)

Hi Ben,

Take a look at the example code for the SoftwareSerial.h library: https://www.arduino.cc/en/tutorial/SoftwareSerialExample
In your code you have:

if(Serial.available()){
    Serial.println(Serial1.read());
}

This should be:

if(Serial1.available()){
    Serial.println(Serial1.read());
}

This is incorrect usage:

if(Serial1.available()){
Serial1.write(random(512));
}

Hope that helps :slight_smile:

Regards,
Oliver
Support | Core Electronics

1 Like

Not quite correct. You are trying to read from Serial1, so the if statement should say

if (Serial1.available()) {
Serial.println(Serial1.read());
Serial1.write(random (512));
}

You only need the one if test to fix the original code. Note that the available function is used to check the port you are READING FROM.

2 Likes

Using this:

  // Open serial communications and wait for port to open:
  Serial.begin(57600);


  Serial.println("USB Serial...");

  // set the data rate for the Serial port
  Serial1.begin(4800);
  Serial1.println("Serial Port 1...");
}

void loop() { // run over and over
  if (Serial1.available()) {
    Serial.println(Serial1.read());
    Serial1.write(random (512));
  }

}

On both the A-Star and the MKR 1010 results in the A-Star outputting random numbers to the serial monitor even when disconnected from the mkr and the MKR not outputting anything. Please help!! :slight_smile: :pleading_face:

1 Like

Please show the pin connections for the two boards. You may not have the boards properly connected. All the ports in use are hardware backed, so pin connection is relevant here.

3 Likes

A post was split to a new topic: MKR 1010 WiFi UART Serial Communication