pdf-icon

Arduino Quick Start

2. Devices & Examples

Tough Speaker

Tough speaker related APIs and example programs.

Example Program

Build Requirements

  • M5Stack Board Manager version >= 2.1.4
  • Board selection = M5Tough
  • M5Unified library version >= 0.2.5
  • M5GFX library version >= 0.2.7
cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
#include <M5Unified.h>
#include <M5GFX.h>
void setup() {
M5.begin();
M5.Display.setRotation(1);
M5.Display.setFont(&fonts::DejaVu40);
M5.Display.print("\nStart playing\nmusical scale");
M5.Speaker.setVolume(128); // 0~255, bigger is louder
}
void loop() {
M5.update();
M5.Speaker.tone(880, 500); // frequency (Hz), duration (ms)
delay(500);
M5.Speaker.tone(990, 500);
delay(500);
M5.Speaker.tone(1120, 500);
delay(500);
M5.Speaker.tone(1180, 500);
delay(500);
M5.Speaker.tone(1320, 500);
delay(500);
M5.Speaker.tone(1480, 500);
delay(500);
M5.Speaker.tone(1650, 500);
delay(500);
M5.Speaker.tone(1760, 500);
delay(1000);
M5.Speaker.tone(1760, 500);
delay(500);
M5.Speaker.tone(1650, 500);
delay(500);
M5.Speaker.tone(1480, 500);
delay(500);
M5.Speaker.tone(1320, 500);
delay(500);
M5.Speaker.tone(1180, 500);
delay(500);
M5.Speaker.tone(1120, 500);
delay(500);
M5.Speaker.tone(990, 500);
delay(500);
M5.Speaker.tone(880, 500);
delay(1000);
}

This program will play the scales 12345671 and 17654321.

API

The Tough speaker functionality uses the Speaker_Class from the M5Unified library. For more related APIs, please refer to the following documentation:

On This Page