Lesson 3

Lesson 3: Making Sounds with Apeiros

In this lesson you will learn how to create buzzer tone sounds using the ApeirosBuzzer class library.

Prerequisites

Before starting this lesson be sure to follow the Initial Setup & Configuration. Also, we strongly recommend that you take some time to read "Getting Started with Arduino and Genuino products".

Introduction

Apeiros has been expertly designed to include a magnetic buzzer. You can easily control the buzzer from a sketch by using several functions contained within the ApeirosBuzzer class library. Making sounds with the buzzer is a great way for Apeiros to announce something to the world! A magnetic buzzer vibrates a small disc in order to generate sound waves. If you want to learn more about magnetic buzzers, then please visit this great resource!

Apeiros Class Library Functions

The ApeirosBuzzer class library contains a total of (3) different functions for controlling the buzzer. See code below for a list of all related buzzer functions. The ApeirosBuzzer class library makes use of the Arduino tone() function and so the minimum frequency is 31 Hertz and the highest frequency is 65,535.

void playTone(uint16_t buzzFreq);
void playToneForDuration(uint16_t buzzFreq, uint32_t buzzDuration);
void noSound();

Recall that we need to first declare an instance of the ApeirosBuzzer class library. However, we do NOT need to add anything to the setup() function.

// Declare Apeiros class instances.//
/***********************************/
ApeirosBuzzer buzzer;
/***********************************/

void setup() {
  // put your setup code here, to run once:

}

Now suppose that we want to play a low frequency tone sound on the buzzer. Low frequency simply means that the buzzer will be vibrating fewer times per second. When talking about frequency we use the units of Hertz [Hz], which simply means times per second. We can use the playTone() function and specify the desired frequency, which in this case will be 50 Hz.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Play a tone at 50 Hz.
  buzzer.playTone(50);
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

We can play a higher frequency tone sound too.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Play a tone at 1175 Hz.
  buzzer.playTone(1175);
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

The buzzer will continue to operate at the specified frequency after calling the playTone() function. If we want to stop the buzzer, then we must call the noSound() function.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Stop playing buzzer sounds.
  buzzer.noSound();
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

Or you can simply make another call to the playTone() function and specify a new frequency.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Play a tone at 500 Hz.
  buzzer.playTone(500);

  delay(1000);  // Delay for a specified amount of time in milliseconds

  // Play a tone at 123 Hz.
  buzzer.playTone(123);
  
  delay(150);  // Delay for a specified amount of time in milliseconds
}

What if we want to play multiple tone sounds in sequence, but don't want to add a call to the delay() function after each call to playTone(). We can make use of the playToneForDuration() function and specify a frequency and duration of play.

void loop() {
  // put your main code here, to run repeatedly:
 
  // Play a tone at 500 Hz for 250 milliseconds.
  buzzer.playToneForDuration(500, 250);

  // Play a tone at 1175 Hz for 500 milliseconds.
  buzzer.playToneForDuration(1175, 500);

  // Play a tone at 123 Hz for 1000 milliseconds.
  buzzer.playToneForDuration(123, 1000);
  
  delay(150);
}

EXPERIMENT


 

Play around with the buzzer functions and test out playing different tone sounds. Can you create a custom sketch that plays a unique sequence of tone sounds? Recall that the minimum frequency is 31 Hz and the maximum frequency is 65,535 Hz. Try playing a tone sound at 31 Hz and then at 65,535 Hz. What happens when you play a tone sound at 65,535 Hz? Apeiros uses a magnetic buzzer that has a rated frequency of 2,730 Hz and so exceeding this value may produce somewhat muted tone sounds. A magnetic buzzer must vibrate a small disc and so it will be limited in its abilty to vibrate at higher frequencies. Try playing a tone sound at 2,730 Hz and observe the difference.