Object Detection and Counting Using ESP8266 and TF Luna LiDAR (2)
Object Detection and Counting Using ESP8266 and TF Luna LiDAR (2)
Required Components
ESP8266 Microcontroller: A low-cost Wi-Fi microcontroller used to process data
from the TF Luna LiDAR sensor and communicate results.
TF Luna LiDAR Sensor: A compact LiDAR sensor capable of measuring distances
with high accuracy.
Arduino IDE: Development software for programming and uploading code to the
ESP8266 microcontroller.
USB Cable: For powering the ESP8266 and uploading code.
Connecting Wires: To establish connections between the ESP8266 and the TF Luna
LiDAR sensor.
Power Supply: A suitable power source to ensure stable operation of both the
ESP8266 and the LiDAR sensor.
Required Libararies
1. Wire Library: For I2C communication between the ESP8266 and TF Luna LiDAR.
o Pre-installed in the Arduino IDE.
2. TFLI2C Library: For interfacing with the TF Luna LiDAR sensor.
o Download the TFLI2C Library
3. ArduinoJson Library: For handling JSON serialization and deserialization.
o Download ArduinoJson Library
Installation Steps
1. Set Up Arduino IDE
o Install the latest version of the Arduino IDE from arduino.cc.
o Install the ESP8266 board support package in Arduino IDE.
Go to File > Preferences.
Add the following URL to the "Additional Board Manager URLs":
http://arduino.esp8266.com/stable/package_esp8266com_inde
x.json
Go to Tools > Board > Board Manager and search for "ESP8266".
Click Install.
2. Install Required Libraries
o Open Arduino IDE.
o Go to Sketch > Include Library > Manage Libraries.
o Search for TFLI2C and install it.
o Search for ArduinoJson and install it.
3. Connect Hardware
o Connect the TF Luna LiDAR sensor to the ESP8266 using I2C (SDA and SCL
pins).
o Power the ESP8266 and LiDAR using a suitable power source.
4. Upload Code
o Open the provided code in the Arduino IDE.
o Select the correct board and port under Tools > Board and Tools > Port.
o Click Upload to upload the code to the ESP8266.
Program
#include <Wire.h>
#include <TFLI2C.h>
#include "ArduinoJson.h"
TFLI2C tfli2c;
int16_t tfDist;
int detectionCount = 0;
StaticJsonDocument<200> doc;
void setup() {
Serial.begin(9600);
Wire.begin();
void loop()
if (tfli2c.getData(tfDist, tfAddr))
if (!isObjectDetected)
objectDetectedTime = millis();
isObjectDetected = true;
isReappearedTooQuickly = true;
else
{
isReappearedTooQuickly = false;
detectionCount++;
lastDetectionTime = millis();
objectLeaveTime = millis();
isObjectDetected = false;
else
if (isObjectDetected)
objectLeaveTime = millis();
isObjectDetected = false;
Serial.println(jsonOutput);
delay(100);
StaticJsonDocument<1024> staticJsonDocument;
staticJsonDocument["Count"] = a;
staticJsonDocument["Dist"] = b;
char docBuf[1024];
serializeJson(staticJsonDocument, docBuf);
return String(docBuf);
How It Works
1. The TF Luna LiDAR sensor continuously measures the distance to objects in its line of
sight.
2. If an object is detected within the defined threshold limit (80 cm):
o The system checks if the object remains within the range for more than 1 second
before counting it.
o If the object is removed and repositioned too quickly, the system avoids
incrementing the count to prevent false triggering.
3. The object count and distance data are serialized into a JSON string and printed to the
Serial Monitor.
Features
Real-time object detection and counting.
JSON output for easy integration with other systems.
Configurable detection thresholds and timing parameters.
Applications
Smart home automation.
Industrial object counting systems.
Robotics and automation.
Output
1.When Object is detected within range for more than 1 second (Count +1)
2.When objected is detected beyond the range (Count does not increase)
3.When objected is removed and re positioned within 1 sec for more than 1 sec (Count does
not increase)
4.When objected is removed and re positioned after 1 sec for more than 1 sec (Count +1)