i am using TF02 pro LiDAR sensor with Arduino UNO for intelligent traffic system. I am using a following codes but facing a problem of false signals in between when sensor reads 0 cm average distance.
`#include <SoftwareSerial.h>
#define HORN_PIN 2 // Pin for activating the horn
#define RED_LIGHT_PIN 3 // Pin for the red light
#define GREEN_LIGHT_PIN 4 // Pin for the green light
#define TF02_RX_PIN 5 // Pin for TF02 sensor RX
#define TF02_TX_PIN 6 // Pin for TF02 sensor TX
SoftwareSerial tf02Serial(TF02_RX_PIN, TF02_TX_PIN); // SoftwareSerial instance for TF02 sensor
const int NUM_READINGS = 5; // Number of readings to average
uint16_t distanceReadings[NUM_READINGS]; // Array to store distance readings
int currentIndex = 0; // Index for storing the current reading
void setup() {
Serial.begin(9600);
pinMode(HORN_PIN, OUTPUT);
pinMode(RED_LIGHT_PIN, OUTPUT);
pinMode(GREEN_LIGHT_PIN, OUTPUT);
digitalWrite(RED_LIGHT_PIN, LOW); // Turn off red light initially
digitalWrite(GREEN_LIGHT_PIN, HIGH); // Turn on green light initially
tf02Serial.begin(115200); // Initialize serial communication for TF02 at baud rate 115200
// Initialize distance readings array
for (int i = 0; i < NUM_READINGS; i++) {
distanceReadings[i] = 0;
}
}
void loop() {
uint16_t distance = readDistanceFromTF02(); // Read distance measurement from TF02
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Update distance readings array
distanceReadings[currentIndex] = distance;
currentIndex = (currentIndex + 1) % NUM_READINGS;
// Calculate average distance
uint32_t distanceSum = 0;
for (int i = 0; i < NUM_READINGS; i++) {
distanceSum += distanceReadings[i];
}
uint16_t averageDistance = distanceSum / NUM_READINGS;
// Check if average distance is less than 30 meters
if (averageDistance < 2000) { // 30 meters = 3000 cm
activateHorn();
digitalWrite(RED_LIGHT_PIN, HIGH); // Turn on red light
digitalWrite(GREEN_LIGHT_PIN, LOW); // Turn off green light
} else {
digitalWrite(RED_LIGHT_PIN, LOW); // Turn off red light
digitalWrite(GREEN_LIGHT_PIN, HIGH); // Turn on green light
}
delay(100); // Delay between distance measurements
}
uint16_t readDistanceFromTF02() {
// Send command to request distance data
tf02Serial.write((uint8_t)0x5A); // Command to request distance data
tf02Serial.write((uint8_t)0x05); // Data length
tf02Serial.write((uint8_t)0x00); // Data byte 1
tf02Serial.write((uint8_t)0x00); // Data byte 2
tf02Serial.write((uint8_t)0x00); // Data byte 3
tf02Serial.write((uint8_t)0x01); // Data byte 4
// Read response
uint8_t response[9];
tf02Serial.readBytes(response, 9);
// Check if response is valid
if (response[0] == 0x59 && response[1] == 0x59) {
// Extract distance data
uint16_t distance = (response[3] << 8) | response[2]; // Combine high and low bytes
return distance;
} else {
return 0; // Invalid response, return 0 distance
}
}
void activateHorn() {
digitalWrite(HORN_PIN, HIGH); // Activate horn
delay(1000); // Horn duration: 1 second
digitalWrite(HORN_PIN, LOW); // Deactivate horn
}`
2 posts - 2 participants