Analog pin reading very large value after recording to SD card

Hi Guys,

I'm trying to use analog sensor to begin recording a wav file to a sd card with sd library.

The sensor reads correct values on the first recording but after that the value spikes and continually triggers the recording without any control.

I've tested with both a potentiometer and a piezo buzzer to use as a trigger and still get the same problem.

I tested powering the sd card adapter from a separate battery and smae result.

Attached images of my hardware.

Any help would be much appreciated.

Cheers!

/*
This sketch demonstrates recording of standard WAV files that can be played on any device that supports WAVs. The recording
uses a single ended input from any of the analog input pins. Uses AVCC (5V) reference currently.

Requirements:
Class 4 or 6 SD Card
Audio Input Device (Microphone, etc)
Arduino Uno,Nano, Mega, etc.

Steps:
1. Edit pcmConfig.h
    a: On Uno or non-mega boards, #define buffSize 128. May need to increase.
    b: Uncomment #define ENABLE_RECORDING and #define BLOCK_COUNT 10000UL

2. Usage is as below. See https://github.com/TMRh20/TMRpcm/wiki/Advanced-Features#wiki-recording-audio for
   additional informaiton.

Notes: Recording will not work in Multi Mode.
Performance is very dependant on SD write speed, and memory used.
Better performance may be seen using the SdFat library. See included example for usage.
Running the Arduino from a battery or filtered power supply will reduce noise.
*/

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

#define SD_ChipSelectPin 4  //example uses hardware SS pin 53 on Mega2560
//#define SD_ChipSelectPin 4  //using digital pin 4 on arduino nano 328, can use other pins

//values for piezo sensor

int ledPin = 13;
int knockSensor = 1;
byte val = 0;
boolean r = false;
int THRESHOLD = 197;


TMRpcm audio;   // create an object for use in this sketch
int ctr = 0;
String writeName;
String readName;
char writeVar[11];
char readVar[11];
void setup() {

  audio.speakerPin = 9; //5,6,11 or 46 on Mega, 9 on Uno, Nano, etc
  pinMode(12, OUTPUT); //Pin pairs: 9,10 Mega: 5-2,6-7,11-12,46-45

  Serial.begin(115200);

  if (!SD.begin(SD_ChipSelectPin)) {
    return;
  } else {
    Serial.println("SD OK");
  }
  // The audio library needs to know which CS pin to use for recording
  audio.CSPin = SD_ChipSelectPin;

}

//Functions for updating the file names

void updateWriteName() {
  String ctrStr = String(ctr);
  writeName = String(ctrStr + "test.wav");
  for (int i = 0; i < writeName.length(); i++) {
    writeVar[i] = writeName.charAt(i);
  }
  Serial.println(writeVar);
}

void updateReadName() {
  String ctrStr = String(ctr);
  readName = String(ctrStr + "test.wav");
  for (int i = 0; i < readName.length(); i++) {
    readVar[i] = readName.charAt(i);
  }
  Serial.println(readVar);
}

//this one lets you specify a number
void updateReadName(int num) {
  String ctrStr = String(num);
  readName = String(ctrStr + "test.wav");
  for (int i = 0; i < readName.length(); i++) {
    readVar[i] = readName.charAt(i);
  }
  Serial.println(readVar);
}

//Functions for triggering recording
void triggerRecord() {

  updateWriteName();
  updateReadName();
  audio.startRecording(writeVar, 16000, A0, 1); // get rid of the 1 argument to remove passthrough to speaker

}


void loop() {

  // for piezo sensor

  val = analogRead(knockSensor);

  if (val >= THRESHOLD) {

    triggerRecord();
    delay(5000);  // we have to make a delay to avoid overloading the serial port
    audio.stopRecording(readVar);
    ctr += 1;

    //distort the last wav file
    
    //delay
    delay(2000);
    
    //playback the last wav file
audio.play(readVar);

delay(5000);

  }
  // Gwills instructions for what we need to code for the whole project - We can have a go at it and send to him to help us debug it.
  //constantly check sensor value
  //if sensor value within some range
  //then make a number to use as a read name
  //e.g. 1
  //then call the updateReadName function and use this number as the argument
  //then call  audio.play(readVar);
  //(this will play that selected file)

  //constantly check different sensor value
  //if within some range
  //update the write name
  //call the start recording function
  //keep checking the sensor, or just record for a set amount of time (start a delay)
  //call the stop recording function
  //update the counter

  if (Serial.available()) {
    //Send commands over serial to play
    //setup file naming



    switch (Serial.read()) {

      case 'r': audio.startRecording("test.wav", 16000, A0); break;  //Record at 16khz sample rate on pin A0
      case 'R': triggerRecord();
        break;  //Record, but with passthrough to speaker.
      case 't': audio.startRecording("test.wav", 16000, A0, 2); break; //Do not record. Output direct to speaker
      //Note: If samples are dropped before writing, it
      //      will not be heard in passthrough mode
      case 's': audio.stopRecording(readVar);
        ctr += 1;
        break;              //Stop recording
      case 'p': audio.play(readVar); break;                      //Play the recording
      case '=': audio.volume(1); break;                              //Increase volume by 1. Does not affect recording
      case '-': audio.volume(0); break;                              //Decrease volume by 1. Does not affect recording
      case 'S': audio.stopPlayback(); break;           //Stop all playback
      case '1': updateReadName(1);                                //Pick file to play by name
        audio.play(readVar);
        break;
    }
  }
}

A wiring diagram would be more informative.

Posting or Attaching the code being run would help also.

Hi,

I have reposted with code and wiring diagram (apologies for roughness).

Cheers

Are you sure that your SD card should be powered by 3.3V?

The SD card signals may cause trouble, when the card is powered by 3.3V, while the Arduino sends and expects 5V signals. Also the on-board 3.3V regulator may be too weak to power the SD card.

The speaker on pin 9 and 10. I suppose that's a 600ohm type.
Not some common 4 or 8ohm speaker, because that would be an almost dead short for an Arduino.
Leo..

Hi,

Thanks for the responses.

Leo - The speaker is an 8ohm 0.5w speaker. What pins would you recommend I use?

DrDiettrich - Information I have found previously suggests that 3.3v is best suited to power the sd card adapter. Should I try powering it on 5v? It won't fry the sd card adapter will it?

Will either of these things effect the issue with the piezo sensor reading?

Cheers,

JC