0

I am using the library SimpleSDAudio to play a song from my SD card to an op-amp later in the circuit. I would like to take a sample from the audio pin (possibly with a Analog read) to power some visualizations (VU, etc.), although I am getting some unexpected readings off the audio pin.

I tried to bias the audio using this post, but got the same results.

I'm not sure if I am headed in the wrong direction here, any help would be amazing.

#include <SimpleSDAudio.h>
int ReadPin = A0;
int AudioValue = 0;

void setup() {
   Serial.begin(9600);
   while (!Serial) {
   }

  Serial.print(F("\nInitializing SD card..."));  

  if (!SdPlay.init(SSDA_MODE_FULLRATE | SSDA_MODE_MONO | SSDA_MODE_AUTOWORKER)) {
    Serial.println(SdPlay.getLastError());
    while(1);
    } else {
    Serial.println(F("Wiring is correct and a card is present.")); 
  }

  Serial.print(F("Looking for THRILLER.WAV... "));
  if(!SdPlay.setFile("THRILLER.WAV")) {
      Serial.println(F(" not found on card! Error code: "));
      Serial.println(SdPlay.getLastError());
      while(1);
  } else {
     Serial.println(F("found.")); 
  }    

  Serial.print(F("Playing... ")); 
  SdPlay.play();

}

  void loop() {
    AudioValue = analogRead(ReadPin);
    Serial.println(AudioValue);
    delay(50);

}

Output of analogRead

My expected output of sampling the output from A0: enter image description here

  • what does this mean? some unexpected readings – jsotola Sep 8 at 18:25
  • I would expect to see something similar to a wave form. Like I would see in Audacity. As the audio plays, I would see different values based upon the level of the output. Sorry if it's confusing. – Linuxx Sep 8 at 18:27
  • it is not confusing ... it is just pointless to say that something is unexpected, without an explanation, because nobody else knows what you are expecting – jsotola Sep 8 at 19:24
  • @jsotola I have added an "expected" image. Thanks. – Linuxx Sep 8 at 21:00
0

You are sampling your audio signal with only 20Hz. That is very low frequency for an audio signal, that humans can hear. Generally you need to sample your signal with double of the highest containing frequency at minimum, or you will only get nonsense (you can google Nyquist Rate for more information). Also a Nano does not have a real DAC, so the audio is generated via a PWM signal, which turn the line on and off very fast, so you only see the waveform only, if you apply a low pass filter (like a speaker will do, because it cannot move that fast).

But directly sampling the audio signal is overly complex, only for making a VU meter visualization. These visualizations are way slower, than the actual audio signal. So you need to take the mean value over a specific time interval. Best you can reach this by building a low pass filter using a resistor and a capacitor. You can sample the output of that filter, which will only have lower frequencies. The sampling frequency then should be double of the cut of frequency of the filter at minimum.

Of course it is also possible to do all this in software, but for this you would have to change the library and I guess you don't want to do this.

  • The delay was a leftover from me troubleshooting the code. When I leave the delay out, I get similar results. I have code to take the sample, and produce a VU, I didn't include as I didn't think I was getting something that I could use from the raw audio pin. – Linuxx Sep 8 at 19:24
  • Still the raw audio pin will only have a PWM signal. You should try the low pass filter and sample that filtered signal. – chrisl Sep 8 at 20:41
  • If you didn't think, that you would get something useful from the audio pin, why did you sample it? – chrisl Sep 8 at 20:42
  • Sorry I was replying from mobile and was trying to be brief. I have edited my question to include a graph more like I was expecting. If I received a waveform from the audio pin like pictured above, I would have then ran the code through my "VU" function. This "expected" graph was produced with a condenser microphone connected with the same code, sampling at 20Hz. This condenser circuit has a op-amp that drives the output. Perhaps the output of the PWM audio signal isn't enough to drive the ADC and produce the waveform I thought I would see? Thanks @chrisl for your help! – Linuxx Sep 8 at 20:59
  • You didn't show a VU function, so I cannot know, if this function would help. So you get a more expected output, if you take the sound with a microphone and sample that? As I tried to explain, there is already a (mechanical) low pass filter incorporated, since the speakers membrane cannot move fast enough to follow the PWM signal. Instead it will take the mean. That is the whole point in doing sound via PWM. If you don't want to use a mecrophone to sample the real sound, but want to use the direct electrical signal, you need an electrical low pass filter, to filter out the PWM frequency – chrisl Sep 9 at 8:09

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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