Need help troubleshooting project with Ultrasonic motion sensor, DFPLayer and servo

My project is an Arduino Mini, An Ultrasonic Motion Sensor, a small servo and a DIY DFPlayer with 3.5 mm jack. The wiring diagram is outlined below as is the code but every time I power it up I get the same error code:

21:02:53.464 -> Initializing DFPlayer...
21:02:55.666 -> Unable to begin DFPlayer Mini.

Wiring DIagram:

DFPLayer
TX- D11
Rx – 330 Ohm resistor – D12
VCC- 5V arduino
GND – Arduino GND

Ultrasonic sensor
VCC – 5V Arduino
Trig -D9
Echo – D10
GND – Arduino GND

Servo
Orange – D6
Brown – Arduino GND
Red – Arduino 5V

Code:

#include <SoftwareSerial.h>
#include <DFRobotDFPlayerMini.h>
#include <Servo.h>

#define TRIG_PIN 7
#define ECHO_PIN 6
#define SERVO_PIN 9

SoftwareSerial dfSerial(11, 12); // RX, TX for DFPlayer
DFRobotDFPlayerMini myDFPlayer;
Servo myServo;

bool triggered = false;
unsigned long triggerTime = 0;

void setup() {
  Serial.begin(9600);
  dfSerial.begin(9600);
  
  pinMode(TRIG_PIN, OUTPUT);
  pinMode(ECHO_PIN, INPUT);
  
  myServo.attach(SERVO_PIN);
  myServo.write(0); // Start position

  Serial.println("Initializing DFPlayer...");
  if (!myDFPlayer.begin(dfSerial)) {
    Serial.println("Unable to begin DFPlayer Mini.");
    while (true);
  }

  Serial.println("DFPlayer Mini ready.");
  myDFPlayer.volume(25); // Set volume (0–30)
}

void loop() {
  long duration, distance;

  digitalWrite(TRIG_PIN, LOW);
  delayMicroseconds(2);
  digitalWrite(TRIG_PIN, HIGH);
  delayMicroseconds(10);
  digitalWrite(TRIG_PIN, LOW);
  
  duration = pulseIn(ECHO_PIN, HIGH, 30000); // Timeout at 30ms
  distance = duration * 0.034 / 2;

  Serial.print("Distance: ");
  Serial.print(distance);
  Serial.println(" cm");

  // Trigger if within 152cm (≈5 feet) and not already triggered
  if (distance > 0 && distance < 152 && !triggered) {
    Serial.println("Target detected! Triggering sequence...");
    myDFPlayer.play(1); // Play 0001.mp3
    Serial.println("Audio playback started.");
    triggered = true;
    triggerTime = millis();
  }

  // After 26 seconds, move servo
  if (triggered && millis() - triggerTime >= 26000) {
    myServo.write(90);
    Serial.println("Servo activated.");
    delay(2000); // optional hold time
    myServo.write(0);
    triggered = false;
    Serial.println("System reset.");
  }

  delay(300);
}

The connections mentioned above do not agree with the pins defined in the code:

#define TRIG_PIN 7
#define ECHO_PIN 6
#define SERVO_PIN 9

I think you mean distance sensor, perhaps HC-SR04?

You should never attempt to power a motor or servo from the Arduino 5V pin. That will cause the Arduino to malfunction, and can even damage it permanently.

Use a separate power supply (4xAA battery pack is OK for a small servo), and be sure to connect the grounds.

1 Like

I don't see a wiring diagram.

1 Like

How is the Mini powered?

A DFPlayer with 4 Ohm speaker can also draw significant current on higher volume.
Leo..

Your code works like this... if an object has a distance of 152cm or less, these actions...

Distance: 152 cm
Target detected! Triggering sequence...
Audio playback started.

... and if the object happens to move farther than 152cm from the HC-SR04 for 26 seconds, the actions will be re-triggered.

Servo activated.
System reset.

So... "it works."

Please show your schematic, at least a simple drawing. Or a table showing all your connections. Or a high resolution photo showing all connections. We need to eliminate a wiring error before looking further.

Thanks for all the responses. Yes I know I should power the servo from a separate power source but this is a small servo and it will be activated infrequently so I was just prototyping it with a single power source at the moment. Ill use a separate source in the final project

I tried to make a schematic of the wiring but I only have access to Tinkercad and the don't have an MP3 player in the parts choices. I took some pictures. Obviously harder to see the wiring this way so I apologize but if this help and thanks in advance for taking a look.





There is no reason to expect the prototype to ever work.

@macgyver60 - do you have sketches each for the all the various things you are trying to combine?

Use such sketches to check that each part is working, and works the way you think.

When all the parts work, the question is easier: why does combining things break it?

You will have eliminated wire errors, power supply issues as well as code problems that each may have.

a7

The Catalex-type MP3 player (pics in No.9) is not a DFPlayer.
It will not work with the DFPlayer library.

GitHub - MajicDesigns/MD_YX5300: Library for Serial MP3 Player based on YX5300 IC (Catalex Module)

1 Like

But your image shows some other board. Perhaps Nano?
There is likely diode between USB-supply and 5V pin and it might not be rated for currents that motor/speaker can draw.

Draw the wiring by hand of the ACTUAL connections, then take a picture of the diagram and post it.
But the first step is to reduce the number of components until you have the smallest set that produces the problem. Ideally that will be one! Now draw the diagram etc and post all the code in code tags and error log if any.

1 Like

That's clearly labeled 'Nano', not a 'Mini' that you said.

What made you think this was a DFR MP3 player?

Try the library from this thead.

And it appears to be a clone.

Yes sorry, That was a mistype. Its an arduino type nano

See post #12

Thanks. Yeah, I noticed that an maybe thats the problem. I had to go out of town but when I get back tomorrow I am going to work on that. Thanks for the input.

It's a problem