Fixing a 37 keys piano toy with Arduino

Hello,

my sister’s piano toy is broken.
I tried to find a replacement but impossible.

I bought PAM8302 from Adafruit and now I can play tones with Arduino and the toy’s speaker.
I want to buy a board that is able to handle the 37 keys and I’ll see what more…

At the moment I have an Arduino UNO and I’m trying to play the notes of a 37 key piano. I’ve copied the frecuencies from wikipedia (until 88keys) and selected from 1 to 37.

I coded a program that plays the scale from 1 to 37 and it works!
But, the sound is generated with tone() and I want to know if it’s possible to generate more realistic musical notes… I think tone() uses square waves, and I don’t know how “more natural” notes are called, neither how to play them with Arduino.

I’ve heard some posts on Google that are able to play WAV files with an SD Card module.

I hope you can help me with the following questions:

  1. Is it possible to play more than 1 key with tone()? Imagine my sister pushes 4 keys at the same time, instead of 1 by 1… would be cool to hear them 4 as in a real piano.

  2. Is it possible to play more realistic musical notes?

  3. Would it be possible to play more than 1 musical note at the same time and also more realistic music?

  4. Can I reproduce the voice trought the mic while she’s playing the piano?

Thanks in advance!

I attach my actual code so anyone here can use it:

/*
 * Piano de Dulce María
 * 
 * Por Abel Romero Pérez.
 * Fecha: 11/06/2018
 * Copyright (C) Família Romero y Rodríguez
 * 
 * 
 * Teclas de un piano:
 * 
Tecla Nota Frecuencia (Hz)
88  do7 C8  4186,01
87  si6 B7  3951,07
86  la♯6/si♭6 A♯7/B♭7 3729,31
85  la6 A7  3520,00
84  sol♯6/la♭6  G♯7/A♭7 3322,44
83  sol6  G7  3135,96
82  fa♯6/sol♭6  F♯7/G♭7 2959,96
81  fa6 F7  2793,83
80  mi6 E7  2637,02
79  re♯6/mi♭6 D♯7/E♭7 2489,02
78  re6 D7  2349,32
77  do♯6/re♭6 C♯7/D♭7 2217,46
76  do6 C7  2093,00
75  si5 B6  1975,53
74  la♯5/si♭5 A♯6/B♭6 1864,66
73  la5 A6  1760,00
72  sol♯5/la♭5  G♯6/A♭6 1661,22
71  sol5  G6  1567,98
70  fa♯5/sol♭5  F♯6/G♭6 1479,98
69  fa5 F6  1396,91
68  mi5 E6  1318,51
67  re♯5/mi♭5 D♯6/E♭6 1244,51
66  re5 D6  1174,66
65  do♯5/re♭5 C♯6/D♭6 1108,73
64  do5 C6  1046,50
63  si4 B5  987,767
62  la♯4/si♭4 A♯5/B♭5 932,328
61  la4 A5  880,000
60  sol♯4/la♭4  G♯5/A♭5 830,609
59  sol4  G5  783,991
58  fa♯4/sol♭4  F♯5/G♭5 739,989
57  fa4 F5  698,456
56  mi4 E5  659,255
55  re♯4/mi♭4 D♯5/E♭5 622,254
54  re4 D5  587,330
53  do♯4/re♭4 C♯5/D♭5 554,365
52  do4 C5  523,251
51  si3 B4  493,883
50  la♯3/si♭3 A♯4/B♭4 466,164
49  la3 A4 (la 440) 440,000
48  sol♯3/la♭3  G♯4/A♭4 415,305
47  sol3  G4  391,995
46  fa♯3/sol♭3  F♯4/G♭4 369,994
45  fa3 F4  349,228
44  mi3 E4  329,628
43  re♯3/mi♭3 D♯4/E♭4 311,127
42  re3 D4  293,665
41  do♯3/re♭3 C♯4/D♭4 277,183
40  do3 C4 (do central) 261,626
39  si2 B3  246,942
38  la♯2/si♭2 A♯3/B♭3 233,082
37  la2 A3  220,000
36  sol♯2/la♭2  G♯3/A♭3 207,652
35  sol2  G3  195,998
34  fa♯2/sol♭2  F♯3/G♭3 184,997
33  fa2 F3  174,614
32  mi2 E3  164,814
31  re♯2/mi♭2 D♯3/E♭3 155,563
30  re2 D3  146,832
29  do♯2/re♭2 C♯3/D♭3 138,591
28  do2 C3  130,813
27  si1 B2  123,471
26  la♯1/si♭1 A♯2/B♭2 116,541
25  la1 A2  110,000
24  sol♯1/la♭1  G♯2/A♭2 103,826
23  sol1  G2  97,9989
22  fa♯1/sol♭ 1 F♯2/G♭2 92,4986
21  fa1 F2  87,3071
20  mi1 E2  82,4069
19  re♯1/mi♭1 D♯2/E♭2 77,7817
18  re1 D2  73,4162
17  do♯1/re♭1 C♯2/D♭2 69,2957
16  do1 C2  65,4064
15  si0 B1  61,7354
14  la♯0/si♭0 A♯1/B♭1 58,2705
13  la0 A1  55,0000
12  sol♯0/la♭0  G♯1/A♭1 51,9130
11  sol0  G1  48,9995
10  fa♯0/sol♭0  F♯1/G♭1 46,2493
9 fa0 F1  43,6536
8 mi0 E1  41,2035
7 re♯0/mi♭0 D♯1/E♭1 38,8909
6 re0 D1  36,7081
5 do♯0/re♭0 C♯1/D♭1 34,6479
4 do0 C1  32,7032
3 si-1  B0  30,8677
2 la♯-1/si♭-1 A♯0/B♭0 29,1353
1 la-1  A0  27,5000
 */

float hz[] = {27.500, 29.1353, 30.8677, 32.7032, 34.6479, 36.7081, 38.8909, 41.2035, 43.6536, 46.2493, 48.9995, 51.9130, 55.0000, 58.2705, 61.7354, 65.4064, 69.2957, 73.4162, 77.7817, 82.4069,
87.3071, 92.4986, 97.9989, 103.826, 110, 116.541, 123.471, 130.813, 138.591, 146.832, 155.563, 164.814, 174.614, 184.997, 195.998, 207.652, 220};

void setup() {
  // put your setup code here, to run once:
  pinMode(3, OUTPUT);
  test();
}

void loop() {
  // put your main code here, to run repeatedly:
  test();
}

void test() {
  for (int i = 0; i < 37; i++) {
    tone(3, hz[i]);
    delay(250);
    noTone(3);
  }
}

Maybe need to check this thread …

A discussion about polyphonic instruments.

aberope: 1. Is it possible to play more than 1 key with tone()? Imagine my sister pushes 4 keys at the same time, instead of 1 by 1... would be cool to hear them 4 as in a real piano.

No. You need some kind of synthesiser for this.

  1. Is it possible to play more realistic musical notes?

Yes - add a WAV player, and use that to play your notes, or whatever sounds you want to link to a key.

For reading all those keys, assuming they're acting as on/off momentary buttons, several options: - a Mega with lots of inputs - a key matrix, 37 keys need 13 inputs (6x6 matrix + 1 separate). - shift registers (6-7 of them daisy chained, or two as 8x8 matrix) or port expanders such as the PCF8574 or MCP23008 (two in 8x8 matrix configuration).

Thank you.

Both are right:

  1. Need a mixer
  2. Need more digital inputs (because keys are just a button per key)
  3. Need a memory extension because WAV files for each key are 11kb of 3s per note.

I attach you the information I’ve found of my research, as follows:

The need of a PCM player is covered by High-Low Tech – Simple Arduino audio samples
To convert to WAV format to be Arduino compatible, I’ve used ffmpeg: ffmpeg -i decoded.mpga -ar 8000 -ac 1 -acodec pcm_u8 1.wav (you can use -t to cut from 00:00:00.0).
And finally to convert WAV files into C++ arrays I’ve used EncodeAudio (in the post of the URL are the download links for Win, macOS & Linux).

But, I’ve decided to research with a Raspberry Pi, because I guess I can mix the notes with software, also store 37 WAV files for each note in best quality (and I don’t need to buy another MicroSD), I can read the mic and output trought the speaker while playing, and much more things easier (as I’m experiencer with computers than with microcontrollers)… so the Arduino way is clear.

Hope that helps!