anorako
September 4, 2023, 10:26am
1
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?
awneil
September 4, 2023, 10:31am
2
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...
anorako
September 4, 2023, 10:34am
3
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.
anorako
September 4, 2023, 10:36am
4
also, the noTone
works, just not the tone
.
anorako
September 4, 2023, 10:45am
6
changing to analog pin does not change anything
awneil
September 4, 2023, 10:52am
7
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
anorako
September 4, 2023, 11:02am
9
here are the photos:
I am having trouble finding schematics, probably because i am using the complete starter set by elegoo.
alto777
September 4, 2023, 11:10am
10
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
anorako
September 4, 2023, 11:15am
11
Ok, just making lunch. Will be 5 seconds
anorako
September 4, 2023, 11:18am
12
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;
}
2112
September 4, 2023, 11:29am
13
You may need a PWM pin. Pin 8 is not PWM. Try pin 9
alto777
September 4, 2023, 11:30am
15
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
alto777
September 4, 2023, 11:32am
17
2112:
You may need a PWM pin.
Try it, it cannot hurt. but tone () does not meet PWM capable pins.
a7
anorako
September 4, 2023, 11:36am
19
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.
anorako
September 4, 2023, 11:49am
20
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.