This is only a preview of the July 2017 issue of Silicon Chip. You can view 44 of the 104 pages in the full issue, including the advertisments. For full access, purchase the issue for $10.00 or subscribe for access to the latest issues. Items relevant to "RapidBrake - giving the guy behind extra stopping time":
Items relevant to "Deluxe Touchscreen eFuse, Part 1":
Items relevant to ""Over-the-Top" rail-to-rail op amps":
Items relevant to "The low-cost VS1053 Arduino audio playback shield":
Items relevant to "We put the VS1053 Arduino shield to work":
Items relevant to "Completing our new Graphic Equaliser":
Purchase a printed copy of this issue for $10.00. |
MP3...WAV...MIDI...FLAC...OGG... and many more
Music Player
by Bao Smith
Got some music stored on your SD Card? Here’s an Arduino-based player
that handles most common formats. It’s cheap, easy-to-build and it will
even record as well. Build it . . . and be amazed!
T
his project uses an Arduino “MP3
shield” described in greater detail
on page 72 of this issue. This delivers
audio to a 3.5mm stereo jack which can
be plugged directly into earphones/
headphones or to an audio amplifier
to power larger speakers. For recording, it has an on-board electret microphone along with a mono 3.5mm jack
socket, which allows an external microphone to be used.
All the audio decoding/encoding and playback/recording functions are handled by the VS1053 IC
on this shield. For more information
on that IC, see the separate article on
the shield.
As well as playing back the compressed formats listed in the introduction, this IC is also capable of playing
back General MIDI files and also uncompressed WAV files. It's an impressive chip, with quite a few extra feasiliconchip.com.au
tures that we aren’t using in this project, such as digital tone controls. And
of course, you can modify the software
to take advantage of those features if
you want to.
While this device is capable of playing back a number of formats, most
constructors would use it to play back
either MP3 or AAC files, which are the
de-facto standard formats for digitally
compressed audio.
Pretty much any music player
should play back these formats. Until recently, MP3 was patented and so
required licensed software for decoding. But that patent (held by the German firm Fraunhofer IIS) expired on
the 23rd of April this year, so it is now
an open format.
Because it was patented, the similar open-source Ogg Vorbis standard
was established. And more recently,
AAC was developed, aimed mainly at
providing either higher audio quality
than MP3 at the same file size, or (in
the case of AAC+) reasonable quality
with a much smaller file size (bit rate).
We’ve chosen to use the VS1053based Arduino shield because it is
low cost, can play back all these file
types and has a decent on-board DAC
which is able to power nominally 30W
loads such as many headphones and
earphones; the minimum nominal
load impedance it can handle is 16W.
Construction
Building this project is quite simple,
with most of the connections made either by plugging a shield directly into
the Arduino board or by connecting an
external module to that same board using one of 12 short jumper wires. These
are fitted with a male DuPont connector at one end and a female at the other.
The first step is to slot the MP3
July 2017 77
Fig.1: wiring diagram for the Arduino Music
Player project. The MP3 player shield should
just slot in one-to-one with the Arduino Uno.
The LCD screen with serial I2C can either go into
Analog pins 4 & 5, or the SCL & SDA pins on the
Uno as they are wired in parallel. You might find
that your keypad has a slightly different layout,
so it’s best to check with an ohmmeter what pin
combinations correspond to which key.
shield into the Arduino Uno (or equivalent), with the power connections
matching up and the SD card slot on
top of the USB connector. While you
can upload the software at this point
and start experimenting, with control
via the USB serial monitor in the Arduino IDE, we will describe the remaining assembly steps first.
The next job is to get the serial LCD
screen running. We used a 16x4 screen
but it will also work with a 20x4 alphanumeric display, however, you will
need to modify one line in the software and the whole screen will not be
utilised by default. The software could
78 Silicon Chip
also be modified to work with a smaller
16x2 screen, if you wish.
Refer to Jim Rowe's El Cheapo Modules series article titled “part 5: LCD
modules with I2C” in the March 2017
issue for details on how these I2C LCD
modules work.
If you haven’t already fitted the I2C
module to the LCD, generally it’s just
soldered on the back, with a 1:1 correspondence between its pins and those
on the main LCD board.
We used a slightly unusual LCD
module that we have on hand with
a 20-pin connector, with pins 19 &
20 used for the back-light anode and
cathode connections, compared to a
more typical display which has these
connections on pins 15 and 16. So you
may notice on the photos that we soldered wire links between pins 15 and
19, and 16 and 20 to make the backlight work.
Once you have the I2C module soldered to the LCD, the four I2C/power
supply wires are easily connected to
the MP3 shield (already connected to
the Arduino board below), as shown
in Fig.1. The I2C module normally has
male pin headers while the Arduino
shield has female sockets on top, hence
we have specified male/female jumper
leads to make these connections.
Note that our software assumes the
I2C module uses an address of 0x3F;
some I2C modules use a different address (eg, if it uses a PCF8574T [0x27]
rather than the PCF8574AT). So we
suggest you check the supplied documentation and if necessary, change
the LCD address in the software before
uploading it.
The final set of connections to be
made are between the 4x4 keypad and
MP3 shield as shown in Fig.1. We’re
using a standard switch matrix type of
keypad which is commonly available.
If your keypad doesn’t have any pin
siliconchip.com.au
headers but rather has a series of pads
with holes on its PCB, you will need to
solder a male pin header onto it before
proceeding (straight or right angle).
The 4x4 keypad uses eight connections, four for the rows and four
for the columns. After deducting the
pins used by the “MP3 shield” and
LCD, we’re left with ten free pins on
the Arduino module and only a few of
these are digital pins. Luckily, analog
pins can also be configured as digital
I/Os, so wire up the keypad as shown
in Fig.1.
Note that your keypad may use different row and column connections
to ours but you can easily check this
by setting a DMM in continuity mode,
connecting it across each pair of pins
and pressing each key on the keypad.
You should quickly be able to determine which pins connect to the rows
and columns.
Note that after plugging in the keypad, the only pin remaining to connect
anything else to your board is digital
pin D1. If you do need to add other
devices, consider using I2C devices
which can be simply wired up in parallel with the LCD as long as they do
not use the same I2C address.
Software operation
We won’t go into much detail regarding how the software works here; you
can download and examine the code if
you want to see how it works, or make
any changes.
However, it can be helpful to know
how audio data is piped to the VS1053.
The VS1053 has a 2048-byte buffer
which can receive up to 32 bytes of
data at a time. The data is sent via an
SPI serial bus when the DREQ pin is
high (this pin also indicates the chip
is ready to receive a command over
the SCI interface).
Data can be sent either most-significant byte first (SM_SDIORD [0x9]
set to 0) or least-significant byte first
(SM_SDIORD set to 1) format; the
SM_MODE register address is 0x4800
and this is configured over the SCI interface.
The software requires four libraries to function, with one of them requiring minor changes to the code.
The libraries required are SdFat,
SFEMP3Shield (https://github.com/
madsci1016/Sparkfun-MP3-PlayerShield-Arduino-Library), LiquidCrystalI2C (https://github.com/fdebrabander/Arduino-LiquidCrystal-I2Clibrary) and Keypad (http://playground.
arduino.cc/Code/Keypad#Download).
It's important to mention that the
version of SdFat we are using will
only open files that use the old DOS
8.3 file naming convention (8 characters for name, 3 for extension). Files
with names that do not match this format will not display properly and may
cause other problems. The reason for
this restriction is to save on memory.
The software could be modified to
support long file names but given that
the basic Arduino module only has
32KB of flash and 2KB of RAM, and
our software pretty much fills this, you
would need a more powerful Arduino
board to use the modified software (eg,
one with an ATmega2560 chip rather
than ATmega328).
The software is capable of uploading various patches or “plugins” to
the VS1053b chip. These can fix bugs
in its firmware or add extra capabilities. They are stored in “little-endian”
binary format on the microSD card,
with a file extension of “.053”.
These files can be created from .plg
plugin files downloaded from VLSI's
website (www.vlsi.fi/en/support/software/vs10xxpatches.html), using a
conversion tool that comes with the
SFEMP3Player library called "vs_plg_
to_bin.pl".
Note that you might have trouble
running the above Perl script if your
version of Perl is lower than 5.10.0. As
on line 59, a parameter for the pack()
function makes use of the “<” modifier to force the data to be stored in
little-endian format. You remove this
modifier if you know your CPU architecture will only deal with data in
little-endian format, or alternatively,
you can set SM_SDIORD to 0 when
sending the plugin data.
The reason for converting them is to
save storage space and speed up loading them into the VS1053b IC; even
when converted to binary form, they
can be quite large. The SFEMP3 library
supplies some plugin files along with
the software, already having been converted to this format. Their uses are
described below.
Installing the software
The Arduino Music Player is easy to build, consisting of just four modules
which plug together; either directly or via jumper leads.
siliconchip.com.au
If you don’t already have the Arduino integrated development environment (IDE) on your PC, download
it and install it now. It’s available for
free, from www.arduino.cc/en/Main/
Software We used version 1.6.12; SdFat requires version 1.6 or higher of the
Arduino IDE to function, but it would
be possible to convert it to function on
earlier versions.
Make sure all four libraries mentioned above have been installed or
you will not be able to compile the
July 2017 79
software. Minor changes were made
to the SFEMP3Shield software to expose some internal functions required
by our sketch.
This modified version of the library
will be included in the download
package on the Silicon Chip website,
along with the actual sketch itself.
Once you have the library ZIP files, you
can install them in the Arduino IDE
via the Sketch→Include Library→Add
.ZIP Library menu option.
You will also need the two VS1053b
plugin/patch files named "patches.053" and "oggenc.053", also included in that download package. When
the sketch runs, it looks for these files
in the root directory of the microSD
card, loads them into its memory and
then uploads them to the VS1053b IC.
The first patch file is an update to
the chip’s firmware that fixes some
bugs and also adds extra capabilities.
Some of the bug fixes are required to
play certain audio files that would otherwise return errors.
The second is the code which allows
the chip to record in Ogg Vorbis format.
Without these files, the unit will not
function properly. Once you’ve placed
these files on the microSD card, along
with whatever audio files you want to
play back, plug it into the socket on
the shield.
Note that the SD card should be
formatted with either FAT16 or FAT32
file systems to work with the SdFat
library.
Having ensured the libraries are
installed, open up the sketch (called
“VS1053_example.ino”) in the Arduino IDE and plug the Arduino into your
PC using an appropriate USB cable.
We assume you know how to select
the appropriate USB serial port in the
Arduino IDE; if you’re unsure, we’ve
described this procedure before, for
example, in the Arduino-based Digital LC Meter article, published in the
June 2017 issue (on pages 35 and 36).
You can now use the Sketch→Upload
menu item to load the software into the
Arduino.
Using the module
When the software has been loaded
and the module is up and running, you
should notice that the display has lit
up and menu should be shown. This
menu can be navigated using the keypad with the keys labelled 2/8 corresponding to up/down, 4/6 to left/right,
5 being “enter” (selecting the menu
80 Silicon Chip
Parts List
1 Arduino Uno or equivalent with ATmega328 chip (Jaycar Cat XC4410)
1 Geeetech Arduino MP3 Player Shield (Silicon Chip online shop Cat
SC4315)
1 20x4 or 16x4 LCD screen with I2C backpack module (Silicon Chip
online shop Cat SC4203)
1 4x4 matrix keypad (other sizes will also work with software changes)
1 8-pin header, 2.54mm pitch, straight or right angle (to suit keypad)
12 male-female DuPont jumper leads (Jaycar Cat WC6028)
1 USB Type-A to Type-B “printer” cable (to suit Arduino module)
1 microSD card formatted in FAT16/32, capacity to suit application
1 7-12V DC plugpack with 2.1mm inner diameter plug (optional, for
standalone use)
entry that the cursor is currently on)
and * functions as a general “back”
key in all menus.
The menu items that are available on
the main menu, by default, are:
0. plays all music files available on
the SD card. Pressing 4/6 will go to
the previous/next song, “A” will
restart the song from the beginning, 5 will pause/unpause, 1/3
will decrease/increase playback
speed and 7/9 will increase/decrease playback volume.
During playback, it will display
the file name, current time and a
VU meter signal level for the left
and right channel. Playback can
be stopped by pressing the “*” key
and it will stop automatically after
the last file has been played.
Note that there is a limit of 50
files due to memory constraints
(2 bytes are required per file).
The software could be modified
to remove this limitation but then
it would not be possible to step
backwards, to the previous file;
you could only skip forwards
through the list.
1. lets you select a file to play.
2. record to an .ogg file named recordXX.ogg, where XX starts at 00
and is incremented per recording.
3. toggles between mono and stereo output
4. resets the VS1053 chip to its default settings
5. produces a test sinewave from
the outputs
6. turns on/off the differential output mode
7. put the VS1053 IC in low-power
sleep mode
8. exit sleep mode
Recording uses the on-board electret
microphone. If you want to use the line
input instead, you need to add a line
at the top of the sketch which reads
“#define USE_LINEIN" without quotation marks. The audio is recorded
in Ogg Vorbis format at approximately
128kb/s and saved as an “.ogg” file.
In addition to the keypad/LCD interface described above, the software
can be controlled from your PC using
the serial monitor.
You simply read the menu options
that are displayed on the serial monitor and choose one by pressing the
associated key on your keyboard. This
interface has additional options which
can be used for debugging.
Making simple changes to the
code
Experienced programmers should
have no problems changing the software to suit their needs but even relative beginners can make some changes,
as described below.
A constant called MAX_INDEX defines how many files the software can
handle on the microSD card. Changing this will also affect how much
SRAM is used. If you increase it too
much, there won’t be enough free
memory for the software stack and it
will no longer work properly. Each
additional entry will take another 2
bytes of SRAM.
The definitions LINE1, LINE2,
LINE3 and LINE4 are the addresses of
each line of the display on your LCD
module. The module we used has the
first line at address 0 hex, the second
line at 40 hex, the third line at 14 hex
and the fourth line at 54 hex.
This is pretty standard and most
displays should use the same addresses, but if yours is only displaying the
first line correctly, you may need to
change these.
siliconchip.com.au
Silicon Chip
Binders
REAL
VALUE
AT
$16.95
*
PLUS P
&
P
Here you can see the initial display when the unit is first powered up after
loading the software, showing part of the main menu. The underline indicates
which menu item will be selected when you press enter (5 on the keypad).
Our I2C LCD uses the PCF8574AT
IC and so the software is set up from
an I2C address of 3F hex. If your LCD
interface has a PCF8574T, change the
line which reads:
LiquidCrystal_I2C lcd(0x3F,
LCD_COLS, LCD_ROWS);
to:
LiquidCrystal_I2C lcd(0x27,
LCD_COLS, LCD_ROWS);
Regarding the changes made in the
SFEMP3 library for this project, they
can be summarised as the following:
1) playTrack(uint8_t) was changed
to playTrack(uint16_t) to allow more
than 255 files on the same card (using the naming scheme trackX.mp3,
where X is a number between 0-999).
2) isFnMusic(char *) was changed to
also check for Ogg Vorbis files.
3) the following functions in the
SFEMP3 class were changed from “private” to “public” to allow the sketch
more control over the VS1053b IC:
void getTrackInfo(uint8_t, char*,
uint8_t); // Gets MP3 ID3
metadata (force appends a
null-byte)
static void
Mp3WriteRegister(uint8_t,
uint16_t); // Write 16-bits to a
given register
static uint16_t Mp3ReadRegister
(uint8_t); // read from a given
register
siliconchip.com.au
uint8_t VSLoadUserCode(char*);
// Load a .53 format plugin
Additional uses
As noted earlier, the SFEMP3Player
library has treble and bass controls,
however, our software does not use
these functions.
If readers want to work on the software, this would be one area to start.
Another improvement that could be
made is to take advantage of the library’s functions for reading header
information (eg, ID3 tags), and display
it on the LCD during playback.
The chip itself is surprisingly powerful, handling all the decoding/encoding itself in real time. It can also be
used as a graphic equaliser and MIDI
synthesiser.
VLSI also offer software to use the
chip as a FIR (finite infinite response)
filter, in combination with the free
GNU Octave software. This can be
found under VSIDE DSP Library:
www.vlsi.fi/en/support/software/
vs10xxapplications.html
Using it is outside the scope of this
article, though.
The VS1503 manufacturer's website
can be found at www.vlsi.fi/en There
you can find other example programs
(not for Arduino) along with a list of
patch files (www.vlsi.fi/en/support/
software/vs10xxpatches.html) and bonus functionality that can be loaded
SC
into the chip.
Are your copies of SILICON
CHIP getting damaged
or dog-eared just lying
around in a cupboard or
on a shelf? Can you quickly find a particular issue
that you need to refer to?
Keep your copies
safe, secure and
always available with
these handy binders
These binders will protect your
copies of SILICON CHIP. They
feature heavy-board covers,
hold 12 issues & will look great
on your bookshelf.
H 80mm internal width
H SILICON CHIP logo printed
in gold-coloured lettering on
spine & cover
Silicon Chip Publications
PO Box 139
Collaroy Beach 2097
Order online from www.
siliconchip.com.au/Shop/4
or call (02) 9939 3295 and
quote your credit card number. *See website for overseas prices.
July 2017 81
|