First project: Motion activated random mp3 player

Other than some simple flashing LEDs this is my first Arduino project. I came up with an idea on the fly (in this thread actually) to build something that would play sounds and possibly flash LEDs when someone walks up to it or by it. I thought it might be difficult at first but this past weekend I decided to give it a go.

I started out with a cheap mp3 player:

The way it works is to turn it on you press and hold play for a little over a second to turn it on. It takes a few seconds to power up. Then it has all the standard buttons you would expect. Forward/Back, Volume, etc... I figured if I could bypass the play and forward button I could control it with the Arduino.

So I ripped it apart:

And lucky for me you can see in the next pic the 2 far right buttons are the play and forward track buttons. If they had been next to the battery it would have been a lot tougher.

Looking at the buttons I saw some open contacts that were not being used. Measured with a milti meeter to make sure they worked and they did.

I took some magnet wire (24 gauge I think) cause it was tiny and flexible and did my best to solder to the tiny contacts.

It took a lot of time and the solder joints were not very strong. 2 wires fell off and had to be reattached. Finally I bent them down and taped them in place to reduce the stress on them.

Even after this the far left one fell off and had to be redone but I think they're all pretty good now.

I hooked it up to a breadboard with buttons to test it out. WARNING: Very boring video here. (Takes a bit to load.)

Next I moved to the Arduino with protoboard. Got some 2n2222a transistors and various resisters and tried to hook it up but I couldn't get it to work. I hooked up an LED and the Arduino would make that work but I couldn't make them switch the mp3 player on or forward the tracks. It dawned on me later that I wasn't using a common ground and that was probably my problem. But I figured finding ground on the player would be difficult. Instead I opted for relays. I went to my local electronics surplus store (Halted Electronics) and picked up these relays.

Got it wired up right and it worked flawlessly.

I wrote a little code and got it running in a loop:

Turn on
Forward a random # of tracks
Power down

I had also ordered some Sharp IR sensors so next I plugged that in and added some code. I honestly thought that would be the hardest part but I had that working in about 10 minutes.

Here is the whole setup:

I'll post the code in the next post.

Any feedback is appreciated.


#include <SoftwareSerial.h>

Random Sound Player
By biocow
November 2010

int distanceVal;

const int numReadings = 10;

int readings[numReadings]; // the readings from the analog input
int index = 0; // the index of the current reading
int total = 0; // the running total
int average = 0; // the average

int inputPin = A5;

void setup() {
// initialize the digital pin as an output:
pinMode(6, OUTPUT); // On, Play and pause
pinMode(7, OUTPUT); // Forward track
pinMode(0, INPUT); // will listen for the end of the track

for (int thisReading = 0; thisReading < numReadings; thisReading++)
readings[thisReading] = 0;


void loop() {

// Get the readings from the IR sensor
// and smooth them out.

// subtract the last reading:
total= total - readings[index];
// read from the sensor:
readings[index] = analogRead(inputPin);
// add the reading to the total:
total= total + readings[index];
// advance to the next position in the array:
index = index + 1;

// if we're at the end of the array...
if (index >= numReadings)
// ...wrap around to the beginning:
index = 0;

// calculate the average:
average = total / numReadings;
// send it to the computer (as ASCII digits)
Serial.println(average, DEC);

// is someone there? Let's see

if(average > 30) {

// turn on the MP3 player
Serial.println("turning on player");
digitalWrite(6, HIGH);
digitalWrite(6, LOW);

// wait for it to power up
delay (4500);

// advance one track to make sure we're at the start of a song
digitalWrite(7, HIGH);
digitalWrite(7, LOW);
Serial.println("advance one track");

// Play the current track
digitalWrite(6, HIGH);
digitalWrite(6, LOW);

// listen to the current song for a bit
// later we'll wait and listen for the end of the track
Serial.println("How do you like this song?");

// pause the song
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
Serial.println("Song is over");

// Advance the tracks a random number of times
for(int x = 1; x <= random(1,7); x++) {
digitalWrite(7, HIGH);
digitalWrite(7, LOW);
Serial.print("Advance track ");
Serial.println(" time(s)");

// delay

// turn off the MP3 player
Serial.println("shutting down");
digitalWrite(6, HIGH);
digitalWrite(6, LOW);
delay(5000); // give it time to turn off

// Reset all the sensor stuff
average = 0;
total = 0;
for(int l = 0; l < numReadings; l++) {
readings[l] = 0;

// wait a good bit of time before we reset
int pause = random(10000,20000);
Serial.print("Pausing for ");
Serial.print(pause / 1000);
Serial.println(" seconds");



Nice job. I did something similar with a waveshield from Adafruit. I used it as a doorbell when anyone walked into my office.
The only frustrating part about the waveshield is having to format all your MP3 files into WAV format.

Thanks. Nice idea.

I just bought 2 RBBB kits, one for this project and one for another. I figure the form factor goes nicely with the shape of the mp3 player. I plan to stuff it all into a little project box with a single headphone jack.

I sit in a cube but my desk faces the opening so I could place it on my desk and have it play something when someone comes in or walks by.