Build a jukebox with RTTTL ringtones

FunduinoPublished on

Klingeltöne im RTTTL-Format per Tastenpad auswählen und abspielen

Mit einem Lautsprecher, einem analogen Tastenpad und einem I²C-LCD baust du eine kleine Jukebox. Jede Taste startet ein anderes Musikstück, und das Display zeigt Interpret und Titel an.

Die Melodien liegen im RTTTL-Format vor, das Nokia für die Klingeltöne seiner Handys entwickelt hat. Abgespielt werden sie mit der Bibliothek PlayRtttl.

Circuit diagram: Speaker on pin 7, keypad on A0, I²C LCD on SDA and SCL

You need this

Passive speaker - 8 Ohm, 85 dB, XH2.54 3-pin socket Passive speaker - 8 Ohm, 85 dB, XH2.54 3-pin socket 3,40 € *Item no. F23108955About the product — Passive speaker - 8 Ohm, 85 dB, XH2.54 3-pin socket
Analog keyboard module - 5 buttons, 5 V Analog keyboard module - 5 buttons, 5 V 3,00 € *Item no. F23106483About the product — Analog keyboard module - 5 buttons, 5 V
LCD Module 20x4 - I2C, Blue Backlight, 3.3-5 V LCD Module 20x4 - I2C, Blue Backlight, 3.3-5 V 6,21 € *Item no. F23107181About the product — LCD Module 20x4 - I2C, Blue Backlight, 3.3-5 V

All 3 parts together 12,61 € *Ready to ship immediately · Delivery time: 1-3 business days

* All prices include statutory VAT, plus shipping costs.

RTTTL ringtones

Nokia developed ringtones for its mobile phones, which were played, for example, as incoming call alerts. The resulting RTTTL standard (Ring Tone Text Transfer Language) consists of three parts:

  • the ringtone name
  • the default note duration and octave used
  • comma-separated notes in international notation (c, d, e, f, g, a, b)

A colon separates the three parts. Here is the beginning of the Te Deum as an example:

TeDeum:d=4,o=5,b=63:8c,8f,16f,16g,8a

  • d=4: The default note length is a quarter note.
  • o=5: The notes are played in the fifth octave.
  • b=63: The tempo is 63 beats per minute.
  • 8c: The note C is played as an eighth note.

You can find ringtones to download at PICAXE.

In international musical notation, B refers to the note known as H in German.

Required components

Use the keypad buttons to select a tune. The LCD displays the tune, and the speaker plays it. You will need:

If the speaker is too loud, you can lower the volume with a resistor, as shown in the second image.

The sketch is written for an LCD with four lines of 20 characters each. With a two-line 1602 display, lines 3 and 4 are missing, namely the title and part of the menu.

Various speakers suitable for the jukebox
Examples of speakers
Connecting the speaker with a resistor to reduce the volume
1 / 2

The circuit diagram

Connect the components as shown in the circuit diagram. The speaker is connected to pin 7, the keypad to analog input A0. The LCD is connected via the I²C bus.

Circuit diagram: Speaker on pin 7, keypad on A0, I²C LCD on SDA and SCL

Install the required libraries

Click the Library icon in the Arduino IDE and search for the LiquidCrystal I2C library in the Library Manager. Install it, then search for the PlayRtttl library.

The LiquidCrystal I2C library in the Library Manager
The PlayRtttl library in the Library Manager
1 / 2

Include libraries and define variables

At the beginning, include the two libraries and initialize the LCD with address 0x27 and 20 characters across 4 lines. The speaker is connected to pin 7.

The four pieces of music are stored as char arrays in RTTTL format in the sketch. Before the first colon are the artist and title, separated by a slash. Underscores represent spaces.

If the display stays blank, your module may use address 0x3F instead of 0x27.

Include libraries and define variables
#include "PlayRtttl.hpp"
#include "LiquidCrystal_I2C.h"
// Set the LCD name (lcd)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// Speaker pin
#define Lautsprecher  7
// Variable for the pressed keypad button
int GedrueckteTaste;
String TitelSong;
char TeDeum[] =
"Charpentier/Te_Deum:d=4,o=5,b=63:8c,8f,16f,16g,8a,8f,c6,8a,8a,8a#,16c6,16a#,16a,16a#,8c6,16g,16f,16g,16a,8g,8c,8f,16f,16g,8a,8f,c6,8a,8a,16a#,16c6,16a,16a#,g,16f,2f";
char FuerElise[] =
"Beethoven/Für_Elise:d=8,o=5,b=125:32p,e6,d#6,e6,d#6,e6,b,d6,c6,4a.,32p,c,e,a,4b.,32p,e,g#,b,4c.6,32p,e,e6,d#6,e6,d#6,e6,b,d6,c6,4a.,32p,c,e,a,4b.,32p,d,c6,b,2a";
char OdeandieFreude[] =
"Beethoven/Ode_an_die_Freude:d=4,o=6,b=100:a5,a5,a_5,c,c,a_5,a5,g5,f5,f5,g5,a5,a.5,8g5,2g5,";
char Bolero[] =
"Ravel/Bolero:d=4,o=5,b=80:c6,8c6,16b,16c6,16d6,16c6,16b,16a,8c6,16c6,16a,c6,8c6,16b,16c6,16a,16g,16e,16f,2g,16g,16f,16e,16d,16e,16f,16g,16a,g,g,";

The setup part

In the setup section, you start the LCD and turn on its backlight.

The setup part
void setup()
{
  // Start the LCD
  lcd.init();
  // Turn on the LCD backlight
  lcd.backlight();
}

Query the keypad

In the loop section, use analogRead to check which key on the analog keypad is pressed. The keypad is connected to input A0. The analog inputs return values between 0 and 1023. The values depend on which key is pressed, as shown in the image.

A switch-case statement assigns an action to each value range. The first example shows how to check the left key: case 0 ... 20 checks for values between 0 and 20. Assign the remaining value ranges in the same way. Note the space before and after the three dots!

The second example shows the complete loop section. The rightmost outer key displays a menu with all four titles.

Call the function without an argument: ZeigeTitel(), as shown in the complete loop section.

The analog values of the five keypad buttons
Example: Querying the left button
// analogen Wert an A0 abfragen
int Analogwert = analogRead(A0);
switch (Analogwert)
{
  case 0 ... 20:
      // char-Array des Titels in String umwandeln
      TitelSong = String(TeDeum);
      // Funktion aufrufen, Interpret/Titel extrahieren
      ZeigeTitel(TitelSong);
      // Titel spielen
      playRtttlBlocking(Lautsprecher, TeDeum);
      break;
}
The complete loop section
void loop()
{
  // Check the keypad at input A0
  int Analogwert = analogRead(A0);
  switch (Analogwert)
  {
    // left button;
    case 0 ... 20:
      // Convert the title char array to a string
      TitelSong = String(TeDeum);
      // Call function to extract artist/title
      ZeigeTitel();
      // Play the title
      playRtttlBlocking(Lautsprecher, TeDeum);
      break;
    // top button
    case 30 ... 60:
      TitelSong = String(FuerElise);
      ZeigeTitel();
      playRtttlBlocking(Lautsprecher, FuerElise);
      break;
    // right button
    case 70 ... 120:
      TitelSong = String(OdeandieFreude);
      ZeigeTitel();
      playRtttlBlocking(Lautsprecher, OdeandieFreude);
      break;
    // bottom button
    case 150 ... 200:
      TitelSong = String(Bolero);
      ZeigeTitel();
      playRtttlBlocking(Lautsprecher, Bolero);
      break;
    // outer right button -> display menu
    case 300 ... 400:
      lcd.clear();
      lcd.setCursor(0, 0);
      lcd.print("1: Te Deum");
      lcd.setCursor(0, 1);
      lcd.print("2: F\365r Elise");
      lcd.setCursor(0, 2);
      lcd.print("3: Ode an die Freude");
      lcd.setCursor(0, 3);
      lcd.print("4: Bolero");
      break;
  }
}

ShowTitle

The loop section calls the ZeigeTitel function. It splits the TitelSong string into the artist's name and the title:

  • TitelSong = TitelSong.substring(0, TitelSong.indexOf(":")); – indexOf searches for the first colon, and substring extracts the part from the first character (0) up to the colon.
  • TitelSong.replace("_", " "); – replaces all underscores with spaces.
  • String Interpret = TitelSong.substring(0, TitelSong.indexOf("/")); – the characters from 0 up to the slash contain the artist's name.
  • String Titel = TitelSong.substring(TitelSong.indexOf("/") + 1, TitelSong.length()); – the second part starts with the character after the slash (+1) and ends at the length of the string (length). It contains the title.

The function then displays “Playing:”, the artist, and the title on the first three lines of the LCD.

ShowTitle
void ZeigeTitel()
{
  // Teil des Strings bis zum ersten Doppelpunkt kürzen, enthält die Informationen zu Interpret und Titel
  TitelSong = TitelSong.substring(0, TitelSong.indexOf(":"));
  // _ durch leerzeichen ersetzen
  TitelSong.replace("_", " ");
  // erster Teil des Strings bis zum / -> Name des Interpreten
  String Interpret = TitelSong.substring(0, TitelSong.indexOf("/"));
  // zweiter Teil des Strings vom / + 1 bis zum Ende des Strings -> Name des Titels
  String Titel = TitelSong.substring(TitelSong.indexOf("/") + 1, TitelSong.length());
   // Titel auf dem LCD anzeigen
  lcd.clear();
  lcd.setCursor(0, 0);
  lcd.print("Spiele:");
  lcd.setCursor(0, 1);
  lcd.print(Interpret);
  lcd.setCursor(0, 2);
  lcd.print(Titel);
}
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.