HC-SR04 sensor triggering sound through DFPlayer – circuit issues

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!

Think about:
Arduino UNO is a power supply?
No.
Use a external power suplly to HC-SR and sound modules.
Don't forget, connect all GND together.

Thanks for your answer. Would something like this work?

That is a Yes/No answer. 5 Volts should be fine but the Arduino cannot supply the needed current, causing it to fold back (lower voltage to control current). As suggested by @ruilviana use an external power supply for the Audio portion.

Thanks for the input! I get that I need an external power supply, but I'm still a bit uncertain about the wiring, and spesifically how to make sure that everything has a common ground.

Here's an example using both power rails on the breadboard, with a breadboard power supply:

The Arduino is connected to the minus rail on both sides.

Here's an example using a 9V battery in one of the rails:

Would both of these work? Is one solution better than the other?

Caution. Vin does not have reverse polarity protection.

Spend some time and go through the Arduino Cookbook, it probably has your project in it. Sorry I do not follow fritzing very well. As in your case it is not labeled very well and I do not have the parts you used as a reference.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.