We bring you the stellon tones

The title sounds something provocative, because the saying is known to be a double meaning. But that is not meant here. We want to show you how to elicit your Micro Controller (AVR) tones. The idea to the blog arose because I learned after many years I deal with Raspberry Pis and Micro Controllers, that there are active and passive buzzers (buzzer). So far, I have obviously just randomly done everything well when my students programmed small programs, e.g. a reaction game, and tried to the pain threshold of my ears.

Hardware used

Any

Arduino Compatible Micro Controller (AVR)

1

KY-012 Buzzer module active

1

KY-006 Passive Piezo Buzzer Alarm Module

alternatively

35 in 1 Arduino sensor kit Modular kit and accessory kit for Arduino and other microcontrollers


The Reaction game for two players is about pressing one's button as soon as possible when the buzzer starts to buzz after a randomly chosen time. Now I know: For this you need an active buzzer, as he e.g. is also installed in our washing machine or tumble dryer. The active buzzer has a built-in oscillation circuit electronics (oscillator), which generates the buzzing tone when the voltage of 3,3V or 5V is applied. In terms of construction, one must therefore pay attention to the polarity of the buzzer. With delivery, these buzzers usually have a small sticker with the plus sign and "Remove Seal After Washing". Also on the component itself is a +. To recognize the longer leg, you have to look twice. Or one takes a small breakoutboard, as it is present in the 35 in 1 sensor kit; Here are the contacts with S (= signal) and - (minus). The third (middle) connector is not needed. You can try the buzzer with a simple sketch like Blink, where (almost) any pin can is switched with digitalWrite () each one second to high and one second to low.

The passive buzzer has no built-in oscillator; Therefore, the Micro Controller must take over this function. If you try the above sketch on a passive buzzer, you hear a soft click every second, not more. However, if we extremely shorten the pause time (delay (1) or delay (2)), you can hear a sound whose frequency we can easily calculate: one millisecond height and low means (neglecting the execution time of further commands) 500 cycles on / off per second, ie 500Hz.

With the following small snippet code you can create a short warning sound at the (previously defined) pin for the buzzer:

 int  var = 0;
 while (var < 100) {
   // do something repetitive 100 times
   digitalwrite(buzzer, HIGH);
   delay(1);
   digitalWrite(buzzer, Low);
   delay(1);
   var++;
 }

Even better is the sound production with the function tone (), which, however, can only generate one sound signal (a frequency)  on one pin. The following versions apply to the passive buzzer or a small loudspeaker connected to the corresponding pin via a 100 Ohm resistor.

The syntax is:

tone (pin, frequency) or
tone (pin, frequency, duration)

The parameters used are:

pin: The Arduino Pin on which the sound is to be generated.
frequency: The frequency of the sound in Hertz. Allowed data types: unsigned int.
duration: The duration of the sound in milliseconds (optional). Allowed data types: unsigned long.

If no third parameter for duration specified, the frequency is output on the passive buzzer (or alternatively a small speaker) until either a new frequency assignment is made or the function noTone (pin) is called.

If you use the parameter duration, this does not imply (!) that one can play a scale in this way. The following tones will not be heard, if you do not additionally insert a break with delay (xx) in the sketch. But then you do not need the parameter duration when playing a melody. So: duration Use only if a single (warning) sound is to be output for a short duration.

With regard to the frequencies, a trip to music theory or physics is necessary. The few, I remember, are chamber tone A = 440Hz as well as halving or doubling the frequency per octave. It is recommended for the variable names to use the American spelling of the notes, ie A4 = 440Hz, A3 = 220Hz, A5 = 880Hz. The numbers are the respective octaves named on the piano. Unfortunately, the other frequencies are not always integer numbers (as a reminder: required data type = unsigned int); So the cacophony is unavoidable due to the system, but since only one frequency is played, it remains bearable and the melody remains recognizable.

The best way is to define the notes at the beginning of the sketch, e.g. for the fourth octave with the chamber tone A:

 #define note_c4 262
 #define note_cs4 277
 #define note_d4 294
 #define note_ds4 311
 #define note_e4 330
 #define note_f4 349
 #define note_fs4 370
 #define note_g4 392
 #define note_gs4 415
 #define note_a4 440
 #define note_as4 466
 #define note_b4 494
 #define note_c5 523

Or you take the file pitches.h file from the Sample Sketch ToneKeyboard, in which all notes from 31 Hz to 4,978 kHz are defined.

Example Tonekeyboard

The files for the example are in the program folder for the Arduino IDE in the subdirectory "examples":

pitches.h

Copy this file and save it in your sketch folder; in the sketch then integrate it with #include "pitches.h".

On the Internet I had seen a sketch with the German children song "all my ducks ...", which sounded so terribly because no breaks between the individual notes were inserted; this really "hurts" in particular when a note is repeated. So if you want to hear single tones of a melody like from a piano, you should insert noTone() and another delay. Both breaks together determine the length of a note; Therefore, one should set these values ​​with a variable e.g. for the number of milliseconds of a quarter note.

In the following sketch I tried to improve the song, set the duration of the audible tone with int delay = 400; and the break with int pause = 100; . The length of a quarter note is thus 500ms. As described above, I have filed the Pitches.h file in the Sketch folder.

 / * All my ducks ... once played
  * The file pitches.h must be in the same file folder
  */
 
 #include "pitches.h"
 
 int Delay = 400;
 int BREAK = 100;
 
 void set up() {
   tone(7, Note_c5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_D5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_e5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_f5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_g5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);
   tone(7, Note_g5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);  
   tone(7, Note_a5);
   delay(Delay);  
   notone(7);
   delay(BREAK);
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_a5);
   delay(Delay);  
   notone(7);
   delay(BREAK);    
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_g5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);    
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_a5);
   delay(Delay);
   notone(7);
   delay(BREAK);  
   tone(7, Note_g5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);    
   tone(7, Note_f5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_f5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_f5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_f5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_e5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);    
   tone(7, Note_e5);
   delay(2*Delay);
   notone(7);
   delay(2*BREAK);    
   tone(7, Note_g5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_g5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_g5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_g5);
   delay(Delay);
   notone(7);
   delay(BREAK);    
   tone(7, Note_c5);
   delay(2*Delay);
   notone(7);
 }
 
 
 void loop() {
   // no action to be repetitive
 }
As you can see, no masterpiece of programming art. But certainly good to use if you want to simulate a two-tone siren as in a rescue vehicle.

For longer melodies, the procedure with two arrays is better suited, as described in our eBook. Here, the melody published on the Internet is linked to the title music of "Pirates of the Caribbean":
https://github.com/xitangg/-Pirates-of-the-Caribbean-Theme-Song

In the first array int notes [] the notes are listed, in the second int durations [] the respective duration of the tones. As with other examples, the melody is played only once, the program code for the execution is therefore in the void setup (); the void loop () remains empty.

Here are the explanations of the sketch:

To repeat the for loop correctly for all notes, first the number of notes totalNotes is determined with the function sizeof () . Since this function provides the number of bytes in the array must be divided by the number of bytes per integer number, ie by 2.

The respective values ​​from the arrays are cached in the variables currentNote and wait. For breaks marked in the array with 0, the function noTone () is called up.

Here only a section of the sketch void setup () :

 void set up()
 {
   const int totalnotes = sizeof(notes) / sizeof(int);
     
   // Loop Through Each Note
   for (int I = 0; I < totalnotes; I++)
  {
     contam int CurrentNote = notes[I];
     float wait = duration[I] / songspeed;
     // Play Tone IF CurrentNote Is Not 0 Frequency, OtherWise Pause (Notone)
     IF (CurrentNote != 0)
    {
       tone(buzzer, notes[I], wait); // Tone (Pin, Frequency, Duration)
    }
     else
    {
       noTone(buzzer);
    }
     // delay is used to wait for Tone to Finish Playing Before Moving To Next Loop
     delay(wait);
  }
 }

There are other program libraries for the ATMEGA328P / ATMEGA16U2 microcontroller boards (AVR) for the output of tones, e.g. to spend two tones at different pins at the same time. But those are partly very complex and as a result as sobering as the simple edition of tones with tone (). Our versatile Micro Controller is suitable for many applications, but certainly not as the synthesizer.

Remark to the above mentioned GPIO Zero Reaction Game for Raspberry Pi

Here is the link to the small game for the Raspberry Pi, for which buttons and LEDs (plus resistors) are used in addition to the buzzer. Further description can be found in the source code.

Download gpiozero_reactiongame_nochenating.py

Conclusion

If you want to spend a short warning sound, you should use an active buzzer that is turned on and off like an LED (or in parallel). It is important to take account of the polarity.

If you want to program a two-tone siren (which you hear more clearly) or a brief detection melody (jingle), you should grab the passive buzzer or a small speaker.

For arduinoProjects for beginnersRaspberry pi

Leave a comment

All comments are moderated before being published

Recommended blog posts

  1. Install ESP32 now from the board manager
  2. Lüftersteuerung Raspberry Pi
  3. Arduino IDE - Programmieren für Einsteiger - Teil 1
  4. ESP32 - das Multitalent
  5. OTA - Over the Air - ESP programming via WLAN