Small Passive buzzer module (KY-006) [D023]



https://www.youtube.com/watch?v=ASjVjjn9aGs


* GitHub : https://github.com/rdiot/rdiot-d023.git


* Specs

Buzzer: The Buzzer is an integrated electronic Xiangqi structure, with a DC power supply, widely used in computers, printers, photocopiers, alarms, electronic toys, automotive electronics, telephones, timers, etc. electronic products for sound devices.

Buzzer Category: divided into piezoelectric buzzer and magnetic buzzer

Buzzer circuit pattern symbol: buzzer in the circuit by the letter "H" or "HA" (the old standard with the "FM", "LB", "JD", etc.) respectively.


* Contents

- Connect

S ----- D2

middle ----- 5V

- ----- GND


- Key Code

#include <Wire.h> 

#include <LiquidCrystal_I2C.h>


LiquidCrystal_I2C lcd(0x27,20,4);  // LCD2004

int S = 2 ;// connect the I/O pin on the buzzer to this


// TONES ==========================================

// Start by defining the relationship between 

// note, period, & frequency. 

#define m_c 3830 // 261 Hz 

#define d 3400 // 294 Hz 

#define e 3038 // 329 Hz 

#define f 2864 // 349 Hz 

#define g 2550 // 392 Hz 

#define a 2272 // 440 Hz 

#define b 2028 // 493 Hz 

#define C 1912 // 523 Hz 

// Define a special note, 'R', to represent a rest

#define R 0

 

// MELODY and TIMING =======================================

// melody[] is an array of notes, accompanied by beats[], 

// which sets each note's relative length (higher #, longer note) 

int melody[] = { g, g, a, a, g, g, e, g, g, e, e, d };

int beats[] = { 16, 16, 16, 16, 8, 8, 32, 16, 16, 8, 8, 16 };

int MAX_COUNT = sizeof(melody) / 2; // Melody length, for looping.

 

// Set overall tempo

long tempo = 40000; //10000

// Set length of pause between notes

int pause = 1000; //1000

// Loop variable to increase Rest length

int rest_count = 100; //<-BLETCHEROUS HACK; See NOTES

 

// Initialize core variables

int tone_ = 0;

int beat = 0;

long duration = 0;

 

// PLAY TONE ==============================================

// Pulse the speaker to play a tone for a particular duration

void playTone() {

 long elapsed_time = 0;

 if (tone_ > 0) { // if this isn't a Rest beat, while the tone has 

 // played less long than 'duration', pulse speaker HIGH and LOW

 while (elapsed_time < duration) {

 

 digitalWrite(S, HIGH);

 delayMicroseconds(tone_ / 2);

 

 // DOWN

 digitalWrite(S, LOW);

 delayMicroseconds(tone_ / 2);

 

 // Keep track of how long we pulsed

 elapsed_time += (tone_);

 }

 }

 else { // Rest beat; loop times delay

 for (int j = 0; j < rest_count; j++) { // See NOTE on rest_count

 delayMicroseconds(duration);

 }

 }

}


void setup()

{

  lcd.init();  // initialize the lcd 

  lcd.backlight();

  lcd.print("start LCD2004");


  pinMode(S, OUTPUT);

  delay(1000);


  lcd.clear();

}


void loop()

{

 

  lcd.setCursor(0,0);

  lcd.print("D023:KY006PassiveBuz");


  for (int i = 0; i<MAX_COUNT; i++) {

  tone_ = melody[i];

  beat = beats[i];

 

  duration = beat * tempo; // Set up timing

 

  lcd.setCursor(0,1);

  lcd.print("melody[" + (String)i + "] = " + (String)tone_ + "  ");


  lcd.setCursor(0,2);

  lcd.print("beat[" + (String)i + "] = " + (String)beat + "  ");


  lcd.setCursor(0,3);

  lcd.print("durati[" + (String)i + "] = " + (String)duration + " ");



  playTone();

  // A pause between notes...

  delayMicroseconds(pause); 

  }

}

Posted by RDIoT

댓글을 달아 주세요