Pir sensor activate a sound on SD card

Hi!
I'm new in Arduino but I had some experience repairing some circuits on old stuff

I did some project from the arduino starter kit and now I would like to create a project by myself:
activate a sound from sd card with pir sensor as trigger.

I did a circuit using an LM386 amplifier circuit with gain and volume
added the pir sensor and sd card
the audio is in wav 8 bit

my code is the one below:

#include <SD.h>

const int SD_CS = 4;
const int LED_PIN = 13;
const int INPUT_PIN = 2;
const int AUDIO_PIN = A0;

int pirState = LOW;
int val = 0;

void setup() {
  pinMode(LED_PIN, OUTPUT);
  pinMode(INPUT_PIN, INPUT);
  pinMode(AUDIO_PIN, OUTPUT);

  Serial.begin(9600);

  if (!SD.begin(SD_CS)) {
    Serial.println("Errore nell'accesso alla scheda SD");
    return;
  }
}

void loop() {
  val = digitalRead(INPUT_PIN);
  if (val == HIGH) {
    digitalWrite(LED_PIN, HIGH);
    if (pirState == LOW) {
      Serial.println("Motion detected!");
      File audioFile = SD.open("audio.wav");
      Serial.println("Audio in play");
      if (audioFile) {
        while (audioFile.available()) {
          int sample = audioFile.read();
          analogWrite(AUDIO_PIN, sample);
        }
        audioFile.close();
      } else {
        Serial.println("File audio non trovato");
      }
      pirState = HIGH;
    }
  } else {
    digitalWrite(LED_PIN, LOW);
    if (pirState == HIGH) {
      Serial.println("Motion ended!");
      analogWrite(AUDIO_PIN, 0);
      pirState = LOW;
    }
  }
}

it works but the sound is bad, with many distorsion.

if I try the amplifier with audio in from my phone is perfect
but from SD card on A0 pin is really bad

any suggestion?

Your topic has been moved to a more suitable location on the forum. Installation and Troubleshooting is not for problem with (nor for advice on) your project.

Maybe the data rate (bits per second) of the audio from your phone is higher than that of the SD card file.

I don't know
but the amplifier by itself is good enough to listena a song

the music from SD .wav 8bit 16000hz is really bad and with distortion

I've reand on the forum that the problem is Arduino , maybe?

Now I just changed the A0 input to input 8 and the sound is a little, little, little less distort....but still no good

Does your SD audio sound any different from the SD audio of other projects found on YouTube? To me, they all sound "quakky"

sorry but I do not understand @xfpd

I would expect audio from the Arduino to be no different than this (start at 1:46):

no my audio is not like the example on your video
my audio has distorsion

I tried to export different .wav with different options and I had more distortion then the file I'm using

the file that sound better has been download from a project of an user on the web and it was supposed to be in the correct export options..

i attach a video
sorry for the sound is an alarm
you can hear a buzz everytime....

WhatsApp Video 2023-02-06 at 19.03.48.zip (2,9 MB)

Okay. Maybe the audio was recorded poorly (but perfectly reproducing the poor quality). Also, is your volume set too high, and clipping your audio? The datasheet and library show a setting for "equalizer" (normal, pop, rock, classical) that might be out of range for the speaker... and... did you try another speaker, or inject the signal from the dfplay into an audio receiver (AUX IN)?

can you send me an audio to test the quality? @xfpd

i tried with low volume and high but the volume works as it should...
I also tried with a different speaker, one 8ohm and another 16ohm
I only increase volume if I use the 16ohm

about audio injection I need to understand how to make this test..!

This only means "play your dfplayer through an audio amplifier" of any audio equipment (radio, stereo, amplifier, other) by using the Auxiliary Input (AUX IN) usually on the back and having an RCA (+/-, tip/ring) connector.

Any audio file that you have (within reason), that plays well on your computer, should play well on the dfplayer.

I've also been reading "on the internet" that some dfplayerminis just do not reproduce quality sound, but another dfplayermini in the same batch work just fine.

AND... the circuit drawing for the dfplayermini usually shows a voltage divider (level shifter) from the output pin of the Arduino to the RX pin of the dfplayermini. Does your dfplayermini have this?

ok I understand
I attach the sd card reader I'm using



You should not fiddle with pin 13 when using a SPI device like your card reader on an Uno.

I don't say it's the cause of your problem (I simply don't know for sure) but pin 13 is part of the SPI interface of boards like Uno and Nano.

Try without controlling the LED and see if it improves.

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