Lesson 6. SPEAKER. Play Jingle Bells



  • Step 1. Go to the folder Arduino-ESP32-IDE and open the app arduino.exe (Fig. 1);

    alt text
    Figure 1. The archive with the Arduino IDE extracted to the folder

    Step 2. On the File menu, select New (Fig. 1.1);

    alt text
    Figure 1.1. Create a new sketch

    Step 3. Select Include Library, M5Stack in the Sketch menu (Fig. 1.2);

    alt text
    Figure 1.2. Connection library M5Stack

    alt text
    Figure 1.3. The holes for the sound from the built-in speaker are located on the right side of the device

    alt text
    Figure 1.4. Speaker is located on the left of the bus 2 to 15

    Step 4. Add to the beginning of system functions void setup() function call M5.begin (); because it is necessary to prepare the device to work. Add to the end system functions void loop() function call M5.update (); is required for correct operation of the device with built-in speaker. This step should be performed always when writing a new sketch (Fig. 2).

    alt text
    Figure 2. An example of the correct preparation of the sketch for work with built-in speaker

    Step 5. To work with the speaker (Fig. 1.3, 1.4) use the following functions and constants notes in table 1 and 2 respectively:

    List 1. Functions for working with built-in speaker

    • M5.Speaker.tone(uint32_t freq); to Generate a beep at a specified frequency (Hz)
    • M5.Speaker.mute(); Interrupt generation

    List 2. Constant notes (frequencies rounded to whole numbers)

    • #define NOTE_D0 -1 silence
      THE FIRST OCTAVE
    • #define NOTE_B1 262 To (B)
    • #define NOTE_Cd1 277 C sharp (C#)
    • #define NOTE_D1 294 Re (D)
    • #define NOTE_Dd1 311 in D sharp (D#)
    • #define NOTE_E1 330 Mi (E)
    • #define NOTE_F1 349 FA (F)
    • #define NOTE_Fd1 370 f-sharp (F#)
    • #define NOTE_G1 392 Sol (G)
    • #define NOTE_Gd1 415 g sharp (G#)
    • #define NOTE_A1 440 La (A)
    • #define NOTE_Ad1 466 La-a sharp (A#)
    • #define NOTE_H1 494 si (H)

    Step 6. Write a music box (Fig. 2.1, 2.2);

    alt text
    Figure 2.1. Code music box. Page first

    alt text
    Figure 2.2. Code music box. Page two

    Step 7. Click the Upload button (Fig. 3) in order to flash the device;

    alt text
    Figure 3. Download the firmware to the device

    Step 8. When the device firmware is completed, press the A button and enjoy the melody :)