Speaker 🔉

Class Name: Speaker

begin()

Functionality:

Initializes the speaker.

Function Prototype:

void begin()

Note:
1. If you do not want to initialize the speaker with M5.begin(), please call this function before using the speaker.

Usage Example:

#include <M5Stack.h>

void setup(){
  M5.Speaker.begin(); // Initialize the speaker
  M5.Speaker.tone(661, 1000);   // Set the speaker to sound at 661Hz for 1000ms
}

void loop(){
  M5.Speaker.update();
  delay(100);
}

end()

Functionality:

Stops the speaker.

Function Prototype:

void end()

Usage Example:

#include <M5Stack.h>

void setup(){
  M5.begin();
}

void loop(){
  M5.update();
  if(M5.BtnA.wasPressed()) {  // If button A was pressed
    M5.Speaker.tone(661); // Set the speaker to continuously sound at 661Hz
  }else if(M5.BtnB.wasPressed()){
    M5.Speaker.end(); // Turn off the speaker
  }
  delay(100);
}

update()

Functionality:

Outputs the speaker's settings.

Function Prototype:

void update()

Note:
This needs to be used in conjunction with the sound-making function to produce sound from the M5Core.

Usage Example:

#include <M5Stack.h>

void setup(){
  M5.Speaker.begin(); // Initialize the speaker
  M5.Speaker.tone(661, 1000);   // Set the speaker to sound at 661Hz for 1000ms
}

void loop(){
  M5.Speaker.update();
  delay(100);
}

tone()

Functionality:

Sets the speaker to sound at a frequency for a duration in milliseconds.

Function Prototype:

void tone(uint16_t frequency)

void tone(uint16_t frequency, uint32_t duration)

Parameter Type Description
frequency uint16_t Speaker frequency
duration uint32_t Duration of sound (ms)

Usage Example:

#include <M5Stack.h>

void setup(){
  M5.begin();
}

void loop(){
  M5.update();
  if(M5.BtnA.wasPressed()) {  // If button A was pressed
    M5.Speaker.tone(661, 200); // Set the speaker to sound at 661Hz for 200ms
  }else if(M5.BtnB.wasPressed()){
    M5.Speaker.tone(112); // Set the speaker to continuously sound at 112Hz
  }else if(M5.BtnC.wasPressed()){
    M5.Speaker.end(); // Turn off the speaker
  }
  delay(100);
}

setVolume()

Functionality:

Sets the volume.

Function Prototype:

void setVolume(uint8_t volume)

Parameter Type Description
volume uint8_t Volume (0~11)

Usage Example:

#include <M5Stack.h>
char i = 0;
void setup(){
  M5.begin();
  M5.Speaker.begin();
}

void loop(){
  M5.update();
  if(M5.BtnA.wasPressed()) {  // If button A was pressed
    M5.Speaker.tone(661, 200); // Set the speaker to sound at 661Hz for 200ms
  }else if(M5.BtnC.wasPressed()){
    M5.Speaker.setVolume(i++); // Set the speaker volume
  }
  delay(100);
}
On This Page