M5Stack Beginner: Plus 2 Leveraging Sound and Display Features
Introduction
With the swift progression of the Internet of Things (IoT) and the proliferation of wearable devices, there is an increasing demand from developers for development boards that are compact, efficient, and feature-rich. The M5StickC Plus2, a notable member of the M5Stack series, distinguishes itself through its compact design, multifunctional integration, and user-friendly interface, rendering it an exemplary choice for a diverse array of projects. This article aims to elucidate the key features of the M5StickC Plus2 and provide guidance on leveraging its integrated speaker and display functionalities to execute sound playback and text display within a sample project.
What is M5StickC Plus2?
-
Compact Design: Measuring only 54mm × 54mm, it is easy to carry and integrate into various projects.
-
Built-in Display: A 1.14-inch color TFT display supporting multiple fonts and graphic displays.
-
Multifunctional Sensors: Includes an accelerometer, gyroscope, temperature and humidity sensors, among others, suitable for diverse application scenarios.
-
Built-in Speaker: Supports audio playback, ideal for sound feedback and alarm functions.
-
Rich Interfaces: Supports GPIO, I2C, UART, and other communication interfaces, facilitating peripheral expansion.
-
Battery-Powered: Integrated battery supports long-term operation, suitable for mobile applications.
Setting Up the Development Environment
-
Arduino IDE: Used for writing and uploading code to the M5StickC Plus2. Download from the Arduino Official Website.
-
M5StickC Plus2 Library: Install the M5Stack library in Arduino IDE to utilize its provided functionalities.
Installing the M5StickC Plus2 Library
-
Open the Arduino IDE.
-
Navigate to
Tools
>Board
>Boards Manager
. -
Search for
M5StickC Plus2
and install the latest version of the library.
Sample Project: Displaying Text and Playing Sounds
#include "M5StickCPlus2.h" void setup() { // Retrieves device configuration. auto cfg = M5.config(); // Initializes the M5StickC Plus2. StickCP2.begin(cfg); // Rotates the display StickCP2.Display.setRotation(1); // sets text color to green StickCP2.Display.setTextColor(GREEN); // centers the text StickCP2.Display.setTextDatum(middle_center); // uses the "Orbitron_Light_24" font StickCP2.Display.setTextFont(&fonts::Orbitron_Light_24); StickCP2.Display.setTextSize(1); // Displays the message "Power LED" at the screen’s center. StickCP2.Display.drawString("Buzzer Test", StickCP2.Display.width() / 2, StickCP2.Display.height() / 2); } void loop() { // Play a 10,000 Hz tone for 100 milliseconds StickCP2.Speaker.tone(10000, 100); // Delay for 1 second delay(1000); // Play a 4,000 Hz tone for 20 milliseconds StickCP2.Speaker.tone(4000, 20); // Delay for 1 second delay(1000); }
Code Explanation
-
Including the Library
#include "M5StickCPlus2.h"
-
setup()
Function
void setup() { // Retrieves device configuration. auto cfg = M5.config(); // Initializes the M5StickC Plus2. StickCP2.begin(cfg); // Rotates the display StickCP2.Display.setRotation(1); // sets text color to green StickCP2.Display.setTextColor(GREEN); // centers the text StickCP2.Display.setTextDatum(middle_center); // uses the "Orbitron_Light_24" font StickCP2.Display.setTextFont(&fonts::Orbitron_Light_24); StickCP2.Display.setTextSize(1); // Displays the message "Power LED" at the screen’s center. StickCP2.Display.drawString("Buzzer Test", StickCP2.Display.width() / 2, StickCP2.Display.height() / 2); }
-
Rotation:
setRotation(1)
rotates the display by 90 degrees to match the device's physical orientation. -
Text Color:
setTextColor(GREEN)
sets the text color to green. -
Text Alignment:
setTextDatum(middle_center)
centers the text alignment. -
Font and Size: Uses the
Orbitron_Light_24
font and sets the text size to 1. -
Display Text: Displays "Buzzer Test" at the center of the screen.
-
Refresh Display: Calls
display()
to update the display with the drawn content.
3. loop()
Function
StickCP2.Speaker.tone(10000, 100);
-
10000
: This parameter usually represents the frequency of the tone in Hertz (Hz). A frequency of 10,000 Hz is a high-pitched sound, near the upper limit of human hearing. -
100
: This parameter typically represents the duration of the tone in milliseconds (ms). So, the tone will play for 100 milliseconds (0.1 seconds).
-
Plays a 10,000 Hz high-frequency tone for 100 milliseconds.
-
Delays for 1 second.
-
Plays a 4,000 Hz lower-frequency tone for 20 milliseconds.
-
Delays for another 1 second.
void loop() { // Play a 10,000 Hz tone for 100 milliseconds StickCP2.Speaker.tone(10000, 100); // Delay for 1 second delay(1000); // Play a 4,000 Hz tone for 20 milliseconds StickCP2.Speaker.tone(4000, 20); // Delay for 1 second delay(1000); }
Project Expansion and Applications
-
Sound Feedback and Alarm Systems
-
Notification Alerts: Play sounds to notify users of specific events.
-
Alarm Systems: Emit high-frequency tones to alert users of abnormal conditions.
-
Interactive Games: Create simple game sound effects using different frequencies and durations.
-
Information Display and Interaction
-
Real-Time Data Display: Show real-time sensor data like temperature and humidity.
-
User Interfaces: Create simple menus and buttons for user interaction through the display.
-
Animations and Graphics: Display dynamic graphics or animations to enhance the visual appeal of projects.
-
Wearable Devices
-
Health Monitoring: Combine sensors to monitor health data such as heart rate and steps, and display the information.
-
Smart Bracelets: Implement information reminders and simple interactions using the display and speaker.
-
Fitness Trackers: Record and display exercise data in real-time through the display.
Considerations and Optimization Suggestions
-
Confirm Object Names
M5
as the main object. If the library indeed uses StickCP2
, refer to the official documentation to confirm its usage. If necessary, replace StickCP2
with M5
as shown in the sample code.-
M5Unified: https://github.com/m5stack/M5Unified
-
M5StickCPlus2: https://github.com/m5stack/M5StickCPlus2
-
Speaker Performance
-
Frequency Range: Ensure that the frequencies you intend to play are within the speaker's supported range. Extremely high or low frequencies may cause sound distortion or may not be audible.
-
Volume Control: If the library supports volume control, adjust the volume as needed to avoid sounds being too loud or too soft.
-
Display Refresh
display()
function to refresh the screen. Otherwise, the drawn content may not appear on the display.-
Error Handling
-
Power Management
M5StickC Plus2 Project
M5StickC Bluetooth Speaker – OpenELAB
Mini Slot Machine Project with M5StickC Plus2
Recommended Articles
How Do I Turn On and Turn Off My M5StickC PLUS2?
What is the difference between M5StickC PLUS and PLUS2?
Build a Bicycle Speed Detection System
M5Stack Beginner: M5Burner Burning
M5Stack Beginner: PLUS2 Button User Guide
M5Stack Beginner: PLUS2 Infrared Capabilities
M5Stack Beginner Project: implement a remote control function