Lesson 10 Play the Music

1831

Overview

In this lesson, we will program the Arduino to control a passive buzzer, and then make the passive buzzer play music.

Requirement

- 1* Arduino MEGA 2560

- 1* USB Cable

- 1* NPN Transistor (8050)

- 1* 1KΩ Resistor

- 1* Passive Buzzer

- 1* LED

- 1* 220Ω Resistor

- 1* Breadboard

- Several Jumper Wires

Principle

As long as you send the square wave signals to a passive buzzer with different frequency, then the passive buzzer will make different sound.

1.png

 

Key function:

●tone()

Generates a square wave of the specified frequency (and 50% duty cycle) on a pin. A duration can be specified, otherwise the wave continues until a call to noTone(). The pin can be connected to a piezo buzzer or other speaker to play tones.

Only one tone can be generated at a time. If a tone is already playing on a different pin, the call to tone() will have no effect. If the tone is playing on the same pin, the call will set its frequency.

Use of the tone() function will interfere with PWM output on pins 3 and 11 (on boards other than the Mega).

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

Syntax

tone(pin, frequency)

tone(pin, frequency, duration)

Parameters

pin: the pin on which to generate the tone

frequency: the frequency of the tone in hertz - unsigned int

duration: the duration of the tone in milliseconds (optional) - unsigned long

Returns

nothing

●noTone()

Stops the generation of a square wave triggered by tone(). Has no effect if no tone is being generated.

NOTE: if you want to play different pitches on multiple pins, you need to call noTone() on one pin before calling tone() on the next pin.

Syntax

noTone(pin)

Parameters

pin: the pin on which to stop generating the tone

Returns

nothing

 

 

Procedures

1. Build the circuit

2.png

2. Program

_10_music.ino

/***********************************************************
File name: 10_music.ino
Description:  you can hear the passive buzzer playing music,
              and you can see flickering LED follow the rhythm
              of the music at the same time.
Website: www.adeept.com
E-mail: support@adeept.com
Author: Tom
Date: 2015/12/27 
***********************************************************/
#define NTD0 -1                    //bass  1#   2#   3#   4#   5#   6#   7# 
#define NTD1 294                   // A    221  248  278  294  330  371  416 
#define NTD2 330                   // B    248  278  294  330  371  416  467
#define NTD3 350                   // C    131  147  165  175  196  221  248
#define NTD4 393                   // D    147  165  175  196  221  248  278
#define NTD5 441                   // E    165  175  196  221  248  278  312
#define NTD6 495                   // F    175  196  221  234  262  294  330
#define NTD7 556                   // G    196  221  234  262  294  330  371
                                   
#define NTDL1 147                  //Alto  1    2    3    4    5    6    7
#define NTDL2 165                  // A    441  495  556  589  661  742  833
#define NTDL3 175                  // B    495  556  624  661  742  833  935
#define NTDL4 196                  // C    262  294  330  350  393  441  495
#define NTDL5 221                  // D    294  330  350  393  441  495  556
#define NTDL6 248                  // E    330  350  393  441  495  556  624
#define NTDL7 278                  // F    350  393  441  495  556  624  661  
                                   // G    393  441  495  556  624  661  742
#define NTDH1 589
#define NTDH2 661                  //high pitch  1#   2#   3#   4#   5#   6#   7#  
#define NTDH3 700                  //    A       882  990  1112 1178 1322 1484 1665   
#define NTDH4 786                  //    B       990  1112 1178 1322 1484 1665 1869 
#define NTDH5 882                  //    C       525  589  661  700  786  882  990
#define NTDH6 990                  //    D       589  661  700  786  882  990  1112
#define NTDH7 112                  //    E       661  700  786  882  990  1112 1248
//c pinlv                          //    F       700  786  882  935  1049 1178 1322
#define WHOLE 1                    //    G       786  882  990  1049 1178 1322 1484
#define HALF 0.5
#define QUARTER 0.25
#define EIGHTH 0.25
#define SIXTEENTH 0.625

int tune[]=      //Music tones
{
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD3,NTD2,NTD2,
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD2,NTD1,NTD1,
  NTD2,NTD2,NTD3,NTD1,
  NTD2,NTD3,NTD4,NTD3,NTD1,
  NTD2,NTD3,NTD4,NTD3,NTD2,
  NTD1,NTD2,NTDL5,NTD0,
  NTD3,NTD3,NTD4,NTD5,
  NTD5,NTD4,NTD3,NTD4,NTD2,
  NTD1,NTD1,NTD2,NTD3,
  NTD2,NTD1,NTD1
};
float durt[]= //Each musical tone delay time
{
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1+0.5,0.5,1+1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,1,
  1+0.5,0.5,1+1,
  1,1,1,1,
  1,0.5,0.5,1,1,
  1,0.5,0.5,1,1,
  1,1,1,1,
  1,1,1,1,
  1,1,1,0.5,0.5,
  1,1,1,1,
  1+0.5,0.5,1+1,
};
int length;
int tonepin=6;  //Buzzer connected digital pin 6
int ledp=3;     //LED connected digital pin 3
void setup()
{
  pinMode(tonepin,OUTPUT); //Digital pin 6 output mode
  pinMode(ledp,OUTPUT);    //Digital pin 3 output mode
  length=sizeof(tune)/sizeof(tune[0]);//Calculate the total number of musical tones
}
void loop()
{ 
  for(int x=0;x<length;x++)
  {
    tone(tonepin,tune[x]);    //Open buzzer
    digitalWrite(ledp, HIGH); //LED On
    delay(400*durt[x]);       //Tone Delay. Note:400 can be replaced
    digitalWrite(ledp, LOW);  //LED Off
    delay(100*durt[x]);       //Tone Delay. Note:100 can be replaced
    noTone(tonepin);          //Turn off the buzzer
  }  
  delay(2000);                //delay 2S
}


3. Compile the program and upload to Arduino MEGA 2560 board

Now, you can hear the music of passive buzzer with blinking an LED.

3.jpg