ArduinoGeneral

Laser Sensor SNE0366 & arduino MKR wifi 1010 no go

userHead John.bgood 2023-01-08 20:36:22 238 Views1 Replies

Hello 

 

There was a working board in the lab with the laser sensor working. The arduino code was long gone. 

I had to get it working again, so I used the code from this page: 

https://wiki.dfrobot.com/Infrared_Laser_Distance_Sensor_50m_80m_SKU_SEN0366

 

The board will not work now and the laser sensor does not even turn on

 

This is the start of the arduino code from the above page: 

 

#include <SoftwareSerial.h> 

SoftwareSerial mySerial(2,3);//Define software serial, 3 is TX, 2 is RX

char buff[4]={0x80,0x06,0x03,0x77};

unsigned char data[11]={0};

void setup()

{ Serial.begin(115200);

mySerial.begin(9600);

}

 

 

Now the MKR wifi 1010 does not need the SoftwareSerial.h apparently, and it does not compile code with it. One has to use the internal serial, or Serial1 as they call it. 

 

So the above code becomes:

 

 char buff[4]={0x80,0x06,0x03,0x77};

unsigned char data[11]={0};

void setup()

{ Serial.begin(115200);

Serial1.begin(9600);

}

*similar for all other references to myserial, as it is now removed. 

The problem is that now, there is nothing coming from the sensor, there are no values and as mentioned the sensor doesn't even turn on.  

Need to mention that the board is inside a box, so nobody messed with it, I uploaded the arduino code as I found it, it didn't work, I changed the myserial thing to the Serial1 thing, and now nothing works. 

 

Any suggestions greatly appreciated !

 

thank you all 

 

The full code is: 

 


 

char buff[4]={0x80,0x06,0x03,0x77};

unsigned char data[11]={0};

void setup()

{

 Serial.begin(115200);

 Serial1.begin(9600);

}


 

void loop()

{

  Serial1.print(buff);

  while(1)

  {

    if(Serial1.available()>0)//Determine whether there is data to read on the serial

    {

      delay(50);

      for(int i=0;i<11;i++)

      {

        data[i]=Serial1.read();

      }

      unsigned char Check=0;

      for(int i=0;i<10;i++)

      {

        Check=Check+data[i];

      }

      Check=~Check+1;

      if(data[10]==Check)

      {

        if(data[3]=='E'&&data[4]=='R'&&data[5]=='R')

        {

          Serial.println("Out of range");

        }

        else

        {

          float distance=0;

          distance=(data[3]-0x30)*100+(data[4]-0x30)*10+(data[5]-0x30)*1+(data[7]-0x30)*0.1+(data[8]-0x30)*0.01+(data[9]-0x30)*0.001;

          Serial.print("Distance = ");

          Serial.print(distance,3);

          Serial.println(" M");

        }

      }

      else

      {

        Serial.println("Invalid Data!");

      }

    }

    delay(20);

  }

}

 

 

2023-02-20 17:23:11

Hi

You have to define the pins of Serial1 when you use .begin() function. 

The pins should follow the datasheet of the board, sorry we do not have this board so you have to check yourself.

Modify the codes and wiring according to the datasheet, then try again.

Hope it can work.

userHeadPic NeloKin