I've recently purchased this tof sensor from waveshare labeled as " TOF (Time Of Flight) Laser Range Sensor (B)", which is rated for 15m.
Using the esp32 wroom 32d, i have connected the TXD to pin 16 and RXD to 17. The first problem i encountered was that, by simply reading the data from the esp and printing them to serial(in HEX), it seems to continuously oscillating between F7, F3, E3, E7. The code:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
Serial2.begin(115200);
}
void loop() {
if (Serial2.available()>10) {
Serial.println(Serial2.read(), HEX);
}
}
, which is completely wrong. Following the documentation from waveshare's website, it seems that their protocol consists of 16 bytes of discrete information, as shown on their wiki:
Second thing i tried, was to get a USB to TTL usb converter as recommended from their website, in order to use their software which comes with UI. The sensor was recognized almost instantly and i was seeing the data flowing, as well as some nice graphs. After about 10 mins, the device just disconnected. From now on, when i hit "connect", i get a red message saying " Recognize failed" and can only see the data coming in as raw hex.:
Yes i have connected the sensor directly to the 5v of the esp32 as shown in the documentation diagram. I believe there exists some internal resistance.
Nice catch. By changing the baud rate to the default, fixed that part...Im getting now the same data as in the software. I'll do some tests and i will come back shortly. Thanks again!
Ok so. After some investing, i noticed that after reading the data from the buffer, it doesn't automatically refill(sensor transmits 256 bytes of data once and then stops). I tested this, with this piece of code:
#include <HardwareSerial.h>
void setup() {
// put your setup code here, to run once:
Serial.begin(921600);
Serial2.begin(921600);
}
void loop() {
Serial.println(Serial2.available());
delay(4000);
char i = Serial2.read();
}
This slowly returns 256,255,254,... all the way to 0. I've even added 4secs to give it enough time. This behavior is very weird and it was not happening before...
I used the delay to give a chance for the buffer to fill up again. If i remove the delay, Serial2.available() returns 0 instantly. As i mentioned, for some reason, the buffer does not get renewed, meaning if i call Serial2.read() enough times, .available() stays at 0. As of the 256, it seems to be the max buffer size. I dont understand where the problem is...
Oh ok, thanks for that. If you could not tell, i am experimenting with stuff, that's new for me. As far as I'm concerned, active mode can be configured through the software. Since the software does not work on my system, i can't find another way. I have read the wiki multiple times about the protocol used...Maybe i missed something?
Also i think it is important to note that, i have tried running their demo code(modified to use HarwareSerial) many many times and i've spent a lot of time debugging it. What i found out was that it fails to recognize the 3 data headers, as referred in the wiki. The wiki code(without modifications):
#include <SoftwareSerial.h>
SoftwareSerial Serial1(11,10); //Serial1 connected to TOF Serial:10<-->RX, 11<-->TX
unsigned char TOF_data[32] = {0}; //store 2 TOF frames
unsigned char TOF_length = 16;
unsigned char TOF_header[3] {0x57,0x00,0xFF};
unsigned long TOF_system_time = 0;
unsigned long TOF_distance = 0;
unsigned char TOF_status = 0;
unsigned int TOF_signal = 0;
unsigned char TOF_check = 0;
void setup() {
// initialize both serial ports:
Serial.begin(115200);
Serial1.begin(115200);
}
bool verifyCheckSum(unsigned char data[], unsigned char len){
TOF_check = 0;
for(int k=0;k<len-1;k++)
{
TOF_check += data[k];
}
if(TOF_check == data[len-1])
{
Serial.println("TOF data is ok!");
return true;
}else{
Serial.println("TOF data is error!");
return false;
}
}
void loop() {
// read from port 1:
delay(100);
if (Serial1.available()>=32) {
for(int i=0;i<32;i++)
{
TOF_data[i] = Serial1.read();
}
for(int j=0;j<16;j++)
{
if( (TOF_data[j]==TOF_header[0] && TOF_data[j+1]==TOF_header[1] && TOF_data[j+2]==TOF_header[2]) && (verifyCheckSum(&TOF_data[j],TOF_length)))
{
if(((TOF_data[j+12]) | (TOF_data[j+13]<<8) )==0)
{
Serial.println("Out of range!");
}else{
Serial.print("TOF id is: ");
Serial.println(TOF_data[j+3],DEC);
TOF_system_time = TOF_data[j+4] | TOF_data[j+5]<<8 | TOF_data[j+6]<<16 | TOF_data[j+7]<<24;
Serial.print("TOF system time is: ");
Serial.print(TOF_system_time,DEC);
Serial.println("ms");
TOF_distance = (TOF_data[j+8]) | (TOF_data[j+9]<<8) | (TOF_data[j+10]<<16);
Serial.print("TOF distance is: ");
Serial.print(TOF_distance,DEC);
Serial.println("mm");
TOF_status = TOF_data[j+11];
Serial.print("TOF status is: ");
Serial.println(TOF_status ,DEC);
TOF_signal = TOF_data[j+12] | TOF_data[j+13]<<8;
Serial.print("TOF signal is: ");
Serial.println(TOF_signal ,DEC);
Serial.println("");
}
break;
}
}
}
}
If the sensor is in active mode, it should be transmitting data nearly continuously. If it is not transmitting data, it is probably safe to assume that it is not in active mode, and expects a properly formatted inquiry packet from the Arduino.
The best way to get started is to find a PC that will run their software package, and configure the device the way you want it to work.
Ok, will try that again later. I still cant wrap my head around how the software worked perfectly for 10 minutes and then suddenly refused to recognize the device, ever again.