I have broken out the 2 bytes for the distance reading in my code to try and better understand what’s happening.
From 0 - 127 it is accurate within spec. However at 127, the first byte shifts back to 0 and then climbs with distance up to 63 and then leaps up to 192 where it increments normally to 255. The second byte stays at zero until the first byte exceeds 255 as per normal.
Those numbers are all powers of 2 (except 192, which is 128 + 64) which makes me suspect some sort of bit error. I’d think it was just faulty if I didn’t have 2 of them doing the same thing.
Since it is using a third party software library, I would suggest that you should file a GitHub issue in the event it is a software issue. Can you please also confirm what your setup is and how you are powering your module?
There’s no 3rd party library involved, not sure what you are referring to there? The code uses the official Arduino SoftwareSerial library and just uses it to read raw bytes. I am powering the module from the 5v out pin on the Arduino board. I will try using a separate power supply tonight, but I doubt that is the issue as I’m not getting brownouts or resets that would indicate it’s drawing too much.
It’s basically the code from my link, but I broke out all the values to try and understand what was going on…
/*
Based on: https://github.com/TFmini/TFminiPlus-Arduino/blob/master/Example-ReadData/ReadData/ReadData.ino
*/
#include <SoftwareSerial.h> //header file of software serial port
SoftwareSerial Serial1(2,3); //define software serial port name as Serial1 and define pin2 as RX and pin3 as TX
/* For Arduinoboards with multiple serial ports like DUEboard, interpret above two pieces of code and directly use Serial1 serial port*/
int dist; //actual distance measurements of LiDAR
int strength; //signal strength of LiDAR
float temprature;
int check; //save check value
int i, freq, lastfreq;
unsigned int uart[9]; //save data measured by LiDAR
const int HEADER=0x59; //frame header of data package
const int spkr_pin = 5;
void setup() {
//Serial.begin(115200); //set bit rate of serial port connecting Arduino with computer
Serial1.begin(115200); //set bit rate of serial port connecting LiDAR with Arduino
pinMode(spkr_pin, OUTPUT);
}
void loop() {
if (Serial1.available()) { //check if serial port has data input
if(Serial1.read() == HEADER) { //assess data package frame header 0x59
uart[0]=HEADER;
if (Serial1.read() == HEADER) { //assess data package frame header 0x59
uart[1] = HEADER;
for (i = 2; i < 9; i++) { //save data in array
uart[i] = Serial1.read();
}
check = uart[0] + uart[1] + uart[2] + uart[3] + uart[4] + uart[5] + uart[6] + uart[7];
if (uart[8] == (check & 0xff)){ //verify the received data as per protocol
dist = uart[2] + uart[3] * 256; //calculate distance value
strength = uart[4] + uart[5] * 256; //calculate signal strength value
Serial.print(uart[0]);
Serial.print("\t");
Serial.print(uart[1]);
Serial.print("\t");
Serial.print(uart[2]);
Serial.print("\t");
Serial.print(uart[3]);
Serial.print("\t");
Serial.print(uart[4]);
Serial.print("\t");
Serial.print(uart[5]);
Serial.print("\t");
Serial.print(uart[6]);
Serial.print("\t");
Serial.println(uart[7]);
freq = 200 - dist + 31;
//Serial.println(freq);
if (freq != lastfreq) {
if (dist < 200) {
tone(spkr_pin, freq);
} else {
noTone(spkr_pin);
}
lastfreq = freq;
}
}
}
}
}
}
Aah damn, I got hopeful when you said you had a suspicion. I will try those other channels you suggest. Thanks a lot for your help and quick responses.