Controlling the buzzer

Let's now explore the usage of the buzzer. Let's understand the relationship between the buzzer and the frequency of the sound.

!Important Information

The Nanoshield Interface's buzzer (click on the link for more information) is connected to the pin D5# on Arduino. The buzzer works with frequencies to emit sounds.

The frequency is the characteristic through which our ears distinguish wether a sound is high pitch or low pitch. This characteristic is related to the amount of cycles (vibrations) of a sound wave in one second and its unit is Hertz (Hz).

The frequency range that the human ear is able to understand is between 20 Hz and 20.000Hz or 20 kHz (1kHz = 1 Kilohertz or 1000 Hz).

By applying an electrical signal in a certain frequency, the buzzer produces a musical tone. The tones vary according to the frequency used.

The buzzer is a signalling piezoelectric device. You can use it in various ways in your project. For instance, a click, a beep or short sound can indicate that a button was pressed, or that a sensor was activated.

!Identifying the Components

On the figure below, we can see the position of the buzzer on the Interface Nanoshield.

Buzzer Interface

!Code Components

Functions

tone([pin],[frequency]):

You can make sounds with a buzzer using the function tone(). In order to use it, you need only to tell the pin to which the buzzer is connected and which frequency (in Hertz) you want. For example tone(5, 4000); produces a frequency of 4 kHz on the pin D5 on Arduino.

noTone([pin]):

In order to stop the sound, we must use another function called noTone(). This function receives as parameters only the pin, without the necessity to specify the frequency. For instance, noTone(5);.

!Running on Arduino

Run the code below on your Arduino to make the buzzer produce musical tones.

// Pin connected to buzzer
int buzzer = 5;

void setup() 
{
    // Defines the Buzzer pin as output 
    pinMode(buzzer,OUTPUT);    
}

void loop() 
{
    // Sounds the buzzer at the frequency relative to the note C in Hz
    tone(buzzer,261);    
    // Waits some time to turn off
    delay(200);
    //Turns the buzzer off
    noTone(buzzer); 
    // Sounds the buzzer at the frequency relative to the note D in Hz   
    tone(buzzer,293);             
    delay(200);    
    noTone(buzzer); 
    // Sounds the buzzer at the frequency relative to the note E in Hz
    tone(buzzer,329);      
    delay(200);
    noTone(buzzer);     
    // Sounds the buzzer at the frequency relative to the note F in Hz
    tone(buzzer,349);    
    delay(200);    
    noTone(buzzer); 
    // Sounds the buzzer at the frequency relative to the note G in Hz
    tone(buzzer,392);            
    delay(200);
    noTone(buzzer); 
}

!Your Turn!

Try to modify the code above using the green LED to show that the buzzer is on and the yellow LED to show when the buzzer is off.

Answer

//Pin connected to buzzer
int buzzer = 5;
// Matching the LEDs to the corresponding pins
int yellowLed = A1;
int greenLed= A2; 

void setup() {     // Defines the buzzer pin as output     pinMode(buzzer,OUTPUT);     // Initialise each LED pin as output     pinMode(yellowLed,OUTPUT);     pinMode(greenLed,OUTPUT); }

void loop() {     // Turns the yellow LED on     digitalWrite(yellowLed, HIGH);     // Waits one second     delay(1000);     // Turns the yellow LED off     digitalWrite(yellowLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,261);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,293);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,329);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,349);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,261);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     // Turns the green LED on     digitalWrite(greenLed, HIGH);     // Sounds the buzzer with the received frequency     tone(buzzer,392);     delay(200);     noTone(buzzer);     // Turns the green LED off     digitalWrite(greenLed, LOW);     digitalWrite(yellowLed, HIGH);     delay(100); }