Stefan Vasic
Published © GPL3+

Arduino MP3 Nodding Toy

Stuffed toy with MP3 module, speaker, servo and piezoelectric sensor that nods to music.

BeginnerFull instructions provided4,781
Arduino MP3 Nodding Toy

Things used in this project

Hardware components

Arduino UNO
Arduino UNO
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
DFRobot DFPlayer Mini MP3 Player For Arduino
×1
Piezoelectric
×1
Flash Memory Card, MicroSD Card
Flash Memory Card, MicroSD Card
×1
Jumper wires (generic)
Jumper wires (generic)
×1

Software apps and online services

Arduino IDE
Arduino IDE

Story

Read more

Schematics

ArduinoMP3ToyFritzing

ArduinoMP3ToyFritzing image

Code

ArduinoMP3Toy.ino

Arduino
#include "DFPlayer.h"
#include <Servo.h>

// DFPlayer 
#define DFPserial_RX 5  // DFP RX pin 2 connect to Arduino TX pin 5
#define DFPserial_TX 4  // DFP TX pin 3 connect to Arduino RX pin 4
#define BUSYPIN 2
#define VOLUME_INITIAL 20
// Servo 
#define SERVO_PIN 6
#define SERVO_INITIAL 70
#define SERVO_NODD 55

//Piezoelectric
#define PIEZO_PIN A0
#define THRESHOLD 70

Servo servo1;
DFPlayer mp3;
bool playing = false;

void setup() {
  Serial.begin(115200);
  
  servo1.attach(SERVO_PIN);
  
  // Set initial position of servo
  servo1.write(SERVO_INITIAL);


  // Start MP3 module
  mp3.begin();  
  mp3.getPlayerStatus();
  mp3.setVolume(VOLUME_INITIAL);
}

void loop() {

  //Read piezoelectric
  int val= analogRead(PIEZO_PIN);
  delay(100);

  // Check that the piezo is pressed
  if (val >= THRESHOLD)
  {
    // If it's not playing and piezo is pressed, then start the song.
    if(!playing)
    {
        mp3.playMP3Folder(1);       // Play track 1 in the mp3 folder
    } 
    else               
    {
       // If it's playing and piezo is pressed, then stop the song.
        mp3.stopAll();
        
        // Set servo to initial position
        servo1.write(SERVO_INITIAL);
    }
  }

  // Use hardward to detect if playing using the DFPlayer Busy pin connected to pin 2 on the Arduino 
  if (mp3.playing(BUSYPIN)) { 

    //Start nodding
    nodd();
    
    if (!playing) {
      playing = true;
    }
    
  } 
  else 
  {
    if (playing) 
    {
      playing = false;
    }
  }
  delay(10);
}

void nodd()
{
    servo1.write(SERVO_NODD);
    delay(300);
    servo1.write(SERVO_INITIAL);
}

Credits

Stefan Vasic
5 projects • 17 followers
Full Stack Developer. Big mobile development and Arduino enthusiast.
Contact

Comments

Please log in or sign up to comment.