3

I am trying to build a system, that plays specific audios (.wav files) from a SD-Card, according to the key combination that has been pressed.

I am using:

So I broke the project down in various steps:

  1. playing .wav file from the SD Card (works fine)
  2. using keypad (works fine)
  3. combinig both (as you may have guessed: does not work)

Here are the codes I used for every step:

1)

#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

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

  tmrpcm.setVolume(5);
  tmrpcm.play("test.wav");
}

void loop() {}

This plays test.wav one time after uploading or resetting.

2)

#include <Keypad.h>

// Definition für das Keypad von Elegoo
const byte ROWS = 4; // vier Zeilen
const byte COLS = 4; // vier Spalten
// Symbole auf den Tasten
const byte rows[ROWS] = {2, 3, 12, 5};//connect to the row pinouts of the keypad
const byte cols[COLS] = {6, 7, 8, 11};//connect to the column pinouts of the keypad 

char keys[ROWS][COLS] = { //create 2D arry for keys
  {'D', 'C', 'B', 'A'},
  {'#', '9', '6', '3'},
  {'0', '8', '5', '2'},
  {'*', '7', '4', '1'},
};


// Instanz von Keypad erzeugen, Keymap und Pins übergeben
Keypad customKeypad = Keypad(makeKeymap(keys), rows, cols, ROWS, COLS);

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

void loop() {
  // Gedrückte Taste abfragen
  char customKey = customKeypad.waitForKey();
  Serial.println(customKey);
}

This code prints out every pressed key.

3)

#include <Keypad.h>
#include "SD.h"
#define SD_ChipSelectPin 4
#include "TMRpcm.h"
#include "SPI.h"

TMRpcm tmrpcm;

// Definition für das Keypad von Elegoo
const byte ROWS = 4; // vier Zeilen
const byte COLS = 4; // vier Spalten
// Symbole auf den Tasten
const byte rows[ROWS] = {2, 3, 12, 5};//connect to the row pinouts of the keypad
const byte cols[COLS] = {6, 7, 8, 11};//connect to the column pinouts of the keypad 

char keys[ROWS][COLS] = { //create 2D arry for keys
  {'D', 'C', 'B', 'A'},
  {'#', '9', '6', '3'},
  {'0', '8', '5', '2'},
  {'*', '7', '4', '1'},
};

// Instanz von Keypad erzeugen, Keymap und Pins übergeben
Keypad customKeypad = Keypad(makeKeymap(keys), rows, cols, ROWS, COLS);

void setup() {
    tmrpcm.speakerPin = 9;
  Serial.begin(9600);
  if (!SD.begin(SD_ChipSelectPin)) {
    Serial.println("SD fail");
    return;
  }
} 
  
void loop() {
  // Gedrückte Taste abfragen
  char customKey = customKeypad.waitForKey();
  Serial.println(customKey);

  if (customKey == 8) {
    tmrpcm.setVolume(5);
    tmrpcm.play("test.wav");
  }
}

This code should play test.wav if key 8 is pressed.

THE PROBLEM

So the problem is not that there is no sound at all, but its just completely distorted. The sound quality never was great but sufficient. Now it sounds like "too much signal" is coming onto the speaker and interferes with the audio. Even if there shouldn't be any audio playing, machine-like sounds are produced by the speaker. By slowly working through the code I managed to find the line that probably causes the problem:

char customKey = customKeypad.waitForKey();
Serial.println(customKey);

If put it in a comment and put

tmrpcm.setVolume(5);
tmrpcm.play("test.wav");

in the setup function the audio is played.

I tried a lot and it doesn't matter where you put the getKey line (or change it to waitForKey()), as long as this line is somewhere in a function the sound is distorted.

Why does this happen? How can i work around it?

Can anybody please help me with this? :)

wiring diagram

enter image description here enter image description here enter image description here enter image description here enter image description here enter image description here

4
  • 3
    Pins 11 and 12 are MISO/MOSI SPI pins, you're using them both for the SD card and the touchpad
    – Mat
    Dec 22 '20 at 6:27
  • Hm ok, so so you have a solution? I am running Out of Pins... Can I use a different wiring? What Others Pins could I use to avoid it? And what about the Others digital Pins that have an additional SPI purpose? Are they a Problem as well?
    – D_0
    Dec 22 '20 at 8:51
  • 1
    You could probably use pins from A0 to A3 (or to A5 if you don't use I2C) electronics.stackexchange.com/questions/67103/…
    – Mat
    Dec 22 '20 at 8:58
  • Thank you! You were right. Pins 11 and 12 interfered with the other SPI pins i used. I switched to A0 and A2 and now everything works perfectly.
    – D_0
    Dec 23 '20 at 2:07

Your Answer

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

Browse other questions tagged or ask your own question.