Nano iot33 serial port example troubles, resolved

I am trying to get serial data thru TX/RX on nano 33 iot. but not receiving anything

so, I decided to verify using the example

and get the same results.. the sender responds to entered data on usb serial port
the receiver never responds.

I added verification in the receiver loop that it is running

but Serial1.available()>=0 never becomes true

the sender

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);      // set LED pin as output
  digitalWrite(LED_BUILTIN, LOW);    // switch off LED pin
 while(!Serial)
  Serial.begin(9600); 
 while(!Serial1)// initialize serial communication at 9600 bits per second:
  Serial1.begin(9600); 
  //Serial1.println("test");// initialize UART with baud rate of 9600
}
void loop() {

  if (Serial.read() == '1'){
    Serial1.println('1');
    Serial.println("loop");
    digitalWrite(LED_BUILTIN, HIGH);
    Serial.println("LEDS ON");
  }
  else if (Serial.read() == '0'){
    Serial1.println('0');
    Serial.println("loop1");
    digitalWrite(LED_BUILTIN, LOW);
    Serial.println("LEDS OFF");
  }
}

the receiver


void setup() {
  pinMode(LED_BUILTIN, OUTPUT);      // set LED pin as output
  digitalWrite(LED_BUILTIN, LOW);    // switch off LED pin

 while(Serial)
Serial.begin(9600);

while(!Serial1)
Serial1.begin(9600);            // initialize UART with baud rate of 9600
}
void loop() {
  while (Serial1.available() >= 0) {
    char receivedData = Serial1.read();   // read one byte from serial buffer and save to receivedData
    if (receivedData == '1') {
      digitalWrite(LED_BUILTIN, HIGH); // switch LED On
    }
    else if (receivedData == '0') {
      digitalWrite(LED_BUILTIN, LOW);  // switch LED Off
    }
  }
}

Please provide details. I don't see anything added to the "receiver" sketch that could be considered "verification".

I do see this code you added though:

This code will cause the sketch to hang in a perpetual loop, calling Serial.begin(9600); over and over and over again until you connect the "receiver" board to your computer via its USB cable and open that board's serial port in the Arduino IDE Serial Monitor (or any equivalent application).

The tutorial is written with the intent that you only have the "sender" board connected to your computer with the Serial Monitor.

Since you aren't even doing anything with Serial in the "receiver" sketch, this seems like a very bad addition all around.

I also see this addition:

 while(!Serial1)// initialize serial communication at 9600 bits per second:
  Serial1.begin(9600); 

This would cause Serial1.begin(9600); to be called over and over again as long as Serial1 is false. That a strange thing on its own, since you only need to call Serial1.begin(9600); once, but you'll see it is downright disastrous once you study the documentation:

On the boards with native USB, if (Serial) (or if(SerialUSB) on the Due) indicates whether or not the USB CDC serial connection is open. For all other boards, and the non-USB CDC ports, this will always return true.

Serial1 is a "non-USB CDC port", so this means the while condition will never evaluate as true, and thus Serial1 is never initialized.

I strongly recommend starting out by following the tutorial exactly. Once you have the "known good" code working as expected, you can feel free to methodically iterate on that to add whatever additional functionality you want. But jumping right in with random modifications from the start is not a recipe for success.

I didn't do 'random' mods.

I did the code as written, copy paste.

the receiver lights don't follow the sender.

so, altho the code isn't there now, I added a Serial.println() to loop, to insure it was running
I added a serial.println() to the end of setup to insure it finished

I added Serial.println to show that data was received from the Serial.available and read

the example says you don't NEED to have the receiver connected, but I do.

I added the whiles, just because my other sketches do.

The code you shared here is definitely not a copy/paste of the code in the tutorial.

Please try it again without the random modifications.

yep, why it works now, same cut and paste as original attempt.. i do not know
adding the serial.xxx functions also works

thanks..

now to find out why the other sketch doesn't do the same

1 Like

in 'random' modifications

  • missing ; after while
  • missing ! in while (Serial)
  • testing serial connection state before begining it
1 Like

so the original sketch issue

receive data over tx/rx, 115200, n81

setup

  Serial1.begin(115200);
  while (!Serial){
    Serial.begin(115200);

loop

void loop() {

  while (Serial1.available() >= 0){
    Serial.println("incoming data");
// write a message to serial to acknowledge data received
    Serial.write(Serial1.read());
  }

other stuff (BLE) which works) 

get stuck in the while loop

08:54:54.436 -> ⸮incoming data
08:54:54.436 -> ⸮incoming data
08:54:54.436 -> ⸮incoming data
08:54:54.436 -> ⸮incoming data
08:54:54.436 -> ⸮incoming data
08:54:54.539 -> ⸮incoming data
08:54:54.539 -> ⸮incoming data
08:54:54.539 -> ⸮incoming data
08:54:54.539 -> ⸮incoming data

but there isn't any incoming data yet

with rx/tx disconnected get the same

i put the sample at 115200 for tx/rx and all ok

so, is serial1 not available when using BLE? on nano iot
i think there are two different chips Nina for wifi/ble

from the datasheet page 8
Communication with NINA W102 happens through a serial port and a SPI bus through the following pins
31 PA22 23 GPIO3 Processor TX Nina RX
32 PA23 22 GPIO1 Processor RX Nina TX

using the sample receiver at 115200 from my other board works as expected.

does not work when BLE is used

yes, Serial1 is connected not only to RX/TX pins, but to the Nina module and ArduinoB:E library uses it for communication with the nina-fw n the Nina

bummer..

my external board can only send thru UART

now I need USB host mode on the NANO to connect BOTH boards, argh!

actually its Serial2 for nina.

and Serial1 problem resolved

while (Serial1.available() >= 0){

doah!

while (Serial1.available() > 0){

sorry. MKR 1010 has it on Serial1. they made it better for Nano Iot

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.