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
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.
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?