Transferring code to the Nano 33 IOT

Hi,

I have been using the code example for the A02YYUW ultrasonic sensor with a Pro Mini but now want to change to the Nano 33 IOT. The example code below is asking for SoftwareSerial.h and won't compile. I understand the Nano 33 IOT does not use/need SoftwareSerial.h, but what do I need to change in the code to make it work?

Thanks

Bob

/*
  *@File  : DFRobot_Distance_A02.ino 
  *@Brief : This example use A02YYUW ultrasonic sensor to measure distance
  *         With initialization completed, We can get distance value 
  *@Copyright [DFRobot](https://www.dfrobot.com),2016         
  *           GUN Lesser General Pulic License
  *@version V1.0           
  *@data  2019-8-28
*/

#include <SoftwareSerial.h>

SoftwareSerial mySerial(10,11); // RX, TX
unsigned char data[4]={};
float distance;

void setup()
{
 Serial.begin(57600);
 mySerial.begin(9600); 
}

void loop()
{
    do{
     for(int i=0;i<4;i++)
     {
       data[i]=mySerial.read();
     }
  }while(mySerial.read()==0xff);

  mySerial.flush();

  if(data[0]==0xff)
    {
      int sum;
      sum=(data[0]+data[1]+data[2])&0x00FF;
      if(sum==data[3])
      {
        distance=(data[1]<<8)+data[2];
        if(distance>30)
          {
           Serial.print("distance=");
           Serial.print(distance/10);
           Serial.println("cm");
          }else 
             {
               Serial.println("Below the lower limit");
             }
      }else Serial.println("ERROR");
     }
     delay(100);
}

use Serial1. pins labeled RX and TX

Thanks for the reply and please excuse my ignorance as I am very new to coding. Could you explain a bit more what needs to change. I am beginning to understand that the 33 IOT uses pins D0 and D1 for serial communication. So instead of the above line it should read:

And also change all references of mySerial to Serial1?

Thanks

Bob

Serial1 object already exists. you don't have to create it as you don't create Serial

How do I write that? Will it automatically refer to pins D0 and D1?
Serial1; // RX, TX
unsigned char data[4]={};
float distance;

void setup()
{
Serial.begin(57600);
Serial1.begin(9600);

the Arduino core creates Serial1 for you for that specific board. you just begin it.
did you never see code for Arduino with more Setials like Mega, Leonardo, Micro?

Many thanks. With a bit of trial and error I got it working. Deleted the top line and all references of MySerial to Serial1 and used pins D0 & D1. Worked just fine. Thanks for pointing me in the right direction.

Bob

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