everyone. I am working on learning Arduino from scratch and I am on the buzzer lesson. The tutorial I am following consists on writing a code to get an input from the user in the form of an integer number, and, if it is greater than 10 (or whatever threshold you set it to) it will buzz.
I wrote the code exactly as the tutorial said but the buzzer would not buzz, it would do a weird clicking sound instead. I searched for a reason everywhere and could not find an explanation. Most questions in forums were solved by changing the positive wire from the 5V to a regular pin on the board, and those problems were due to the fact that the people asking the questions were not aware that the buzzer was a passive one.
Anyway, I managed to get a buzz by switching from digital to analogWrite and typing a value less than 255. It seemed weird that 255 and HIGH would produce the click, but values less than 255 would buzz, so I tried several analogWrite values:
- At 255 the buzzer does the weird clicking.
- At 254 the buzzer does a very faint buzz.
The buzzing got louder as I wrote lower numbers until it got stable at around 250, and then got less loud as I decreased the number.
Does anyone know why that is? Is the voltage too much for the buzzer at 255/HIGH? I tried a resistor but still got nothing but clicking with the program set to 255/HIGH.
This is mostly out of curiosity, but I'd still appreciate it if you could answer it.
Here is the code ("Numero" means "number" and "Dame un número" means "Give me a number":
int Numero;
int buzz=10;
int DLY=2000;
String msg="Dame un numero.";
void setup() {
Serial.begin(9600);
pinMode(buzz,OUTPUT);
}
void loop() {
Serial.println(msg);
while (Serial.available()==0){
}
Numero=Serial.parseInt();
if (Numero>25){
Serial.println(Numero);
analogWrite(buzz,10);
delay(DLY);
digitalWrite(buzz,LOW);
}
}