PROTO D21G audio playback

using the DAC output to play sound

Tom

The Industruino D21G MCU runs at 48MHz and has a DAC output, which enables it to create a sound output. Arduino provides this example of a simple audio player, with the AudioZero library available in the IDE library manager.

A short video of the demo project is shown below:

 
 

A0 vs DAC0

On the Arduino boards, the DAC is on pin A0 but this pin is not defined in the Industruino D21G, which uses DAC0 as pin name for this output. The AudioZero library uses the A0 name, so we have to add a line to the AudioZero.h library file:

  #define A0 DAC0  // added for Industruino D21G

This line makes sure the library uses DAC0 instead of A0.

The above tutorial uses an LM386 as audio amplifier, but i used a simple cheap class D stereo amp (PAM8403) that does not even require a DC filtering capacitor; the DAC output can be directly connected to one of the amp's input channels. It can be powered from the PROTO's 5V and GND terminals, so only 3 wires are required.

SD card

We use the SD card slot in the Industruino Ethernet module. As we're not using the Ethernet functionality, we need to make sure to pull the SPI chip select lines of the Ethernet and FRAM high to avoid interference.

The sound file needs to be of a certain format: 8-bit unsigned PCM mono. I generated a file that with this online converter.

Below the code as modified from the above library example. The original did not have the AudioZero.end() so there was a lot of noise after the file stopped playing.

/*
  Simple Audio Player for Arduino Zero
  Demonstrates the use of the Audio library for the Arduino Zero

  Hardware required :
   Arduino shield with a SD card on CS4
   A sound file named "test.wav" in the root directory of the SD card
   An audio amplifier to connect to the DAC0 and ground
   A speaker to connect to the audio amplifier

  Arturo Guadalupi <a.guadalupi@arduino.cc>
  Angelo Scialabba <a.scialabba@arduino.cc>
  Claudio Indellicati <c.indellicati@arduino.cc>

  This example code is in the public domain

  http://arduino.cc/en/Tutorial/SimpleAudioPlayerZero

  online .wav converter: https://audio.online-convert.com/convert-to-wav

  Modified for INDUSTRUINO D21G by Tom Tobback
  Nov 2017

  NOTE: pin A0 is not defined for Industruino, the DAC output is on pin DAC0
  add this line to the library file AudioZero.h:
  #define A0 DAC0  // added for Industruino D21G
*/

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

#include <UC1701.h>      // Industruino LCD
static UC1701 lcd;

void setup()
{
  pinMode(10, OUTPUT); // Ethernet CS             // needed for Industruino Ethernet module
  pinMode(6, OUTPUT);  // FRAM CS
  pinMode(4, OUTPUT);  // SD card CS
  digitalWrite(10, HIGH);
  digitalWrite(6, HIGH);
  digitalWrite(4, HIGH);

  pinMode(26, OUTPUT);      // Industruino backligh on
  digitalWrite(26, HIGH);

  lcd.begin();
  lcd.setCursor(2, 0);
  lcd.print("INDUSTRUINO D21G");
  lcd.setCursor(2, 1);
  lcd.print("AudioZero demo");

  // debug output at 115200 baud
  SerialUSB.begin(115200);
  //  while (!SerialUSB);  // useful for debugging

  // setup SD-card
  SerialUSB.print("Initializing SD card...");
  lcd.setCursor(2, 3);
  lcd.print("init SD card...");
  if (!SD.begin(4)) {
    SerialUSB.println(" failed!");
    lcd.print("failed");
    while (true);
  }
  SerialUSB.println(" done.");
  lcd.print("done");

}

void loop()
{
  // 44100kHz stereo => 88200 sample rate
  AudioZero.begin(1 * 44100);                // online .wav generator did not have 88200Hz, only 44100Hz

  // open wave file from sdcard
  File myFile = SD.open("T2.wav");
  if (!myFile) {
    // if the file didn't open, print an error and stop
    SerialUSB.println("error opening .wav file");
    lcd.setCursor(2, 4);
    lcd.print("file not found");
    while (true);
  }

  SerialUSB.println("Playing");
  lcd.setCursor(2, 4);
  lcd.print("playing .wav file");

  // until the file is not finished
  AudioZero.play(myFile);

  SerialUSB.println("End of file. Thank you for listening!");
  lcd.setCursor(2, 5);
  lcd.print("finished playing file");

  AudioZero.end();   // otherwise noise on DAC output

  lcd.setCursor(2, 6);
  lcd.print("press Enter to play");

  while (digitalRead(24));   // wait for Enter button pressed

  lcd.setCursor(2, 4);
  lcd.print("                         ");
  lcd.setCursor(2, 5);
  lcd.print("                         ");
  lcd.setCursor(2, 6);
  lcd.print("                         ");
}