Generate tones with the Arduino and a speaker

FunduinoLast updated on

Vom einzelnen Piepton bis zur Melodie mit dem passiven Lautsprecher

Mit dem Arduino kannst du auf verschiedene Arten Töne erzeugen. Am einfachsten geht das mit einem aktiven Lautsprecher (active buzzer), den du lediglich an die Spannung von 5V anschließt. Die Tonerzeugung übernimmt eine im Inneren eingebaute Elektronik. Der Nachteil: Ein active buzzer kann nur einen einzigen Ton erzeugen – Melodien oder Sirenengeräusche sind damit nicht möglich.

Aufgabe: Mit einem passiven Lautsprecher erzeugst du unterschiedliche Töne und eine Melodie.

Underside of speaker and piezo speaker side by side – the speaker has a visible green circuit board
Bottom: speaker on the left, piezo speaker on the right
Top side of speaker and piezo speaker compared
Top side: the speaker on the left, the piezo speaker on the right
1 / 2

Active and passive speakers – a brief overview

With a passive speaker (passive buzzer), you can use the Arduino microcontroller to generate different tones, melodies, or siren signals, since the passive buzzer contains no electronics that determine the tone.

The pictures in this guide show you how piezo speakers and speakers differ. The speaker is shown on the left in each case, and the piezo speaker on the right. You can identify the speaker most easily by the visible green circuit board on the underside.

Connect the speaker

Tone generation is primarily based on the command tone(x, y). The x value specifies the pin to which the positive side of the speaker is connected, and the y value specifies the pitch. An example:

Circuit diagram with piezo speaker on pin 8 of the Arduino UNO
Circuit diagram: Piezo speaker connected to the Arduino UNO
How the tone() command works
tone(8, 100); // The speaker on pin 8 is activated at a pitch of "100"
delay(1000); // Pause for 1000 milliseconds, or 1 second - the speaker remains active for this long.
noTone(8); // The speaker on pin 8 is deactivated.

Simple sound generation

In the first sketch, the speaker emits a tone for one second, then remains silent for one second.

Program code - Simple tone generation
// Im Setup werden keine Informationen benötigt.
// Die Spannungsausgabe für den Piezo-Lautsprecher legt der Arduino-Befehl "tone" automatisch fest.
void setup()
{
}
void loop()
{
  tone(8, 100); // Im Hauptteil wird nun mit dem Befehl "tone(x, y)" ein Ton abgegeben.
  delay(1000); // mit einer Dauer von 1 Sekunde
  noTone(8); // Der Ton wird abgeschaltet
  delay(1000); // Der Lautsprecher bleibt eine Sekunde aus
}

Generate alternating pitches

In the second example, you only add a few lines to the main part (loop). This makes two tones sound alternately at pitches of "100" and "200", like a siren. There is no pause between the tones.

Program Code - Alternating Pitches
void setup()
{
}
void loop()
{
  tone(8, 100);
  delay(1000);
  noTone(8); // The first tone stops here.
  tone(8, 200); // The second tone with a pitch of "200" sounds.
  delay(1000); // ... for one second ...
  noTone(8); // The second tone stops here, and the loop starts over.
}

Generate a tone at the press of a button

In the third example, a push button is added to the circuit on pin 6. In the main loop, an IF statement reads the push button. If the button is pressed, a tone with a pitch of “300” sounds for one second.

Circuit diagram with a speaker on pin 8 and a push button on pin 6 of the Arduino UNO
Circuit diagram: Generate a tone by pressing a button
Program code - sound at the press of a button
int Button = 6;
int ButtonStatus = 0;
void setup()
{
  pinMode(Button, INPUT);
}
void loop()
{
  ButtonStatus = digitalRead(Button);
  if (ButtonStatus == HIGH) // If the button is pressed ...
  {
    tone(8, 300); // ... play this tone ...
    delay(1000); // ... for one second ...
    noTone(8); // turn off the tone
  }
}

Generate tones depending on different keys

In the fourth example, the sketch determines which tone the speaker plays depending on which button is pressed. To do this, connect one pushbutton to pin 6 and another to pin 7. In the main section, two IF statements determine which tone is played.

Circuit diagram with a speaker on pin 8 and buttons on pins 6 and 7 of the Arduino UNO
Circuit diagram: Tones depending on different buttons
Program Code – Two Buttons, Two Tones
int Taster1 = 6; // Button 1 connected to pin 6
int Taster2 = 7; // Button 2 connected to pin 7
int Tasterstatus1 = 0; // Variable to store the status of button 1.
int Tasterstatus2 = 0; // Variable to store the status of button 2.
void setup()
{
  pinMode(Taster1, INPUT); // Set button 1 as an input
  pinMode(Taster2, INPUT); // Set button 2 as an input
}
void loop()
{
  Tasterstatus1 = digitalRead(Taster1); // Read the status of button 1 (HIGH or LOW)
  Tasterstatus2 = digitalRead(Taster2); // Read the status of button 2 (HIGH or LOW)
  if (Tasterstatus1 == HIGH) // If button 1 is pressed, then ...
  {
    tone(8, 100); // Output a tone with a pitch of 100.
    delay(1000); // lasting one second
    noTone(8); // Turn off the tone.
  }
  if (Tasterstatus2 == HIGH) // If button 2 is pressed, then ...
  {
    tone(8, 200); // Output a tone with a pitch of 200.
    delay(1000); // lasting one second
    noTone(8); // Turn off the tone.
  }
}

Create melodies with the Arduino

The Arduino software contains a file specifically for producing tones with a speaker (passive buzzer). Instead of specifying pitches as numbers, such as tone(8, 200), you can use it to select notes from the scale. The Arduino software includes the “toneMelody” sketch as an example.

This example already includes the file that maps pitches to numerical values. It’s called “pitches.h”. To view its contents, click the second tab in the open example sketch.

To access the file, simply add the command #include "pitches.h" to the sketch, and make sure the file is open as a tab. The easiest way is to open and edit the “toneMelody” sketch from the examples in the Arduino software. This automatically opens the “pitches.h” file as a tab.

The following short sketch alternately plays two tones from the “pitches.h” file.

You no longer need noTone() in this sketch: the extended command tone(x, y, z) stops the tone by itself. Here, x is the speaker pin, y is the pitch, and z is the tone duration in milliseconds.

Arduino software menu with the toneMelody example sketch
Find it under “Examples” → “02.Digital” → “toneMelody”
Opened the toneMelody sketch with the pitches.h tab
The pitches.h file is in the sketch’s second tab.
1 / 2
Play notes from pitches.h in program code
#include "pitches.h"
void setup()
{
  pinMode(8, OUTPUT); // Speaker on pin 8
}
void loop()
{
  tone(8, NOTE_C4, 1000); // Play note C4 on pin 8 for 1000 ms
  delay(3000); // The sketch pauses for 3 seconds. Since the tone lasts only 1 second, two seconds of silence remain afterward.
  tone(8, NOTE_G3, 1000); // Play note G3 on pin 8 for 1000 ms
  delay(3000); // The sketch pauses for 3 seconds. Since the tone lasts only 1 second, two seconds of silence remain afterward.
}
Rate the instructions

Comments

Ask a question or share an experience

Contact us if something doesn’t work. Please include your board and the error message.

We review every comment before publication. Your email address remains with us.