pdf-icon

Arduino Quick Start

Basic/Fire/Gray/M5GO Speaker

Class Name: Speaker

begin()

Description:

Initializes the speaker.

Syntax:

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

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11
#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()

Description:

Stops the speaker.

Syntax:

void end();

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#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()

Description:

Outputs the speaker's settings.

Syntax:

void update();
Note:
This needs to be used in conjunction with the sound-making function to produce sound from the M5Core.

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11
#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()

Description:

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

Syntax:

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)

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#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()

Description:

Sets the volume.

Syntax:

void setVolume(uint8_t volume);
Parameter Type Description
volume uint8_t Volume (0~11)

Example:

cpp
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
#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