Click here to Skip to main content
15,888,610 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm testing my sound sensor with a piezo buzzer and everytime I plug it into pin 11 while it's grounded it's just keeps buzzing.
I'm new to this so I'm not sure how to identify problems in coding
This is the code I used: (I also followed a youtube video)

const int buz=11; // ledpin and soundpin are not changed throughout the process
const int sound=A5;
const int threshold=100; // sets threshold value for sound sensor
void setup() {
  

pinMode(buz,OUTPUT);
pinMode(sound,INPUT);
}
void loop() {
int soundsens=analogRead(sound); // reads analog data from sound sensor
if (soundsens>=threshold) {
digitalWrite(buz,HIGH); //turns buzzer on
delay(1000);
}
else{
digitalWrite(buz,LOW);
}
}


What I have tried:

I tried setting the threshold higher
Posted
Updated 5-Mar-22 7:28am
v3
Comments
Richard MacCutchan 5-Mar-22 11:29am    
What is the value returned in soundsens?

First, try connecting the buzzer to the circuit, and upload an empty project. Do you get sound?

Next, eliminate the senor reading. Eliminate the HIGH call. Do you get sound?

Next, with the high call eliminated, start Serial writing your sensor values to make sure they are the result you expect.

This is the kind of thing you should be trying so you can come to the forum with some more information. It will help you get answers more quickly.
 
Share this answer
 
v2
Comments
CPallini 5-Mar-22 16:08pm    
5.
Calling a function "loop" does not mean that is does any looping: it executes once and once only. So if it turns it on to start with, it stays turned on.

But ... your code doesn't even call it, or have any entry point code shown - so at a guess you app starts with an empty main function and exits immediately. So if the buzzer is on when the app starts, it stays on ... forever.

Throw youtube tutorials in the bin: they are nearly all completely useless as they miss out far too much ...
 
Share this answer
 
Comments
honey the codewitch 5-Mar-22 12:21pm    
This is the Arduino framework. setup() is the entry point, and loop() is called repeatedly by the framework for the life of the app once setup exits. There is no exiting "main" - main is implemented as main() { setup(); while(true) loop(); } basically (simplified)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900