研究テーマ->音楽とロボット->オリジナル制作のロボット->楽器の音でロボットをコントロールする(Operating Arduino with musical instruments.)    
  マイクで拾った音から、ドレミの音を解析し、音高によってサーボモーターを動かす仕組みをオープンソースプロジェクトとして公開しています。  
 
Purpose of the project
The purpose of the project is to control the arduino using instruments that can play scales, such as pianos and flutes.
 
Technical background and logic
Each note in the scale has a unique frequency.
Note Name Frequency (Hz) Octave Frequency
A 440.0 880.0
A#(Bb) 466.2 932.3
B 493.9 987.8
C 523.3 1046.5
C#(Db) 554.4 1108.7
D 587.3 1174.7
D#(Eb) 622.3 1244.5
E 659.3 1318.5
F 698.5 1396.9
F#(Gb) 740.0 1480.0
G 784.0 1568.0
G#(Ab) 830.6 1661.2



https://www.asahi-net.or.jp/~hb9t-ktd/music/Japan/Research/DTM/freq_map.html
 

The notes in the scale are expressed as Do,Re,Mi in Japanese and C,D.E in English, but English expression is used in this project.
Using a method called Fourier transform, you can find the frequency components of the sound produced by the instrument.

If the power of frequency around 440Hz is the largest, it is considered that the instrument is playing the note A.
*Note The instrument plays single notes, not chords.

 

Components and structure
Mic
Arduino (UNO,nano,pro mini) ,(8Mhz or 16Mhz)
Controller for servo motor (Arduino or other controller)
servo motors

※If a microphone and PWM ports are used at the same time, noise will be introduced and frequency detection will be unstable.
Therefore an additional controller is required for operating servo-motors.
If it operates only LEDs, an additional controller is not required.

 

Source Code for Arduino Sketch

Download

Add ArduinoFFT as library. (Refer https://ppdr.softether.net/esp-fft-test)
 

 

Tuning of sketch
It is necessary to adjust the frequency data according to the measured values.
(1) rewrite freq_data

int freq_data[12+2] = {
// 理想値 idial value replace to measured value

415,//Ab / G#
440,//A
466,//A#
494,//B
523,//C
554,//Db/C#
587,//D
622,//Eb/D#
659,//E
699,//F
740,//Gb/F#
784,//G
830,//Ab/G#
880,//A
};
These are theoretical values, and they are not correct.
First, you have to know real values.
// Serial.println(peak); //Print out peek frequency
Delete // and activate this line.

Run the program.
Play note C, then you have frequency data for C.
for example, it is 530, replace 523,//C to 530,//C
If it is 1066, then divide it by 2. It would be 533//C

Check the frequency for all sounds.
(2) rewrite THRESHOLD_VOLUME
Comment out Serial.println(peak); , and activate Serial.println(volume);

Serial.println(volume); //Print out volume
// Serial.println(peak); //Print out peek frequency


Play the musical instrument, and replace value of THRESHOLD_VOLUME with appropriate value.
 

 

License
This project is open source project. Credit for the author is required to use the source code.
<Frequency to musical note>
Copyright (C) 2020 Tetsuji Katsuda
This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.