Skip to content

+49 1626571232

info@openelab.io

🚀 Free Shipping from 50€ in EU / 80€ Worldwide

M5Stack Beginner: Plus 2 Leveraging Sound and Display Features

21 Nov 2024 0 Comments

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.

M5StickC PLUS2

M5StickC PLUS2

View Product

  

What is M5StickC Plus2?

The M5StickC Plus2 is a compact development board that utilizes the ESP32 chip (ESP32-PICO-V3-02), featuring an array of integrated sensors and peripherals. This design renders it highly suitable for a diverse range of embedded applications. Its main features include:
  • 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

Before starting to write code, ensure you have the following development tools installed:
  1. Arduino IDE: Used for writing and uploading code to the M5StickC Plus2. Download from the Arduino Official Website.

  2. M5StickC Plus2 Library: Install the M5Stack library in Arduino IDE to utilize its provided functionalities.

More Details: Arduino IDE Setup
 

Installing the M5StickC Plus2 Library

  1. Open the Arduino IDE.

  2. Navigate to Tools > Board > Boards Manager.

  3. Search for M5StickC Plus2 and install the latest version of the library.

More Details:

 

Sample Project: Displaying Text and Playing Sounds

The following document presents a straightforward sample project that illustrates the process of displaying text on the M5StickC Plus2's screen, as well as generating various frequency tones through the integrated speaker.
#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);
}


✔ Copied!

  

Code Explanation

  1. Including the Library  #include "M5StickCPlus2.h"

This statement includes the M5StickC Plus2 library, enabling access to its various functionalities, such as display control and speaker playback.
  1. 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);
}

✔ Copied!

 

Display Configuration:
  • 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).

Tone Playback:
  • 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.

This loop continuously alternates between high and low-frequency tones, creating rhythmic sound feedback.
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);
}

✔ Copied!

 

Project Expansion and Applications

  1. Sound Feedback and Alarm Systems
Utilizing the built-in speaker, various sound feedback and alarm functionalities can be implemented, such as:
  • 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.

  1. Information Display and Interaction
Combining the display, rich information presentation and user interaction can be achieved:
  • 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.

  1. Wearable Devices
Due to its compact size and rich features, the M5StickC Plus2 is highly suitable as a core component for 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

  1. Confirm Object Names
Ensure that you are using the correct object names in your code. Typically, the M5Stack series libraries use 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.
Dependent Library:
  1. 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.

  1. Display Refresh

After drawing content on the display, always call the display() function to refresh the screen. Otherwise, the drawn content may not appear on the display.
  1. Error Handling

Incorporate appropriate error-handling mechanisms to ensure that the device can provide feedback and take necessary actions if initialization or runtime errors occur.
  1. Power Management

For battery-powered projects, manage power consumption effectively to extend the device's operational time. Utilize low-power modes or optimize the code to reduce unnecessary energy usage.

 

 

M5StickC Plus2 Project

  

Recommended Articles

What is M5Stack?

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

 
We really hope this article makes it easier for you to get to know the M5StickC Plus2! If you run into any bumps along the way during your development, don’t hesitate to reach out to friendly communities for help or share your amazing project results. Wishing you the best of luck on your exciting journey into IoT and embedded development!
  
Prev Post
Next Post

Leave a comment

All blog comments are checked prior to publishing

Someone recently bought a

Thanks for subscribing!

This email has been registered!

Shop the look

Choose Options

Edit Option
Back In Stock Notification
this is just a warning
Login
Shopping Cart
0 items
RuffRuff App RuffRuff App by Tsun