Arduino with mp3 player electric noise

hello ppl
I have an arduino nano (the same problem with mega), connect to 7 push buttons, the buttons also connected to GND.
also MP3 player, YX5300, to pins 10-11, GND and 5V.

I have a code of sequence pin 3-9 in order, every right push sound 1 if wrong sound 2

I'm using a usb pc speakers.

when the arduino and the speakers are connected to a pc everything working fine but when I connecting the arduino and the speakers to a power supply (like a pc power supply) I'm gating electric noise and the sounds playing over and over again ("Incorrect input! Back to the beginning!" in the monitor), also if I'm connecting them to GND and 5V of a mega arduino connected to the power supply.

What could be causing the problem? what can I do to fix the problem?

I'm adding the code if maybe it's can be fix by editing...

thanks in advance

#include "SerialMP3Player.h"

SerialMP3Player mp3Player;
#define CORRECT_FOLDER 1
#define WRONG_FOLDER 2

const byte numInputs = 7;
const byte inputPins[numInputs] = { 3, 4, 5, 6, 7, 8, 9 };
const byte numSteps = 7;
const byte steps[numSteps] = { 0, 1, 2, 3, 4, 5, 6 };
const byte lockPin = A0;
const byte ledPin = 13;

bool lastInputState[] = { HIGH, HIGH, HIGH, HIGH, HIGH, HIGH, HIGH };
int currentStep = 0;
bool sequenceCorrect = false;

unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  for (int i = 0; i < numInputs; i++) {
    pinMode(inputPins[i], INPUT_PULLUP);
  }

  pinMode(lockPin, OUTPUT);
  digitalWrite(lockPin, HIGH);

  pinMode(ledPin, OUTPUT);

  mp3Player.begin(9600);

  Serial.begin(9600);
  Serial.println(F("Serial communication started"));
  mp3Player.stop();
}

void loop() {
  if ((millis() - lastDebounceTime) > debounceDelay) {
    for (int i = 0; i < numInputs; i++) {
      int currentInputState = digitalRead(inputPins[i]);
      if (currentInputState != lastInputState[i]) {
        lastDebounceTime = millis();
      }
      if (currentInputState == LOW && lastInputState[i] == HIGH) {
        if (steps[currentStep] == i) {
          currentStep++;
          Serial.print(F("Correct input! Onto step #"));
          Serial.println(currentStep);
          mp3Player.playF(01);  // Play the files in folder 01 once
          mp3Player.stop();


          //          mp3Player.stop();  // Stop any ongoing playback
          if (currentStep == numSteps) {
            sequenceCorrect = true;
            currentStep = 0;
          }
        } else {
          sequenceCorrect = false;
          currentStep = 0;
          Serial.println(F("Incorrect input! Back to the beginning!"));

          mp3Player.playF(02);  // Play the files in folder 02 once
          mp3Player.stop();     // Stop any ongoing playback
        }
      }
      lastInputState[i] = currentInputState;
    }
  }

  if (sequenceCorrect) {
    onSolve();
    sequenceCorrect = false;
  }
}

void onSolve() {
  Serial.println(F("Puzzle Solved!"));
  digitalWrite(ledPin, HIGH);  // Turn on the onboard LED for 3 seconds
  delay(1000);
  digitalWrite(ledPin, LOW);
  digitalWrite(lockPin, LOW);

  mp3Player.stop();  // Stop the MP3 player playback
}

Sounds (!) like a power supply capacity/noise issue .

Need a circuit diagram !

Also looks like you are configuring the Tx/Rx pins as inputs , not a good idea if the usb port is used

Power supply, speakers or grounding

If thats REALLY a computer psu they arent designed to work with a light load.

thank you, the problem was the psu , when I change it to a 5v power supply it's worked great

1 Like

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