1
\$\begingroup\$

I am using an Arduino Nano in combination with a SD card to play some music. I have loaded some .wav files on my SD card, the card initializes, everything runs correctly, but on my output pin for my music I get an unstable square wave signal all the time.

I have tried to change the CS pin to pin 4 and 10 (only pin 4 works), I have tried different .wav files and a bunch of code and all of my libraries are installed. What can I do?

enter image description here

#include <SPI.h>
#include <SD.h>
#include <TMRpcm.h>

TMRpcm audio;

void setup() {
  audio.speakerPin = 9;

  //starting up SD
  Serial.begin(9600);
  if (!SD.begin(4)) {
    Serial.println("Initialization failed");
    while (true) {
    }
  }
  Serial.println("Initialization complete");

  audio.setVolume(3);
  audio.play("SSTC.wav");
}

void loop() {
}
\$\endgroup\$
5
  • \$\begingroup\$ Is the output meant to be a PWM modulated square wave, or is does the arduino have a DAC? \$\endgroup\$
    – HandyHowie
    Commented Jan 22, 2023 at 8:56
  • \$\begingroup\$ It is meant to be PWM. But the signal I get out is no music whatsoever. Just weird signals. \$\endgroup\$ Commented Jan 22, 2023 at 9:42
  • \$\begingroup\$ One more thing, If I give it different songs, It gives me different types of buzzes.....so....something is working... \$\endgroup\$ Commented Jan 22, 2023 at 10:35
  • \$\begingroup\$ What bandwidth can TMRpcm handle? I doubt it has remotely the bandwidth for ordinary audio WAV files, especially on an Arduino Nano. \$\endgroup\$
    – Neil_UK
    Commented Jan 22, 2023 at 11:20
  • \$\begingroup\$ Can you open and play the file in audio player/editor? What format it is? Can the Arduino library play that format? Have you tried to play some simpler test files, like a -3dB 440 Hz sine wave stored as 8-bit unsigned PCM at 8 kHz? \$\endgroup\$
    – Justme
    Commented Jan 22, 2023 at 11:46

1 Answer 1

4
\$\begingroup\$

It is (probably) working as intended. From the TMRpcm library's Wiki:

If using an amplifier, consider that the output is a choppy digital PWM signal, not a smooth analog signal.

This could be what you are seeing: the output is PWM, not analog. The PWM signal you get is supposed to be low-pass filtered by either a filter or the characteristics of the speaker you use.

If you want analog output, you need to pass the PWM through a low-pass filter, or use a microcontroller with a DAC, or use an external DAC (and a different library for the last two options).

Another possible problem is the performance of the Arduino Nano with .wav files with a high sample rate and bit depth; the library's Wiki says:

Main formats: WAV files, 8-bit, 8-32khz Sample Rate, mono.

It could be your .wav file doesn't comply with that.

\$\endgroup\$
6
  • \$\begingroup\$ Hello, thank you for your response! There is only one problem though. The .wav file I gave it has PWM music on it (the original PWM signal vs the output Arduino PWM signal are very different). But if I amplify the signal coming out of the Arduino, it resembles NOTHING of the original. Just some weird buzzes. \$\endgroup\$ Commented Jan 22, 2023 at 9:33
  • \$\begingroup\$ @GeorgeChita: I don't know if a .wav file can contain PWM data, but it is at least very unusual; most .wav files contain LPCM data. Are you sure your .wav file contains PWM data? \$\endgroup\$
    – ocrdu
    Commented Jan 22, 2023 at 11:16
  • \$\begingroup\$ Yes, I am sure, it is PWM, though not with constant amplitude. \$\endgroup\$ Commented Jan 22, 2023 at 11:38
  • \$\begingroup\$ Thank you very much. The file wasn't compatible. It finally worked. Thank you again! \$\endgroup\$ Commented Jan 22, 2023 at 12:47
  • \$\begingroup\$ @GeorgeChita: you may want to accept the answer (if you accept the answer), so the question doesn't stay open. \$\endgroup\$
    – ocrdu
    Commented Jan 22, 2023 at 12:48

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.