Loud buzzing when sound not playing

This is probably a really elementary question but I have searched and cannot find an answer.
I am using a MKR ZERO board, and I am trying to play a sound file.
I have the simplest of circuit: a headphone socket connected to DAC0 and the GND terminals on the Arduino.
When I upload the code that follows, the sound file "10.wav" on the SD card plays, but when it has finished I get a loud buzz.
If I put a button into the circuit, and change the code so that the sound plays each time I push the button, The loud buzz sounds until the button is pushed; the sound plays and the buzz is supressed; then when the file finishes playing the buzz resumes.
Am I doing something amazingly stupid. I have only just started with this Arduino malarkey.

// LOAD LIBRARIES
#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>

// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = SDCARD_SS_PIN;

void setup()
{
// debug output at 115200 baud
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect.
}

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

// we'll use the initialization code from the utility libraries
// since we're just testing if the card is working!
if (!card.init(SPI_HALF_SPEED, chipSelect)) {
Serial.println("initialization failed. Things to check:");
Serial.println("* is a card inserted?");
Serial.println("* is your wiring correct?");
Serial.println("* did you change the chipSelect pin to match your shield or module?");
while (1);
} else {

// Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
if (!volume.init(card)) {
Serial.println("Could not find FAT16/FAT32 partition.\nMake sure you've formatted the card");
while (1);
}

SD.begin();
AudioZero.begin(88200);
}

}

void loop()
{
int count = 0;
// open wave file from sdcard
File myFile = SD.open("10.wav");
if (!myFile) {
// if the file didn't open, print an error and stop
Serial.println("error opening test.wav");
while (true);
}
Serial.print("Playing");
// until the file is not finished
AudioZero.play(myFile);
Serial.println("End of file. Thank you for listening!");
while (true) ;
}

Please read this:-
How to get the best out of this forum
Because your post is breaking the rules about posting code.

You were supposed to get redirected to this page as you started your post, did you just ignore it?

This is more of a hardware question than a software one. So please post a schematic ( not a fritzing crap thing but a hand drawn diagram ).

Note that the

"I have the simplest of circuit: a headphone socket connected to DAC0 and the GND terminals on the Arduino."

is a bad idea because of the very low current you can get from this pin without damaging it.

I am very sorry about breaking rules: I read the introduction, but obviously not with sufficient care! I posted code because I THOUGHT the guidelines said "Don't just post a snippet. Post the whole thing". I will read again. Mea culpa.

Quick look at the AudioZero sources shows it disables the output pin at the end of playing,
presumably allowing it to go high-impedance, hence the loud mains pick-up.

Try adding 10k to ground on the output pin so it can't float.

Thanks for the time you took to reply to this, Mark.
I'm sorry, my ignorance is so comprehensive that I am going to have to ask you to explain "Try adding 10k to ground on the output pin so it can't float."
Do you mean that I connect a 10,000 ohm resistor between the ground and the DAC/A0 pin?

Do you mean that I connect a 10,000 ohm resistor between the ground and the DAC/A0 pin?

Yes he did.

OK, sorry about my earlier mistakes. I took MarkT's advice and put a 10K resistor between the DAC0 and the ground, but this didn't seem to make a difference.
Mike: thanks for your warning about connecting a headphone directly to DAC0, but this was recommended in a beginner's example sketch for this board.
Before posting again I replaced all the wires on the breadboard with shorter ones in case the wires were picking up audio noise.
I also spent some time rooting around and commenting out lines of code to see if I could pinpoint the problem myself.
If I eliminate code that detects when the button is pressed, the audio file plays correctly once the sketch is uploaded and runs: there is no buzz either before or after the sound plays.
But the conditional code to detect button-push seems to be what's creating the buzz

[code]
/*
  19 November Works but loud buzzing when sound not playing

*/

// LOAD LIBRARIES
#include <SD.h>
#include <SPI.h>
#include <AudioZero.h>


// set up variables using the SD utility library functions:
Sd2Card card;
SdVolume volume;
SdFile root;

const int chipSelect = SDCARD_SS_PIN;
int buttonApin = 7; // FOR PUSH BUTTON

void setup()
{
  pinMode(buttonApin, INPUT_PULLUP);

  // debug output at 115200 baud
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect.
  }
  Serial.print("\nInitializing SD card...");

  // we'll use the initialization code from the utility libraries
  // since we're just testing if the card is working!
  if (!card.init(SPI_HALF_SPEED, chipSelect)) {
    Serial.println("initialization failed.");
    while (1);
  } else {

    // Now we will try to open the 'volume'/'partition' - it should be FAT16 or FAT32
    if (!volume.init(card)) {
      Serial.println("Could not find FAT16/FAT32 partition");
      while (1);
    }
    SD.begin();
    AudioZero.begin(88200);
  }

}
void loop()
{ if (digitalRead(buttonApin) == LOW)
  { Serial.println("\nButton pressed");
    int count = 0;
    // open wave file from sdcard
    File myFile = SD.open("10.wav");
    if (!myFile) // CONFIRM FILE OPENED
    { // if the file didn't open, print an error and stop
      Serial.println("\nError opening file");
      //  while (true);
    }
    Serial.print("Playing");
    // until the file is not finished
    AudioZero.play(myFile);
    AudioZero.end();
    Serial.println("\nFinished playing");
    // while (true) ;
  }
}

[/code]

richardjcplatt:
Mike: thanks for your warning about connecting a headphone directly to DAC0, but this was recommended in a beginner's example sketch for this board.

Well that example is at fault - you must never abuse a DAC like this, DACs produce a signal levels at fairly
high impedances, headphones need such signals amplified to a low impedance.

Anyway I'm wondering if the AudioZero library can be modified not to disable the DAC at all - perhaps
there's something else in the hardware controlling that pin when the DAC is disabled?

What are you using to amplify the output? What's its input impedance?
Do you have a 'scope? Can you record a snippet of the nasty buzz for us?

Hello Mark
Thanks very much for taking the time and trouble to deal with my elementary questions.

MarkT:
Well that example is at fault - you must never abuse a DAC like this, DACs produce a signal levels at fairly
high impedances, headphones need such signals amplified to a low impedance.

I'm afraid that -- until I looked it up in Wikipedia -- I didn't even know what impedance is. And you can probably also guess from that admission that I don't have a scope.
I have been using a pair of ear-buds without amplification to try and get this to work. But I have also been connecting the earphone socket to the AUX input of an old iPhone player (see attachment). I don't know what the input impedance of this is, but perhaps you can guess a typical value.
Just to put this into context, I am trying to make a device that plays a random sound file from the SD card when a coin is inserted.
I have actually solved the buzzing problem now. I read in this thread -- Arduino Forum -- that the begin() command should be in the loop, not in the setup. I tried this, so that the procedure was ... open random file ... begin()... play() ... end() ... close file. Buzz went away, but there is still a pop at the beginning and end of playing the file. I found some discussion of this issue in this GitHub thread: Pop/Crack sound at the end · Issue #4 · arduino-libraries/AudioZero · GitHub.
So how should I connect the DAC pin to the amplifier I have got? Or do I need another amplifier?
Thanks again for your generous help.

Amplifier model instructions.pdf (672 KB)

The impedance of an Amplifier input is normally quite high. You should be able to connect your DAC output, but the popping noise is caused by a sudden change in D.C. level from the DAC output as the sound finishes and stops driving the output. I would suggest that as soon as the sound is finished then write a mid level value to the DAC.

You might also want to use a 1uF series capacitor between the DAC output and your amplifier input.

Grumpy_Mike:
the popping noise is caused by a sudden change in D.C. level from the DAC output as the sound finishes and stops driving the output. I would suggest that as soon as the sound is finished then write a mid level value to the DAC.
You might also want to use a 1uF series capacitor between the DAC output and your amplifier input.

Hi Mike. Very many thanks for this helpful reply. I will try the capacitor -- I dont have one of this value, but have ordered.
" I would suggest that as soon as the sound is finished then write a mid level value to the DAC."
I am so clueless that I don't know how to do this. I can't reasonably expect you to spoon-feed me step-by-step instructions, but please could you perhaps point me at a simple project from which I can adapt the code?
I apologise for asking such elementary questions. I really value the input I have had on this forum, and I assure you that I have spent many hours trying to make my project work before posting questions!

I am so clueless that I don't know how to do this.

Simply do an analog write to the D/A of half the maximum range. This depends on how many bits you have set the resolution of the D/A to. So if you set it to 10 bits that is a maximum value of 1023 so the mid point of this 512.

It is just one instruction.