0

Arduino + MAX9744 + SD card speaker project
Moderators: adafruit_support_bill, adafruit

Please be positive and constructive with your questions and comments.

Arduino + MAX9744 + SD card speaker project

by Risen91 on Fri Jun 18, 2021 11:52 am

Hey Adafruit community! I'm pretty new to the whole Arduino stuff and wanted to start off with a little project that I thought would be fun, but I'm running into some confusion that I can't seem to find much information on so I was wondering if you beautiful people could point me in the right direction! :D

My goal of the project is to get my Arduino to play a sound file through some speakers stored on a micro SD card. I will try to give as much detailed information as possible to help!

My issue is that I'm not so sure that I have everything set up correctly to play the sound file from the micro SD card, through the amplifier and to the speakers. I used the code from the MAX9744's digital control page https://learn.adafruit.com/adafruit-20w-stereo-audio-amplifier-class-d-max9744/digital-control and also added code to read/play from the SD (code shown below). Once I had everything set up (hardware/code) I tried uploading and running the code yet I'm not getting any sound playing through the speakers (regardless of volume). I'm a little nervous that I could damage something if I wire things up wrong and would rather get advice on here than try to fix/brute force a result myself.

I would be grateful if someone could help guide me on the code or hardware setup to get this correctly working and playing the sound files through the speakers :)

The tools I'm using are:
• Arduino Uno rev3
• 2x 4Ω speakers - Cambridge SoundWorks (not too sure about the wattage, but there is an image of the speakers below also)
• MAX9744 - https://learn.adafruit.com/adafruit-20w-stereo-audio-amplifier-class-d-max9744
• Micro SD module https://www.amazon.de/gp/product/B06XHJTGGC/ref=ppx_yo_dt_b_asin_title_o03_s00?ie=UTF8&psc=1
• Micro SD card

I previously did a test where I connected the SD card up to the Arduino and attached a speaker and ran this code (just below) which worked fine however the sound was extremely quiet so I decided to get the MAX9744 amplifier.

Code: Select all | TOGGLE FULL SIZE
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm audio;
char fileName[] = "2.wav";

void setup(){
audio.speakerPin = 9;
Serial.begin(9600);
if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}

audio.setVolume(5);
audio.play(fileName);
}

void loop(){  }


After wiring up the amplifier to match the Adafruit digital control page on https://learn.adafruit.com/adafruit-20w-stereo-audio-amplifier-class-d-max9744/digital-control I edited the code there to also include the code above to get the data from the micro SD which left me with the code below (I hope no one is cringing too much at my attempt to understand and mash the code together here).

Code: Select all | TOGGLE FULL SIZE
#include <Wire.h>
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm audio;
char fileName[] = "2.wav";

// 0x4B is the default i2c address
#define MAX9744_I2CADDR 0x4B

// We'll track the volume level in this variable.
int8_t thevol = 5;

void setup() {
  Serial.begin(9600);
  Serial.println("MAX9744 demo");
  Wire.begin();

  if (!SD.begin(SD_ChipSelectPin)) {
Serial.println("SD fail");
return;
}
 
  if (! setvolume(thevol)) {
    Serial.println("Failed to set volume, MAX9744 not found!");
    while (1);
  }
}

// Setting the volume is very simple! Just write the 6-bit
// volume to the i2c bus. That's it!
boolean setvolume(int8_t v) {
  // cant be higher than 63 or lower than 0
  if (v > 63) v = 63;
  if (v < 0) v = 0;
 
  Serial.print("Setting volume to ");
  Serial.println(v);
  Wire.beginTransmission(MAX9744_I2CADDR);
  Wire.write(v);
  if (Wire.endTransmission() == 0)
    return true;
  else
    return false;

    audio.play(fileName);
}

// Read in + and - characters to set the volume.
void loop() {
  if (! Serial.available()) return;
 
  // read a character from serial console
  char c = Serial.read();
 
  // increase
  if (c == '+') {
    thevol++;
  }
  // decrease
  else if (c == '-') {
    thevol--;
  }
  // ignore anything else
  else
    return;
   
  if (thevol > 63) thevol = 63;
  if (thevol < 0) thevol = 0;

  setvolume(thevol);
}


This is the board layout I have (hope it's ok with them split? I thought it would give more clarity on the connections):
Image

I also have some actual images of the setup to help understand my setup better:

Image
Image
Image

Also images of the speakers (lighter for scale):
Image
Image

The audio files on the SD card are just as shown in the code "2.wav".

I do have two identical speakers but I figured I would just connect one at the moment for testing incase I accidentally blow something up ;)

Instead of using the 3.5mm jack I am trying to use the digital RIN and LIN pins, I would rather keep it this way but most of the guides (in fact almost all) use the 3.5mm jack as input so it makes it more difficult for me to understand the setup. I'm also currently just using the USB cable for the Arduino power currently, the same one I upload the code onto the device with.

I hope I gave enough detailed information but writing this my brain turned a little to mush, so just let me know if you need any more details of the setup!

Thanks in Advance!

Risen91
 
Posts: 3
Joined: Fri Jun 18, 2021 9:55 am

Re: Arduino + MAX9744 + SD card speaker project

by blnkjns on Sun Jun 20, 2021 9:20 am

There is no sound, as you don't have a component that actually can play the WAV!
The I2C connector for the amp board is just there for volume control.
You can do 2 things:
Move to a microcontroller with audio-out, like the Itsy Bitsy M4.
https://www.adafruit.com/product/3800
Or use a WAV-playback SD board, with analog out to connect to your Amp board.
https://www.adafruit.com/product/1381
or if 2 or 16MB is enough:
https://www.adafruit.com/product/2342
https://www.adafruit.com/product/2341

And for improving compatibility between boards, never attach I2C to A4/A5 or SPI to pin 11,12,13! Those ONLY work on the UNO.
I know there are many "bad" instructions out in the wild, but always connect to the SDA/SCL pins and the ICSP header. That way you can move projects between various boards without running into "not working" issues.

blnkjns
 
Posts: 663
Joined: Fri Oct 02, 2020 3:33 am

Re: Arduino + MAX9744 + SD card speaker project

by Risen91 on Mon Jun 21, 2021 11:44 am

Thanks for the response blnkjns!

I didn't know I needed something that can play the .wav since in a previous test using only the Arduino, SD card reader and a speaker I was able to get it to play the .wav files fine... just very quiet! Which is why I though just adding the amplifier to it would work to increase the volume. Are you sure this is right?

Also thanks for the advice on pins! I will definitely keep it in mind in future projects! :) I also have the Arduino Nano which I'm guessing also works the same as the UNO? or similar :D

Risen91
 
Posts: 3
Joined: Fri Jun 18, 2021 9:55 am

Re: Arduino + MAX9744 + SD card speaker project

by blnkjns on Tue Jun 22, 2021 2:49 am

In theory you can push the .wav data into a PWM output on a digital pin. And you can hear that yes. But the signal will be a very harsch pulse-width modulated square wave. You could filter out overtones with coils and capacitors, and smoothen the stuff so it can be amplified, but having a true DAC is better, easier and probably cheaper. You could add a manual DAC to your boards:
https://www.adafruit.com/product/935

blnkjns
 
Posts: 663
Joined: Fri Oct 02, 2020 3:33 am

Re: Arduino + MAX9744 + SD card speaker project

by Risen91 on Tue Jun 22, 2021 1:47 pm

I had no idea, thanks! and I think the VS1053 WAV-playback SD board looks like a really good suggestion where I can just replace my existing Micro SD with that instead. Hopefully things will go smoother with this setup and I should be able to figure it out.
Thanks again!

Risen91
 
Posts: 3
Joined: Fri Jun 18, 2021 9:55 am

Please be positive and constructive with your questions and comments.