A different birthday present for my friend

I met Gabriel a few years ago at a security conference here in Argentina. We began good friends, we also used to work together and we share some taste and passions about electronics, computers, music, etc.

I thought a different birthday present would be nice, so I put hands on to build an audio player with some of our team/groups audio messages, most of them very hilarious. The gift has to be something funny, like a cheap teddy bear who play sounds.

Schematic circuit

Schematic circuit is very simple. I used an old Arduino Nano V3.0 that I bought like 7-8 years ago, a SD card module, a BC548 (or any NPN transistor), a 220 Ohm resistor and an old speaker I recycled from a broken set of headphones.

The audio output is based on a pulse width modulation setup (PWM) thanks to the TMRpcm library, then it’s amplified by a common-emiter amplifier NPN transistor. The 220ohm inline resistor helps to avoid input saturation on the amplifier base. You may want to play with the tmrpcm.setVolume() function. a value of 5 is ok for this configuration, more than 5 induces a lot of distorsion and saturation.

As I’m not looking for a high quality audio amplifier, neither I got AC to get filtered, the amplifier circuit is SUPER simple, but not the best in terms of quality, noise filtering and so on. I highly recommend this page: http://www.electronics-tutorials.ws/amplifier/amp_2.html to learn about common emitter amplifiers

Benchtesting the audio player

Software

#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
char *randomAudio[] = {"1.wav", "2.wav", "3.wav", "4.wav", "5.wav", "6.wav", "7.wav", "8.wav", "9.wav", "10.wav", "11.wav"};

TMRpcm tmrpcm;

void setup() {
randomSeed((analogRead(0) + analogRead(1) + analogRead(2)) / 2);
tmrpcm.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}

tmrpcm.setVolume(5);
tmrpcm.play(randomAudio[random(0, 10)]);
}

void loop() { }

The code is pretty simple. I use the TMRpcm library to play the wav files from the SD card. Audio files must be WAV 8-bit unsigned, mono at 16000 hz, SD card must be formated on FAT. Audio files can be generated or converted using Audacity.

You may found that you need to install the TMRpcm library. If you are using the Arduino IDE, go to Sketch > Include library > Manage libraries…

And search for tmrpcm, click over it, then click on install.

I also use the random function from Arduino, fed by a randomSeed who is created based on analog inputs. As long as they are hi-Z inputs (and they are not connnected to anything), variable input values creates unique seeds everytime the Arduino is powered on.

Putting everything together

As I don’t have too much time to do a case, neither a custom PCB I just soldered the SD card module, Arduino and audio amplifier over an old board, I added a switch to power it on, attached to a 9V battery.

I removed some of the stuff  from inside the teddy bear to make some place to put all the electronics inside. I placed the speaker in front of the teddy, somewhere near the belly. I left the board near the switch, so removing the switch makes enough place to remove the PCB and/or take the SD card to replace/add more sounds.

The finally assembled teddy bear looks like this, just a power switch on the back. I also added a red LED on the front of the teddy to know if it’s on or not.

Proof of concept