Glitch in Object detecting goggles

Ask questions about projects relating to: aerodynamics or hydrodynamics, astronomy, chemistry, electricity, electronics, physics, or engineering.

Moderators: AmyCowen, kgudger, bfinio, MadelineB, Moderators

Post Reply
meowrat
Posts: 2
Joined: Tue Dec 03, 2024 12:13 am
Occupation: Student

Glitch in Object detecting goggles

Post by meowrat »

Hello, I have a question regarding the object-detecting goggles you discussed on your website. So, I made the circuit and everything works alright but the code is not working well. Because sometimes even when no object is present, the buzzer makes small beeping noises. I have put in the direct code from your website; i.e. copying and pasting it onto the Arduino IDE. I've also checked with the serial monitor but to no avail.
if you could send me a detailed explanation of what went wrong or how to fix it it would be deeply appreciated)
The code I used from your website:



/*
Code to make a buzzer beep faster as an object gets closer to an ultrasonic distance sensor
For instructions and circuit diagram see:
https://www.sciencebuddies.org/science- ... king-stick

*/

// constant variables for pins
const int trigPin = 6;
const int echoPin = 7;
const int buzzerPin = 11;

int threshold = 30; // threshold distance to activate buzzer
// buzzer delay variables
int buzzerDelay;
int buzzerDelayMin = 10;
int buzzerDelayMax = 250;

long duration; // ultrasonic return pulse duration
long cm; // distance in centimeters

void setup() { // setup code that only runs once
// set pin modes
pinMode(trigPin,OUTPUT);
pinMode(echoPin,INPUT);
pinMode(buzzerPin,OUTPUT);
// uncomment the next line to user serial print for debugging
// Serial.begin(9600);
}

void loop() {

// send trigger pulse
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(5);
digitalWrite(trigPin, LOW);

// read echo pulse
duration = pulseIn(echoPin, HIGH);

// convert the time into a distance
cm = microsecondsToCentimeters(duration);

// control the buzzer
if(cm<threshold){ // if an object is closer than the threshold
// map the distance to the buzzer delay time
buzzerDelay = map(cm,0,threshold,buzzerDelayMin,buzzerDelayMax);
// beep the buzzer
digitalWrite(buzzerPin,HIGH);
delay(buzzerDelay);
digitalWrite(buzzerPin,LOW);
delay(buzzerDelay);
}
else{ // if an ob ject is farther away than the threshold
digitalWrite(buzzerPin,LOW); // turn buzzer off
}

// uncomment the next line for debugging
// Serial.println(cm);

}


long microsecondsToCentimeters(long microseconds) {
// The speed of sound is 340 m/s or 29 microseconds per centimeter.
// The ping travels out and back, so to find the distance of the object we
// take half of the distance travelled.
return microseconds / 29 / 2;
}


( In the image with my circuit shown, I haven't turned it on now and though I understand its on a more mounted surface even on a flat or better one the reaction is the same. Which is the slight buzzing on the sensor even with nothing there.)


--------------------------------
[Administrator note: Project url: https://www.sciencebuddies.org/science- ... king-stick
Also, you noted you were including the code here and didn't. I have pasted it in from your email.]
Attachments
poop.png
poop.png (958.11 KiB) Viewed 7173 times
Screenshot 2025-01-28 172040.png
Screenshot 2025-01-28 172040.png (742.02 KiB) Viewed 7173 times
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Glitch in Object detecting goggles

Post by bfinio »

Hi - can you elaborate on "I've also checked with the serial monitor but to no avail."? Can you provide a screenshot or picture of some data from the serial monitor? It's possible for the sensor to get occasional erroneous or false readings based on how sound reflects off objects in the room, which can result in occasional beeping even when it doesn't seem like there's an object in front of the sensor.
meowrat
Posts: 2
Joined: Tue Dec 03, 2024 12:13 am
Occupation: Student

Re: Glitch in Object detecting goggles

Post by meowrat »

I've tested my sensor with my serial monitor to see if it works and it gave wonky-ish results. (I used a simple code to test to read the distances of objects in front of it) Like for starters it seemed like it changed rapidly even if I barely moved my hand. The sensor was picking up a lot of random frequencies. ( I moved my hands at a slow pace yet the numbers kept shooting high up). How am I supposed to fix this issue, because it detects other objects, like do I have to buy another sensor?
Attachments
Screenshot 2025-01-30 174721.png
Screenshot 2025-01-30 174721.png (101.85 KiB) Viewed 7141 times
Screenshot 2025-01-30 175018.png
Screenshot 2025-01-30 175018.png (103.52 KiB) Viewed 7142 times
bfinio
Expert
Posts: 964
Joined: Mon Aug 12, 2013 2:41 pm
Occupation: Lead Staff Scientist, Science Buddies
Project Question: Expert
Project Due Date: n/a
Project Status: Not applicable

Re: Glitch in Object detecting goggles

Post by bfinio »

Hi - the sensor works by detecting reflected sound, so sometimes it works better with a large, flat, rigid object like a book or sheet of cardboard. Since your hand is softer and irregularly shaped it may not reflect sound as evenly. Can you try again with a sheet of cardboard or a book, making sure you are holding it directly in front of the sensor, and see if the serial monitor readings stabilize? You can also pick the sensor up and aim it directly down at the table or at a wall.
Post Reply

Return to “Grades 6-8: Physical Science”