Example Code for Arduino-Volume and Playback
Last revision 2026/01/22
This guide demonstrates how to use Arduino to manage audio playback and volume settings through example code, covering hardware and software preparation, wiring, and module integration.
Hardware Preparation
- DFRduino UNO R3 with IO Expansion Shield and USB Cable A-B ×1
- Voice Module ×1
- Passive Speaker (8Ω3W) ×1
- Dupont lines
Software Preparation
- Arduino IDE,click to Download Arduino IDE from Arduino®.
Wiring Diagram
| UNO R3 | PIN | Voice Module | PIN | Passive Speaker | PIN |
|---|---|---|---|---|---|
| UNO R3 | VCC | Voice Module | +(Gravity) | / | / |
| UNO R3 | GND | Voice Module | -(Gravity) | / | / |
| UNO R3 | Digital 10 | Voice Module | T(Gravity) | / | / |
| UNO R3 | Digital 11 | Voice Module | R(Gravity) | / | / |
| UNO R3 | / | Voice Module | SP+ | Passive Speaker | +(RED) |
| UNO R3 | / | Voice Module | SP- | Passive Speaker | -(BLACK) |
Other Preparation Work
The module has built-in audio, if you need to add or replace audio, please use the micro usb to connect to the computer for updating. 2. The way to update the audio is the same as USB flash driver. 3. This module supports MP3 and MAV format audio files. 4. The file should be stored in the "ZH" folder and it is recommended to represent files with numbers. Such as 01.mp3,02.mp3, two letters or one Chinese character are also OK.
Sample Code
/*
* @file Voice Module.ino
* @brief
* @n [Get the module here]
* @n This example Set the voice module volume and playback
* @n [Connection and Diagram]()
*
* @copyright [DFRobot](https://www.dfrobot.com), 2016
* @copyright GNU Lesser General Public License
*
* @author [carl]([email protected])
* @version V1.0
* @date 2017-11-3
*/
#include <SoftwareSerial.h>
SoftwareSerial Serial1(10, 11);
unsigned char order[4] = {0xAA,0x06,0x00,0xB0};
void setup() {
//Serial.begin(115200);
Serial1.begin(9600);
volume(0x1E);//Volume settings 0x00-0x1E
}
void loop() {
play(0x01);//Play the specified audio:0x01-file0001
// Serial1.write(order,4);//order play
delay(2000);
}
void play(unsigned char Track)
{
unsigned char play[6] = {0xAA,0x07,0x02,0x00,Track,Track+0xB3};
Serial1.write(play,6);
}
void volume( unsigned char vol)
{
unsigned char volume[5] = {0xAA,0x13,0x01,vol,vol+0xBE};
Serial1.write(volume,5);
}
Result
Play the audio "Do".
Was this article helpful?
