Hey all,
I read through the datasheet that DFRobot supplied on their wiki and commented out the example code so it’s human readable:
/*!
* @File DFRobot_Iraser_Sensor.ino
* @brief In this example, the infrared laser ranging sensor is used to measure the distance, and the sensor data is processed to obtain the measured distance
* @copyright Copyright (c) 2010 DFRobot Co.Ltd (http://www.dfrobot.com)
* @licence The MIT License (MIT)
* @author [liunian](nian.liu@dfrobot.com)
* @version V1.0
* @date 2020-08-13
*/
#define ADDR 0x80
#include <SoftwareSerial.h>
SoftwareSerial mySerial(2,3);//Define software serial, 3 is TX, 2 is RX
char buff[4]={ADDR,0x06,0x03,0x77}; //continuous measurement Request
// Continuous measurement returns 11 Bytes. On success:
// ADDR 0x06 0x83 0x3X 0x3X 0x3X 0x2E 0x3X 0x3X 0x3X CHECK_BYTE (where 0x3X are Ascii digits, 0x2E is Ascii '.')
// On Fail:
// ADDR 0x06 0x83 'E' 'R' 'R' '-' '-' '-' 0x3X CHECK_BYTE (where 3X are Ascii digits, 0x2E is Ascii '.')
unsigned char data[11]={0}; //prepate an array to receive data
void setup()
{
Serial.begin(115200); //Hardware serial, for use with serial monitor
mySerial.begin(9600); //Software serial, for use with Laser
}
void loop()
{
mySerial.print(buff); // Request continous measurement mode
while(1)
{
if(mySerial.available()>0) //Determine whether there is data to read from the Laser
{
delay(50);
//read received data
for(int i=0;i<11;i++)
{
data[i]=mySerial.read();
}
//calculate the CHECK_BYTE for received data
unsigned char Check=0;
for(int i=0;i<10;i++)
{
Check=Check+data[i];
}
Check=~Check+1;
if(data[10]==Check) //Compare received CHECK_BYTE to calculated CHECK_BYTE
{ //If the check_bytes match, data is ok
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; //Convert the distance from a string of chars to a float.
Serial.print("Distance = ");
Serial.print(distance,3); //Print the value to Hardware Serial
Serial.println(" M");
}
}
else //The CHECK_BYTEs don't match. Something's wrong!
{
Serial.println("Invalid Data!");
}
}
delay(20);
}
}
It’s probably worth modifying the sketch to see if first turning the laser on by sending:
0x80 0x06 0x05 0x01 0x74
or off with 0x80 0x06 0x05 0x00 0x75
helps at all.
Also, it’s worth noting that it actually returns ascii digits, so you should actually be able to strip it right back and pass the raw data straight to the serial monitor via the Hardware serial.
Here’s a hardware ↔ software serial passthrough sketch I did up a while back that might come in handy here:
Oh, I also double checked, and the UART protocol the datasheet requires is the default for software serial and Hardware serial: 8 data bits, no parity bit, and 1 stop bit, so shouldn’t be any issues there.
Maybe someone who’s actually got one of these modules can get some part numbers off the ICs, and see if we can’t find a proper datasheet?
Another thought, it might be worth writing a simple script that runs through all possible addresses and checks for a valid response.