Electronics

Buzzer Passive Speaker Module Ky-006

AED 5.25

1

Description

You can do a lot of interactive project, the most commonly used is for sound and light shows.
Try to the experiment with digital sound.
Buzzer module make the simplest sound.
Just change the frequency, you can hear different sound (like alarm).
It is used on your daily appliance, like PC, phones and so on.





//code for Buzzer Passive Speaker Module
int buzzer = 10;
int interrupt= 0;// connected on pin 2;
int k = 1;

unsigned long button_time = 0;  
unsigned long last_button_time = 0; 

void setup() {
  
  pinMode (buzzer, OUTPUT) ;// set the digital IO pin mode,
  
  pinMode (2,INPUT);  // anable the pullup resistor
  digitalWrite(2,HIGH); 
  
  attachInterrupt(interrupt, soundOnOrOff, FALLING);
  Serial.begin(9600);

}

void loop() {

  sound();

}

void soundOnOrOff(){
  button_time = millis();
//  Serial.println("interupted");
  if (button_time - last_button_time > 1000)
  {
  if(k == 1){
    Serial.println("set sound off");
  digitalWrite (buzzer, LOW) ;
  k = 0;} //ends while loop
  else{
  k = 1; //start while loop
  Serial.println("set sound on");
  }
  last_button_time = button_time;
  }
  
}
void sound(){
  
  while (k==1)
  {
    
    
    for (int i = 0; i <80; i++) 
    {
      digitalWrite (buzzer, HIGH) ;// send voice
      delay (1) ;// Delay 1ms
      digitalWrite (buzzer, LOW) ;// do not send voice
      delay (1) ;// delay ms
    }
    for (int i = 0; i <100; i++) 
    {
      digitalWrite (buzzer, HIGH) ;// send voice
      delay (2) ;// delay 2ms
      digitalWrite (buzzer, LOW) ;// do not send voice
      delay (2) ;// delay 2ms
    }
   
  }
}