Hi!
I'm trying to trigger an MP3 file through a DFPlayer based on the distance measured by a HC-SR04 sensor. I can get them both to work individually, but when I try to rewire so that their both connected to the Arduino Uno I run into problems. I'm guessing the wiring / circuit is the issue.
When I connect the cables to 5V and GND the Arduino seems to shut down. When I try to upload code I get the error: "stk500 programmer is not responding". No errors when verifying the code. When I disconnect the 5V and GND the Arduino works, and I can upload code to it (tested the Examples > Basic > Blink code).
Here's my current wiring:
The Arduino Uno is so far just powered through my laptop. Is 5V sufficient for this? I've also considered using a breadboard power supply, and power the Ardunio through that, but this is pretty new to me. Here's my sketch, but I haven't tried it out yet:
And here's my code, that I haven't really tried out yet, because I can't upload it. For the DFPlayer I've used the same setup as this tutorial.
#include "./lib/mp3tf16p.h"
#define led 13
// MP3 Player
MP3Player mp3(10, 11);
const int trigPin = 6;
const int echoPin = 2;
long duration;
int distance;
bool isPlaying = false; // Flag to track playback state
void setup(void)
{
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.begin(9600);
pinMode(led, OUTPUT);
mp3.initialize();
Serial.println("MP3 Player initialized.");
}
void loop(void)
{
mp3.serialPrintStatus(MP3_ALL_MESSAGE);
delay(100);
// Trigger the HC-SR04 sensor
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
// Measure the duration of the echo pulse
duration = pulseIn(echoPin, HIGH);
// Convert duration to distance in cm
distance = duration * 0.034 / 2;
Serial.print("Distance: ");
Serial.println(distance);
delay(500); // Adjust delay if needed
// If the distance is less than 10 cm and not already playing, start playing
if (distance < 10 && !isPlaying) {
Serial.println("Object detected! Playing sound...");
mp3.playTrackNumber(1, 30, false);
isPlaying = true; // Set flag to prevent repeated playback
}
// If the distance is greater than or equal to 10 cm and was playing, stop playback
else if (distance >= 10 && isPlaying) {
Serial.println("Object moved away. Stopping playback.");
mp3.stop();
isPlaying = false; // Reset flag
}
}
Any input is very much appreciated!




