This Arduino library adds a set of commands for controlling an audio playback device. Currently this library supports the following module:
Serial MP3 playback module with 1W speaker (SKU: HCARDU0138)
This library is provided free to support the open source community. We spend most of our time creating free content like this because we genuinely want to support the open source community. PLEASE SUPPORT US so that we can continue to create free content by purchasing products from our store - HOBBYCOMPONENTS.COM
You will need to download (please log in to download the library) and unzip this library to the Arduino development environments library area.
On Windows:
My Documents\Arduino\libraries\
On Mac:
Documents/Arduino/libraries/
Linux:
Usually found within the users home area under /Arduino/libraries/
Using the HCMP3 library
To use the library just include the HCMP3.h header file and then create an instance of the library. E.g:
- #include "HCMP3.h"
- HCMP3 HCMP3(txPin, rxPin);
Where:
txPin is the digital Tx pin used transmit commands to the module.
rxPin is the digital Rx pin used receive commands from the module.
size=150]HCMP3 library functions:[/size]
- void HCMP3.play();
Plays the next file, or resumes a currently paused file
- void HCMP3.playAll();
Plays all the files on the flash card in sequence
- void HCMP3.play(index);
Plays a file with a specified index where:
index is an 8 bit value containing the index number of the file to be played
valid values for index are 0x01 to 0xFF
- void HCMP3.play(dir, index);
Plays a file with a specified folder and index where:
dir is an 8 bit value containing the index number of the folder holding the file to play
valid values for dir are 0x01 to 0xFF
index is an 8 bit value containing the index number of the file to play within the specified folder
valid values for index are 0x01 to 0xFF
- void HCMP3.pause();
Pauses a currently playing file
- void HCMP3.stop();
Stops a currently playing file
- void HCMP3.playNext();
Plays the file proceeding the last file played
- void HCMP3.playPrevious();
Plays the file preceding the last file played
- void HCMP3.volumeUp();
Increases the playback volume by one step
- void HCMP3.volume(level);
Sets the playback volume to the specified level where:
level is an 8 bit value containing the level to set the volume to in percent
valid values for level are 0 to 100
- void HCMP3.sleep(state);
Sets the sleep state of the module where:
state is a boolean value which specifies the sleep state. Valid values for state are:
false = wakes the module from sleep
true = puts the module to sleep
- void HCMP3.reset();
Resets the module
- void HCMP3.getPlayState();
Gets the current playback state of the module
Returns an 8 bit value containing the current state of the module where:
STATUS_STOP no files are currently playing
STATUS_PLAY module is currently playing a file
STATUS_PAUSED module is currently paused
STATUS_ERROR There was an error reading the status.
- void HCMP3.getVolumeState();
Gets the current volume level
Returns an 8 bit value representing the current volume level in % where:
0 is the minimum volume level
100 is the maximum volume level
- void HCMP3.getCardState();
Gets the current status of the flash card
Returns an 8 bit value containing the current state of the card where:
CARD_EJECTED A flash card in not currently connected to the module
CARD_INSERTED A flash card is connected to the module.
CARD_UNKOWN The state of the flash card is unknown.
STATUS_ERROR There was an error reading the status.
Example Sketch:
- /* FILE: HCMP3_Example.cpp
- DATE: 09/05/19
- VERSION: 1.0
- AUTHOR: Andrew Davies
- 09/05/19 version 1.0: Original version
- This example sketch demonstrates how to use the HCMP3 library to control
- an audio playback device.
- Currently this library supports the following module:
- Serial MP3 playback module with 1W speaker (SKU: HCARDU0138)
- Connect the above module as follows:
- Arduino.........MP3 Module
- RX..............3
- TX..............2
- VCC.............5V
- GND.............GND
- Within the HCMP3 library folder you will find a folder called 'MP3_Files'
- Copy the contents of this folder to the root directory of your flash card.
- This library is provided free to support the open source community.
- We spend most of our time creating free content like this because we genuinely
- want to support the open source community. PLEASE SUPPORT US so that we can
- continue to create free content by purchasing products from our store -
- HOBBYCOMPONENTS.COM
- You may copy, alter and reuse this code in any way you like, but please leave
- reference to HobbyComponents.com in your comments if you redistribute this code.
- This software may not be used directly for the purpose of selling products that
- directly compete with Hobby Components Ltd's own range of products.
- THIS SOFTWARE IS PROVIDED "AS IS". HOBBY COMPONENTS MAKES NO WARRANTIES, WHETHER
- EXPRESS, IMPLIED OR STATUTORY, INCLUDING, BUT NOT LIMITED TO, IMPLIED WARRANTIES OF
- MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE, ACCURACY OR LACK OF NEGLIGENCE.
- HOBBY COMPONENTS SHALL NOT, IN ANY CIRCUMSTANCES, BE LIABLE FOR ANY DAMAGES,
- INCLUDING, BUT NOT LIMITED TO, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES FOR ANY
- REASON WHATSOEVER.
- */
- /* Include the library */
- #include "HCMP3.h"
- /* Digital pins used for serial communication */
- #define RX_PIN 2
- #define TX_PIN 3
- /* Create an instance of the library */
- HCMP3 HCMP3(TX_PIN, RX_PIN);
- void setup()
- {
- Serial.begin(9600);
- HCMP3.reset();
- /* Get the current status of the module */
- Serial.println("MODULE STATUS");
- Serial.print("Card state....");
- PrintCardState();
- Serial.print("Playback state.....");
- PrintPlaybackState();
- Serial.print("Volume level....");
- Serial.println(HCMP3.getVolumeState());
- Serial.println("");
- delay(2000);
- /* Change the volume level */
- Serial.print("Setting volume level to....");
- HCMP3.volume(10);
- Serial.println(HCMP3.getVolumeState());
- delay(2000);
- }
- void loop()
- {
- /* Play the first file */
- Serial.println("Playing first file on card");
- HCMP3.play();
- delay(2000);
- /* Play the next file */
- Serial.println("Playing next file");
- HCMP3.playNext();
- delay(2000);
- /* Play the previous file */
- Serial.println("Playing previous file");
- HCMP3.playPrevious();
- delay(2000);
- /* Play the first file in folder 1 */
- Serial.println("Playing file 1 in folder 1");
- HCMP3.play(1,1);
- delay(5000);
- /* Pause playback */
- Serial.print("Pausing....");
- HCMP3.pause();
- PrintPlaybackState();
- delay(2000);
- /* Resume playback */
- Serial.print("Resuming....");
- HCMP3.play();
- PrintPlaybackState();
- delay(2000);
- /* Turn the volume down in increments */
- Serial.print("Volume down");
- for(byte i = 0; i < 20; i++)
- {
- Serial.print(".");
- HCMP3.volumeDown();
- delay(100);
- }
- Serial.println("");
- /* Turn the volume up in increments */
- Serial.print("Volume up");
- for(byte i = 0; i < 20; i++)
- {
- Serial.print(".");
- HCMP3.volumeUp();
- delay(100);
- }
- Serial.println("");
- delay(5000);
- /* Stop the playback */
- Serial.print("Playback...");
- HCMP3.stop();
- PrintPlaybackState();
- while(1);
- }
- /* Prints out the current playback state */
- void PrintPlaybackState()
- {
- switch(HCMP3.getPlayState())
- {
- case(STATUS_STOP):
- Serial.println("Stopped");
- break;
- case(STATUS_PLAY):
- Serial.println("Playing");
- break;
- case(STATUS_PAUSED):
- Serial.println("Paused");
- break;
- }
- }
- /* Prints out the current flash card state */
- void PrintCardState()
- {
- switch(HCMP3.getCardState())
- {
- case(CARD_EJECTED):
- Serial.println("Ejected");
- break;
- case(CARD_INSERTED):
- Serial.println("Inserted");
- break;
- case(CARD_UNKOWN):
- Serial.println("Unknown");
- break;
- }
- }
Latest version 1.1
Old versions
V1.0:
Disclaimer: Libraries, example code, and diagrams within this forum thread are provided as an additional free service by Hobby Components and are not sold as part of any product. We do not provide any guarantees or warranties as to their accuracy or fitness for purpose.
Descriptions and diagrams on this page are copyright Hobby Components Ltd and may not be reproduced without permission.