Help on water level project

so I am just trying to make a little water level project to help me get to grips with arduino and I decided to add in a passive buzzer. the code worked fine when it was like this:

  if (val > 100) {
   tone(BuzzerPin, 440);
} else (val < 100) {
    noTone(BuzzerPin);
  }

but it just made a low sound, not like a note, when it was programmed like this:

  if (val > 300) {
   tone(BuzzerPin, 659);
  } else if (val > 200 && val < 300) {
   tone(BuzzerPin, 523);
  } else if (val > 100 && val < 200) {
   tone(BuzzerPin, 440);
  } else (val < 100) {
    noTone(BuzzerPin);
  }

could anyone help me?

What kind of buzzer are you using?

  • Some just buzz when you apply the supply to them;
  • Some require you to provide a drive at the appropriate frequency (eg, using tone).

That could happen If you use tone to drive the first type...

it is passive, so requires a square wave, so i cant just connect it to 5V and GND pins. on my arduino, it is connected to the 5V, GND and Digital 8 pin.

also, the noTone works, just not the tone.

it sounds like a tazer

changing to analog pin does not change anything

Please post a link to the details of the "buzzer" you are using.

Please also post a schematic - words are a very poor way to describe electronic circuits.

Some good, clear photos of your setup would also help.

Study this: How to get the best out of this forum

ok, bear with me

here are the photos:


I am having trouble finding schematics, probably because i am using the complete starter set by elegoo.

Post the code that is making the odd sounds.

I mean a complete sketch. If it's software it's in the code you didn't post.

a7

Ok, just making lunch. Will be 5 seconds

here it is:


#define sensorPower 7
#define sensorPin A0


int val = 0;


int BuzzerPin = 8;

void setup() {

  pinMode(sensorPower, OUTPUT);
  

  digitalWrite(sensorPower, LOW);
  
  Serial.begin(9600);
}

void loop() {
 
  int level = readSensor();
 
  Serial.print("Water level: ");
  Serial.println(level);

  if (val > 300) {
   tone(BuzzerPin, 659);
  } else if (val > 200 && val < 300) {
   tone(BuzzerPin, 523);
  } else if (val > 100 && val < 200) {
   tone(BuzzerPin, 440);
  } else (val < 100); {
   noTone(BuzzerPin);
  }
  
  delay(1);
  
}


int readSensor() {
  digitalWrite(sensorPower, HIGH);  
  delay(10);              
  val = analogRead(sensorPin);   
  digitalWrite(sensorPower, LOW);   
  return val;             
}

You may need a PWM pin. Pin 8 is not PWM. Try pin 9

will do

I'm in transit, so all I can suggest trying is to increase, for the moment, the delay() in the loop.

An experiment, try 1000 milliseconds.

     delay(1000);

All I can think is that you are changing the frequency so often it makes its own impression on what we hear.

HTH

a7

thanks

Try it, it cannot hurt. but tone() does not meet PWM capable pins.

a7

didn't work.

i understand that you are in transit and therefore dont require an immediate response (or response at all), but all changing the delay does is increase the amount of time in between the clicking sound.

hi all, just realised a couple of things. one, i was comparing the wrong variable. i should've been comparing level rather then val. Also, another thing causing an issue was the condition provided with my final else statement. I realised that else statements don't require a condition.