I am trying to create a small device to install in car to alert the driver if the distance with the car in the front is less than a particular distance. I am using a water proof version of ultrasonic sensor. I am using the waterproof one because the sensor will be placed outside of the car and will be thus subjected to rain and similar stuff. I have mentioned the link below.
My plan is to alert the driver using a buzzer. I want to give short beeps if the distance is less than 2 m. And a long continuous non stop beep if it gets less than 1 m.
I am using a standard code from the internet for the ultrasonic module which involves using the trig and echo pins.
But i am facing some problems
1)I am getting a lot of noise. The distance gets down to less than 100 cm suddenly in one reading and back to what it should be in another. I keep getting these type of sudden fluctuations even when the object in front is at constant distance.
i am not able to measure the distance properly around 2m and beyond even though the sensor says it can measure upto 4.5m. (Which is why i am trying to provide beeps on 160cm and continuous beep at 80cm for now).
changing modes i dont think it would help .Try placing the device in height and no obstacles in the front check the values with and without obstacle .Its detection angle is upto 70 degree so look for that too
I am using the standard arduino nano board and i am powering via tha usb cable. The connections are direct and i think are well understood from the code. Here's the code i am using:
// Define pins for the ultrasonic sensor
const int trigPin = 9;
const int echoPin = 10;
// Define pin for the buzzer
const int buzzerPin = 8;
void setup() {
// Initialize the serial communication
Serial.begin(9600);
// Set up the pins
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(buzzerPin, OUTPUT);
}
void loop() {
// Send a pulse to trigger the ultrasonic sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo
long duration = pulseIn(echoPin, HIGH);
// Calculate the distance in cm
float distance = duration * 0.034 / 2;
// Output the distance to the serial monitor
Serial.print("Distance: ");
Serial.print(distance);
Serial.println(" cm");
// Determine the beep pattern based on the distance
if (distance < 80) {
// Long beep if distance is less than 80 cm
digitalWrite(buzzerPin, HIGH); // Turn buzzer on
} else if (distance < 160) {
// Short beep if distance is less than 160 cm
digitalWrite(buzzerPin, HIGH); // Turn buzzer on
delay(100); // Beep for 100 ms
digitalWrite(buzzerPin, LOW); // Turn buzzer off
delay(100); // Pause before checking again
} else {
// No beep if distance is 160 cm or more
digitalWrite(buzzerPin, LOW); // Ensure buzzer is off
}
// Small delay before the next measurement
delay(100);
}
if the sudden change is your issue .it is normal in ultrasonic sensor getting noise from surroundings .Try filtering it by adding millis so if it stays high for particular period of time buzzer goes high
Have you considered using the same sensor the automotive companies use, it is already designed for the automotive environment. I expect you to have a lot of problems when the weather changes such as fog, rain, snow, etc. Check this link: https://www.sciencedirect.com/science/article/pii/S1472029920302435
I have tried checking readings with obstacles. It works fine when there's an obstacle. The reading fluctuates but my concern is not with what is the exact distance between the obstacle, but rather wether there's one within 1m or 2m.
When there's an obstacles the reading fluctuates within +-20 and i am able to still classify if the obstacle is within 1m or 2m. The problem comes when there's no obstacle. The value remains around 400 something and then drops to 100 or so without any obstacles. This causes the buzzer to beep even when theres nothing until atleast 5m.