Ultrasonic-triggered MP3 player with Arduino (old, probably outdated)

Warning: This page is pretty old and was imported from another website. Take any content here with a grain of salt.

This small MP3 player is triggered when the ultrasound sensor notices someone nearby for a longer period of time (must be closer than 70cm for >0.9s). The hardware uses:

I hacked the Music Shield to include risers for some of the pins. This way it’s possible to connect extra sensors (past just the buttons on the board) and use them with the Arduino. Others have done this by connecting the Music Shield with cables to the Arduino. Soldering the risers was a bit tricky because you want them straight, and perhaps even with the plastic distance holders… This should really be on the board by default, or trivial to connect (extra holes). Update: it looks like v2 of the board (archive.org) (shop (archive.org)) has it.

The code essentially turns off the hardware buttons on the Music Shield, so that they can be used for our ultrasound sensor (needs trigger output & echo input) as well as the watchdog LED. Pins D2-D9 can be used for I/O, in this case. We’re using 2 for the LED, and 4 + 5 for the ultrasonic sensor.

The code requires the updated library (I used v1.7) of the Music Shield libraries, which are currently at https://github.com/amperka/music-shield . Note that you will also need the FAT16 & NewSPI libraries, which are also there. On a Mac, you can copy these libraries “into” your Arduino app: Go to Finder / Applications / right-click on Arduino, select Show Package Contents, open Contents, Resources, Java, libraries, and copy the 3 folders there. I’m sure other OS have a more sane approach to that :).

Using a LiPo-Rider (archive.org) circuit that provides power via USB from a LiPo battery, you can connect a passive USB hub, the Arduino and a small USB-powered speaker, to make it a fairly self-contained device that you can stash somewhere and let it surprise visitors. “Hello? … What do you want?”

Code: (gist (archive.org))

#include <arduino.h>
#include <Fat16.h>
#include <Fat16Util.h>
#include <NewSPI.h>
#include “MusicPlayer.h”
MusicPlayer myplayer;

// I/O pin usage
#define US_TRIG 4 
#define US_ECHO 5

// debug modes for ultrasound sensor readings & general debug-text
// 1=on, 0=off
#define DEBUG_US 1
#define DEBUG_TEXT 0

// cycle for watchdog LED
int watchdogStatus = 0;
int watchdogCounter = 0;

// files on Micro-SD card:
// msg0004.mp3 (someone saying hello)

void setup() {
  Serial.println(setup() starting);
  myplayer.keyDisable(); // disable hw keys to enable I/O pins
  pinMode(US_TRIG, OUTPUT);
  pinMode(US_ECHO, INPUT);
  Serial.println(setup() complete);

void loop() {
  Serial.println(loop() starting, should never end);
  myplayer.setVolume(10); // 0=max, 10=pretty good

  if (1==DEBUG_TEXT) Serial.println(starting loop);
  int someoneThereCounter = 0;
  int pauseTime = 300; // ms - loop frequency
  int timeRequired = 900; // ms - time before it plays
  while(1) {  // this is really my loop
    if (1==DEBUG_TEXT) Serial.print(".");
    if (someoneAround()) {
      if (1==DEBUG_TEXT) {
        Serial.print(someone found! counter at );
        Serial.print( of );
      someoneThereCounter += pauseTime;
      if (someoneThereCounter >= timeRequired) {
        if (1==DEBUG_TEXT) Serial.println(still with us, play);
        myplayer.setPlayMode(MODE_NORMAL); // set mode to repeat to play a song
        myplayer.playSong(msg0004.mp3); // play a song named with this name
        if (1==DEBUG_TEXT) Serial.println(short wait);
        delay(8000); // pause
        if (1==DEBUG_TEXT) Serial.println(awaiting next);
        someoneThereCounter = 0;
    } else someoneThereCounter = 0;
  if (1==DEBUG_TEXT) Serial.println("loop() complete, something broke?");

boolean someoneAround() {
  // return true if someone around
  // check ultrasound sensor for nearby person
  int distance = ultrasoundDistance();
  if (distance<=0) return false; // no good measurement or off the scale high
  if (distance<70) return true; // close enough
  return false; // further away

int ultrasoundDistance() {
  int duration, distance;
  digitalWrite(US_TRIG, HIGH);
  delayMicroseconds(100); // 100us pulse
  digitalWrite(US_TRIG, LOW);
  duration = pulseIn(US_ECHO, HIGH, 10000); // max 10ms wait
  distance = (duration/2) / 29.1; // get distance in cm
  if (1==DEBUG_US) {
    Serial.print(Duration: );
    Serial.print(ms; );
    Serial.print(distance: );
  if (distance >= 900 || distance <= 0){
    if (1==DEBUG_US) Serial.println(Out of range);
    distance = -1;
  else {
    if (1==DEBUG_US) {
      Serial.println( cm);
  return distance;

int showImAround() {
  // flicker LED on/off with given frequency
  watchdogCounter += 1;
  if (watchdogCounter > WATCHDOG_LED_COUNT) {
    watchdogStatus = 1-watchdogStatus;
    watchdogCounter = 0;
    if (watchdogStatus==0) {
      if (1==DEBUG_TEXT) Serial.println(LED:ON);
      digitalWrite(WATCHDOG_LED_PIN, HIGH);
    } else {
      if (1==DEBUG_TEXT) Serial.println(LED:OFF);
      digitalWrite(WATCHDOG_LED_PIN, LOW);

Warning: This page is pretty old and was imported from another website. Take any content here with a grain of salt.

Comments / questions

There's currently no commenting functionality here. If you'd like to comment, please use Twitter and @me there. Thanks!

Tweet about this - and/or - search for latest comments / top comments

Related pages