const int buzzerPin = 7;
const int flamePin = 10;
int Flame = HIGH;
int redled = 8;
void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(flamePin, INPUT);
Serial.begin(12000);
}
void loop()
{
Flame = digitalRead(flamePin);
if (Flame== HIGH)
{
digitalWrite(redled, HIGH);
int i=0;
do {
digitalWrite(buzzerPin, HIGH);
tone(buzzerPin,450);
delay(200);
noTone(buzzerPin);
delay(200);
}while (i<2);
}
else
{
digitalWrite(buzzerPin, LOW);
digitalWrite(redled, LOW);
}
}
-
test the buzzer with the simplest code possible– jsotolaCommented Jun 9, 2021 at 16:25
-
We don't know anything about your buzzer or circuit, so there's little to offer in the way of help. Please make sure when posting code that it's formatted as code so people can actually read it. It's also helpful to include what does work, e.g., there are other factors in your code that could make it seem like the buzzer isn't working.– Dave NewtonCommented Jun 9, 2021 at 16:40
-
2450 is too low frequency for a piezo. use 3000– Juraj ♦Commented Jun 9, 2021 at 17:53
Add a comment
|
1 Answer
Here are the modifications I have done
- I changed the Piezo Buzzer frequency to 3000 Hz The green Button simulates the Flame signal
- Change the RED LED from pin 8 to Pin 13 so that I don't have to use an external LED
Here is the simulation output
Observe the onboard LED going HIGH and the buzzer going off. You can find the project link here where you can test with different Piezo buzzer frequency.
Code
const int buzzerPin = 7;
const int flamePin = 10;
int Flame = HIGH;
int redled = 13;
void setup()
{
pinMode(buzzerPin, OUTPUT);
pinMode(redled, OUTPUT);
pinMode(flamePin, INPUT);
Serial.begin(12000);
}
void loop()
{
Flame = digitalRead(flamePin);
if (Flame== HIGH)
{
digitalWrite(redled, HIGH);
int i=0;
do {
digitalWrite(buzzerPin, HIGH);
tone(buzzerPin,3000);
delay(200);
noTone(buzzerPin);
delay(200);
}while (i<2);
}
else
{
digitalWrite(buzzerPin, LOW);
digitalWrite(redled, LOW);
}
}
Disclaimer: I am not the Wokwi creator but I support it since it is open source and free to use
-
-
@Juraj I Doubt it. if the flame signal is high, he is triggering the alarm Commented Jun 13, 2021 at 12:55