Loading...

You have no items in your shopping cart.

 
 
 
×

Registration

Profile Informations

Login Datas

or login

First name is required!
Last name is required!
First name is not valid!
Last name is not valid!
This is not an email address!
Email address is required!
This email is already registered!
Password is required!
Enter a valid password!
Please enter 6 or more characters!
Please enter 16 or less characters!
Passwords are not same!
Terms and Conditions are required!
Email or Password is wrong!
Captcha is required!
Captcha is wrong!

Tone Generator Library For Buzzer Electronic Brick

In order to compatible with more devlopment boards, we update the Buzzer Electronic Brick with driver circuit instead of directly using the I/O.

But it also led to a problem, it's not compatible with the readymade Arduino Tone library. So we have to modify the library to fix this issue, the new Arduino tone library will support the Arduino 1.x IDE, and can drive the Buzzer Electronic Blick to play songs. You can download the library HERE.

Here we briefly introduce some functions in Tone library:

makeTone.begin(speakerPin)
Function:to select to control pins of buzzer
Parameters:

  • speakerPin : Pin N.O. for connecting to S port of buzzer

makeTone.play(note)
Function:to let buzzer make a sound
Parameters:

  • note: value of certain note

makeTone.stop()
Function:to stop making sounds

A song is composed of a number of notes, and a note corresponds to a frequency. If we know the corresponding frequency, and let Iteaduino output different frequencies to control the buzzer, the buzzer will make corresponding sounds. The relationship between different note and frequency is shown in the following three tables:

Tone

Note

 

1
.

 

2
.

 

3
.

 

4
.

 

5
.

 

6
.

 

7
.

A

221

248

278

294

330

371

416

B

248

278

294

330

371

416

467

C

131

147

165

175

196

221

248

D

147

165

175

196

221

248

278

E

165

175

196

221

248

278

312

F

175

196

221

234

262

294

330

G

196

221

234

262

294

330

371

Tone

Note

 

1

 

2

 

3

 

4

 

5

 

6

 

7

A

441

495

556

589

661

742

833

B

495

556

624

661

742

833

935

C

262

294

330

350

393

441

495

D

294

330

350

393

441

495

556

E

330

350

393

441

495

556

624

F

350

393

441

495

556

624

661

G

393

441

495

556

624

661

742

 

 

 

 

 

Tone

Note

 

.

1

 

.

2

 

.

3

 

.

4

 

.

5

 

.

6

 

.

7

A

882

990

1112

1178

1322

1484

1665

B

990

1112

1178

1322

1484

1665

1869

C

525

589

661

700

786

882

990

D

589

661

700

786

882

990

1112

E

661

700

786

882

990

1112

1248

F

700

786

882

935

1049

1178

1322

G

786

882

990

1049

1178

1322

1484

The notation of "Happy Birthday"

This song is in F, according to the corresponding table of notes to frequency, we can find that the corresponding frequency of 5 is 262Hz. In file Tone_Itead.h, there is "# define NOTE_C4 262" , thus we can define frequency 262Hz as NOTE_C4, and other notes can also be analogized in the same way, and finally we can get the complete codes of all the notes  { NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4,NOTE_D4,NOTE_AS4,NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4}

The complete program used in the test is as below:

 

 

#include 
int speakerPin = 3;
Tone makeTone;

// notes to play; see Tone_Itead.h for frequencies; 
int notes[] = { 
 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_F4, NOTE_E4, 
 NOTE_C4, NOTE_C4, NOTE_D4, NOTE_C4, NOTE_G4, NOTE_F4,
 NOTE_C4, NOTE_C4, NOTE_C5, NOTE_A4, NOTE_F4, NOTE_E4, NOTE_D4,
 NOTE_AS4, NOTE_AS4, NOTE_A4, NOTE_F4, NOTE_G4, NOTE_F4}; 

// number of beats for each note
int beats[] = { 
 1, 1, 2, 2, 2, 4, 
 1, 1, 2, 2, 2, 4, 
 1, 1, 2, 2, 2, 2, 2, 
 1, 1, 2, 2, 2, 4}; 

// Calculate song length
int songLength = sizeof(notes) / sizeof(int); 
int tempo = 220; // in milliseconds

void playNote(int note, int beat){
 makeTone.stop(); // speaker reset
 makeTone.play(note); // play tone 
 delay(tempo * beat); // for specified number of beats
 makeTone.stop(); // speaker reset
 delay(tempo / 4); // pause between notes
}

void setup() {
 pinMode(speakerPin,OUTPUT);
 makeTone.begin(speakerPin); // set up piezo speaker
}

void loop() {
 for (int i = 0; i < songLength; i++) 
{
  playNote(notes[i],beats[i]); // make sound
}
  }