I'm creating a speed measurement device with two ESP32's and two TF Mini Plus LiDAR sensors. These sensors would face perpendicular to the roadway, and measure distance. When that distance decreases, a car passes. We can then measure the time difference between the change in distance and calculate speed.
But let's walk before we can run. First, I want to reliably measure distance on one of the devices! I am able to get a distance measurement with one device, and even have that distance displayed on a 0.91inch OLED display (model is ssd1306). The problem is after about 30 seconds of correct distance measurement, it stops, and no change in distance can be seen. This change in behavior also coincides with a change in current. When acting correctly, I'm reading 48 mA in the circuit. During this period of freezing, it jumps to 68 mA and stays there.
For the battery, I'm using an 1800 mAh 3.7V LiPo battery, which I thought should be more than enough. The TF Mini Plus requires 5V, so it is stepped up from 3.7V via a buck-boost converter.
I've heard it could be multitasking issues, or memory issues, which worries me because I still have to set up the wifi access point and routes for communication and logging between the two devices.
Here's the code that is just trying to display distance on the display (will build more logic later one step at a time)
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <HardwareSerial.h>
#define SCREEN_WIDTH 128 // OLED display width
#define SCREEN_HEIGHT 32 // OLED display height
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C // I2C address for the OLED
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
HardwareSerial mySerial(2); // Use hardware serial port #2
void setup() {
// Initialize OLED display
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
display.display();
delay(2000); // Pause for 2 seconds
display.clearDisplay();
// Start the hardware serial port for TF Mini Plus
mySerial.begin(115200, SERIAL_8N1, 16, 17); // RX, TX
Serial.begin(115200); // Start the built-in serial port
}
void loop() {
// Check if data is available to read from TF Mini Plus
if (mySerial.available() >= 9) { // Assuming each data packet is 9 bytes
uint8_t incomingBytes[9]; // Buffer to store incoming bytes
for (int i = 0; i < 9; i++) {
incomingBytes[i] = mySerial.read();
}
// Implement the parsing logic based on the TF Mini Plus protocol
uint16_t distance = parseData(incomingBytes);
// Display the parsed data on OLED
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(SSD1306_WHITE);
display.setCursor(0,0);
display.print("Distance: ");
display.print(distance); // Display the distance
serial.println(distance);
display.println(" cm");
display.display();
}
}
// Function to parse data from TF Mini Plus
uint16_t parseData(uint8_t *bytes) {
// Example parsing logic - Adjust as per the actual data format
uint16_t distance = (bytes[2] << 8) + bytes[3];
return distance;
}
FWIW, I was also trying to do this project with two ESP8266's, but found that the voltage difference with the TF Mini Plus just wasn't really working out. I have the code for that too (which was able to measure speed and post that via wifi, but not reliably due to voltage)
Thank you for any guidance! I really want this project to work, but just can't get past these hurdles.