Help with Coding for Arduino and MP3 trigger with concurrent servo activity

Hi All,
I just want to start by saying I am a newbie to programming so trying to figure things out.

Here is what I am trying to do:

I am trying to set it up so the arduino uno triggers random sounds from the MP3 trigger. While the MP3 trigger is playing the sounds I want the servos to be moving. This is an animatronic head that I want to bobble with the sound. I think there is a way that the arduino can monitor the sound file from the mp3 trigger and action the servos. It would be one servo controlling the head vertical axis of rotation and 2 servos that control flexion and extension synchronously. The delay time between sounds would vary.

I have googled around and searched through this forum but haven't found exactly what I need and some of the code is pretty complex for me :slight_smile:

Has someone done this task and have a link to their code for it? Or is there a good tutorial etc that may help? My event is next weekend so I have a time crunch and a steep learning curve! I am making a C3PO backpack for a chewbacca costume I have and want him to say random phrases while moving his head and cursing at the wookiee :wink:


Ok so here is the code I have FYI. I found a few different sites with code that should work once I have it modified and trying to mesh it together.
I attached it as can’t figure out how to put the code in a text box :slight_smile:

C3PO_head_and_mp3_sketch.ino (17.6 KB)

Seems like a pretty big project for someone who has to hunt for preexisting code.

You put code inside these little guys you see above the smiley faces


Hi Paul,
Yes as I am researching it isn't quite as straight forward as I was hoping. Although there are a few projects on this forum that are close.

I am certainly happy to hear any advice on how I could simplify too.

I have coded really basic servo movement and have used library's a bit and understand the basics of the setup and the loop sequence but as you mentioned there is a few moving parts to this project.

I was really reaching out to see if someone has done a similar project that I could reference or even advice on tutorials to check out that may help my direction. I can happily change the servo movement parameters and the timing between random audio triggers but I am certainly more of a builder than a coder!

Thanks again for any help.


Seems like a pretty big project for someone who has to hunt for preexisting code.

You put code inside these little guys you see above the smiley faces


Hi Again,
I am starting tis project from scratch and teaching myself to program as I go. Intially I want to use the Uno to communicate with the sparkfun MP3 trigger. I am trying to figure out the seerial communication between these 2 devices.
So I would like to have the Uno generate a random number based on the number of tracks I have saved on the MP3 triggers Sd card. Then Trigger the Uno to play that random file.
I have the files saved on the SD card in this format:
001 XXXX (file name)
002 XXXX

My pin wiring is as follows:

Uno------MP3 Trigger

After the Uno generates a random number and triggers that particular file I am trying to get the uno to listen for when the track finishes. It appears that it Generates an ascii character of 254 at the end of playing which corelates to the black square.

Not sure what I am doing wrong as my uno won’t initiate the sound on the MP3 trigger and the return serial is no the expected value of “X” or 88.
I am using this MP3 trigger library:

#include <MP3Trigger.h>

MP3Trigger trigger;
int pin0;
int pin1;
long randNumber;

void setup()  {

 pinMode(pin0, INPUT);
 pinMode(pin1, OUTPUT);

  Serial.begin(9600); // enable serial communication
  Serial.println("Random sound generation from mp3 trigger - serial read");

   // if analog input pin 0 is unconnected, random analog
  // noise will cause the call to randomSeed() to generate
  // different seed numbers each time the sketch runs.
  // randomSeed() will then shuffle the random function.


void loop() {
  // if there is serial avaialbe reade it:
 while (Serial.available() > 0) {

// print a random number from 1 to 10
  randNumber = random(1, 10);

  //looks for character in incoming serial connection from MP3 Trigger
 int pin0 =;

 if (pin0=254) Serial.println("yes");