Skip to content

dsPIC WAV Player

clip_image002In this article Bernard Klinc demonstrates audio playback using the dsPIC on a simple breadboard layout with a free code library for SD card access from Microchip. Bernard will receive a giveaway item of his choice from the hobby parts for articles project here at uCHobby.

This project is mostly around the Microchip MDDFS library (Memory Disk Drive File System). It’s a neat pile of C source code that can manage a FAT12, FAT16 or FAT32 file system and interface to a SD or CF memory card or an IDE hard drive. But best of all its free!

Its downside is only the amount of memory it needs. With all of its features enabled it alone takes up a huge 23KB or flash and 1.4KB of RAM. By disabling features the flash usage can drop drastically but will still at least need 1.3KB of RAM with all extra features disabled

It has taken me some time to get it to compile without errors but once I got it compiled I put in a SD card gave it power for a few seconds, try to read the card and wow there was a txt file created on the card. That was almost too easy!

clip_image004After some fooling around with it I decided to try and play audio directly from the card. First I needed a way to output an analog voltage out of the pic. The easiest method I know is PWM. As you know PWM is used a lot to control DC motors and things like that but if we use a PWM frequency well out of out hearing range it can output an audio signal. Here a dsPIC30F4013 was used. Luckily it has 4 PWM outputs. So I just used one of those and made it run at about 100 KHz. This pic can do 8 bit accuracy at such a high frequency. Then we just use a RC low pass filter to smooth it out in to a nice analog signal.

clip_image006clip_image008As for connecting the card to the pic it connects like a standard SPI device using the usual 4 pins. The pin out is easily found on the web.Note that SD cards are NOT 5V!!! They work on 3.3V. First I simply ran the whole thing on 3.3V but then I wanted to run the dsPIC at its highest speed and it can’t do it on only 3.3V. Because of this I put it on 5V but made a simple regulator with a 3.3V Zener diode to supply the SD card also resistors with series capacitors between the dsPIC and the card so the 5V signals can’t hurt the card. We need to use this simple circuit on the SDO, SCK and CS pins. The SDI pin does not need it because that signal is produced by the SD card so it is 3.3V

Next I needed to know how to get audio data from a *.WAV file. Basically in the beginning of the file it’s described what kind of WAV is it (Sample rate, bit depth, number of channels…) and after that it’s what’s called a data chunk. It’s simply the raw audio data in the format specified in the beginning of the file. The raw data here is actual sound values without any compression or encryption or what so ever (This is considering it’s a standard WAV without any codecs). For more in depth info about the WAV format check links.

All that we need now is some kind of link between the raw data from the WAV file to the PWM output. The reading from the SD card wont happened at a constant speed so we need to get a nicely constant stream of data to the output. This is done by a cyclic buffer. This is a kind of buffer that has no beginning or no end, it’s a circle. So what is happening is that the point that reads from it is moving around the circle at exactly the right speed for the WAVs sample rate (For a 22 KHz WAV file this is exactly 22KB/S) and the reading from the card Is done by a second point that moves at a very non constant speed and what it tries to do it catch the reading point, once it has caught up to that point it has to wait for it to read out some data to make space for new data. When there is free space again it tries to fill it again. So now the data is flowing at a good constant speed in to the PWM module nicely.

And now finally the audio is coming out of the PWM pin on the pic. All that’s left now is plug in the audio jack and have a listen.

What is also needed is to have some code to read the description in the beginning. So it knows how and what at speed to play it. This is not all that difficult. It just needs to check on certain spots of the file. For example 22nd byte contains the number of channels there are in the file (1 = mono, 2 = stereo). But problem is my current hardware can only output mono 8 bit audio. If I utilized all 4 PWMs it would be possible to do 16Bit Stereo, but seems like it’s too slow to play that. But its probably my sloppy coding that’s too slow. But I am going to get my hands on a dsPIC33FJ128GP802. It has a faster CPU and a lot more memory and my favorite a built in audio DAC with its own hardware buffer.

clip_image010As for the user interface I simply used the com port. I have a ready built RS232-TTL cable so it’s just a matter of connecting it to the RX and TX pins. I have used it for debugging purposes but then made it in to a console that can be accessed with any terminal program like Microsoft HyperTerminal, PuTTY etc… A simple LCD could be quickly stuck on and some buttons to make a user interface without a computer.

As for the quality of the sound, it’s surprisingly good. The sound from it is easily better than a cheap PC sound card!

As for improvements there is a lot of room for that. First of all to get it to play the highest quality WAVs also it shouldn’t be that hard to make it record WAVs using the ADC. Also I have a MP3 decoder chip to make it able to play most MP3 files. A dsPIC is not fast enough for decoding MP3s using its CPU.

Supported formats:
8KHz, 11KHz,12KHz, 16KHz, 22KHz,24KHz,32KHz, 44KHz, 48KHz
Stereo or Mono 8Bit or 16Bit


Bernard’s dsPIC Wave Player Source Code

– WAV format :

– Microchip MDDFS lib

– uCHobby DAC article

– Similar WAV player

Posted in Development Tools, Discovering, Electronics Links, Hacks, Microcontroller, Projects.

24 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. SAM says

    thank u for this project it help to me

  2. dfowler says

    Those files are not available for this project. The diagrams and source code should be sufficient for you to recreate the authors efforts.

  3. Ferenc says

    hy please help my how to download this projects. Schematic, pcb, and pic hex file thanks. This is a very good projects.

  4. Mat says

    Would it be possible to post the code somewhere? Im doing a similar project (using an R2R ladder network as a DAC at the minute) and want to use PWM to keep costs down, but im new to PWM…..and its always good having reference known working code 🙂

  5. Berni says

    A MP3 playing version of this player is done!

    All the info you need is in this article

  6. jayakodi says

    sir,please send me the circuit diagram of this project

  7. Berni says

    Well no modifications needed actually. But i did get rid of the write protect and card detect pins because im using a homemade connector so i don’t have the switches on it to detect it.

  8. Dino says

    Are any changes required in the Microchip library for the code to work with MMC cards. I know MMC cards have less security options, like write select, etc. The example program that comes with the Microchip library uses SD cards. Would I need to remove references to the extra SD features or can I leave them in the code?

  9. Berni says

    You can find me on MSN messenger on (Its NOT a valid e-mail adress)

  10. George says

    Hello sorry I ask email Bernard klince ask for a job to develop a code source Thanks.

  11. Joe says

    Thanks Berni

  12. Berni says

    You cant do that because the max speed of the cards SPI bus is 25Mhz. To go faster you need to use the 4 bit bus in the card. But you wont find any documentation what so ever on the net because they are keeping it secret.

  13. Joe says

    Yes, the PWM is not difficult 😉 . My problem is how could I use the SPI bus to read a SD card with a rate access of 35Mb/s while my CPU is running at 30 MIPS ?

  14. Berni says

    Yeah i could make the code avalable. But its not that much codeing really. All i do is read a file out to the PWM module that is running at 100Khz.

  15. George says

    Excuse me could happen email Bernard klince because not find it on this page

  16. Joe says

    Hi ! Great job !
    Could you please share your code with us please. Im actually working on a small “PDA” using a PIC32 for my bachelor project.
    Your work gives me the idea to integrate a wav player in the de new PIC32. So, could you please give us more details about this free system library ?

    Thank in advance for your answer.

  17. Berni says

    For this project i would recommend a dsPIC33F family because of much larger memory, faster CPU and 3,3V full speed operation.

    Well i just got some 32 bit PICs. Huge memory,hell fast cpu and some nice preperials.Meaby il sometime try VGA monitors. Also microchip made PIC32 devices with USB host capability witch means you can have a full speed USB-A port on your project and connect other USB devices in it like mice,keyboards,thumb drive etc…

  18. VisNaicker says

    And for your next magic trick, you will perform … ? Excellent stuff.
    I have acquire a F4013 sample, bought a SD, and got my power supply, LCD, prototyping board all ready … I just need to jump in. One of these days ….
    Your next trick, Pal/NTSC video ?

  19. Berni says

    That looks like a nice board indeed and you could swap that 18F for a dsPIC for more advanced audio processing.

    The file sytsem is not the only thing microchip gives you, They also give you USB , TCP/IP, AC3 codec ,digital filters, noise reduction , voice recognition… and so on

  20. Kris says

    I never knew Microchip had a free file system library. I have always preferred the AVR over the PIC, but with this new knowledge, maybe I will give the PIC a try. I am thinking about buying this PIC development board from Olimex:

    It is a little expensive, but it looks like it has all of the components necessary for a project like this one. Thanks for sharing.

  21. Berni says

    Yeah it is a nice adapter, but since i couldnt find a SD card slot anywhere i just made it my self. The removal and insertion dosent go as easy as on a real SD slot but it works nicely

  22. dfowler says

    Some time ago I purchased a SD card adaptor board from Futurlec. I have not used this adaptor but it looks very good for about $7.

  23. Berni says

    Well this conversion method is not very good. It uses the internal protection diodes to clamp it to 3.3V. Crude but it works.

    As for the SD card connection i used a little pice of prefboard and bent a 6 pin male header so it pushes down on the card ageist the board and works perfectly .

  24. Steve Chamberlin says

    Wow, nice work! I’d love to see more of this kind of article on uC Hobby.

    Could you explain how the resistor and capacitor between the PIC and the SD card can convert from 5v to 3.3v? Seems like you’d need some kind of active level converter.

    How did you make the physical connection between the SD card and the PIC? Were you able to find some kind of SD card slot with pins you could use?