pdf-icon

Arduino Quick Start

2. Devices & Examples

6. Applications

Cardputer Speaker

APIs and example programs related to the Cardputer Speaker, applicable to both Cardputer and Cardputer-Adv.

About the 3.5mm AUX jack
The 3.5mm AUX jack on Cardputer-Adv also applies to this page. Once connected to headphones or external speakers, the audio output will switch from the built-in speaker to the AUX channel.

Example Program

Compilation Requirements

  • M5Stack board manager version >= 3.2.2
  • Board option = M5Cardputer
  • M5Cardputer library version >= 1.1.0
  • M5Unified library version >= 0.2.8
  • M5GFX library version >= 0.2.10

Beep Example

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13
#include "M5Cardputer.h"

void setup() {
  auto cfg = M5.config();
  M5Cardputer.begin(cfg);
}

void loop() {
  M5Cardputer.Speaker.tone(7000, 100);  // frequency, duration
  delay(1000);
  M5Cardputer.Speaker.tone(4000, 20);  // frequency, duration
  delay(1000);
}

Play WAV File Example

Open the example program Advanced -> Speaker_SD_wav_file from the M5Unified driver library. As shown by the arrow in the image below, change the value of SDCARD_CSPIN to GPIO_NUM_12, and update the filename in files[] to the path of the WAV file you want to play (the leading / indicates the root directory of the microSD card). Click the upload button to compile and flash the program to the device. Insert the microSD card into the device (with the card’s contacts facing opposite to the device’s screen). When the device starts, the specified WAV files in the program will be played sequentially.

API

The M5Cardputer library is built on top of the M5Unified library. The speaker driver uses the Speaker_Class from the M5Unified library. For more detailed APIs, please refer to the following documentation:

On This Page