Hi everyone,
I'm trying to use a Teensy 3.6, the audio shield, and the Adafruit I2S microphone (https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout) to record sound to the SD card.
Here are my connections:
Microphone --> Teensy
SEL --> GND
LRCLK --> pin 23
DOUT --> Line in
BCLK --> pin 9
GND --> GND next to line in
3V --> 3V
I have adapted the recorder example to remove the play option and use of buttons as shown below.
I'm able to record to the SD card successfully, when I import the file into Audacity you can hear music playing in the background but there is so much noise in the recording too.
I know the audio library is set up for 16-bit data and the microphone outputs 24-bit so I don't know if that's the problem, however, I have seen in others have managed to get this microphone working.
Does anyone have any ideas how to fix this please? The recording and wiring photos can be found here: https://drive.google.com/drive/folders/1hZaF_oPHFYxPeRJft5oK1y-HC7paSW5i?usp=sharing
Thanks in advance!
I'm trying to use a Teensy 3.6, the audio shield, and the Adafruit I2S microphone (https://learn.adafruit.com/adafruit-i2s-mems-microphone-breakout) to record sound to the SD card.
Here are my connections:
Microphone --> Teensy
SEL --> GND
LRCLK --> pin 23
DOUT --> Line in
BCLK --> pin 9
GND --> GND next to line in
3V --> 3V
I have adapted the recorder example to remove the play option and use of buttons as shown below.
Code:
// Record sound as raw data to a SD card, and play it back.
#include <Bounce.h>
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
// GUItool: begin automatically generated code
AudioInputI2S i2s2; //xy=105,63
AudioAnalyzePeak peak1; //xy=278,108
AudioRecordQueue queue1; //xy=281,63
AudioOutputI2S i2s1; //xy=470,120
AudioConnection patchCord1(i2s2, 0, queue1, 0);
AudioConnection patchCord2(i2s2, 0, peak1, 0);
AudioControlSGTL5000 sgtl5000_1; //xy=265,212
// GUItool: end automatically generated code
const int myInput = AUDIO_INPUT_LINEIN;
unsigned long currentMillis;
unsigned long lastMillis;
unsigned long startMillis;
const unsigned long period = 20000;
// Use these with the Teensy 3.5 & 3.6 SD card
#define SDCARD_CS_PIN BUILTIN_SDCARD
int mode = 0; // 0=stopped, 1=recording, 2=playing
// The file where data is recorded
File frec;
void setup() {
// Audio connections require memory, and the record queue
// uses this memory to buffer incoming audio.
AudioMemory(60);
// Enable the audio shield, select input, and enable output
sgtl5000_1.enable();
sgtl5000_1.inputSelect(myInput);
sgtl5000_1.volume(0.5);
sgtl5000_1.lineInLevel(2, 2);
// Initialize the SD card
if (!(SD.begin(SDCARD_CS_PIN))) {
// stop here if no SD card, but print a message
while (1) {
Serial.println("Unable to access the SD card");
delay(500);
}
}
startMillis = millis();
}
void loop() {
currentMillis = millis();
startRecording();
Serial.println("Recording..");
while ((currentMillis - startMillis) <= period) {
continueRecording();
currentMillis = millis();
}
if (currentMillis - startMillis > period)
{
stopRecording();
Serial.println("Stopped recording.");
while (1);
}
}
void startRecording() {
Serial.println("startRecording");
if (SD.exists("RECORD.RAW")) {
// The SD library writes new data to the end of the
// file, so to start a new recording, the old file
// must be deleted before new data is written.
SD.remove("RECORD.RAW");
}
frec = SD.open("RECORD.RAW", FILE_WRITE);
if (frec) {
queue1.begin();
mode = 1;
}
}
void continueRecording() {
if (queue1.available() >= 2) {
byte buffer[512];
// Fetch 2 blocks from the audio library and copy
// into a 512 byte buffer. The Arduino SD library
// is most efficient when full 512 byte sector size
// writes are used.
memcpy(buffer, queue1.readBuffer(), 256);
queue1.freeBuffer();
memcpy(buffer + 256, queue1.readBuffer(), 256);
queue1.freeBuffer();
// write all 512 bytes to the SD card
//elapsedMicros usec = 0;
frec.write(buffer, 512);
// Uncomment these lines to see how long SD writes
// are taking. A pair of audio blocks arrives every
// 5802 microseconds, so hopefully most of the writes
// take well under 5802 us. Some will take more, as
// the SD library also must write to the FAT tables
// and the SD card controller manages media erase and
// wear leveling. The queue1 object can buffer
// approximately 301700 us of audio, to allow time
// for occasional high SD card latency, as long as
// the average write time is under 5802 us.
//Serial.print("SD write, us=");
//Serial.println(usec);
}
}
void stopRecording() {
Serial.println("stopRecording");
queue1.end();
if (mode == 1) {
while (queue1.available() > 0) {
frec.write((byte*)queue1.readBuffer(), 256);
queue1.freeBuffer();
}
frec.close();
}
mode = 0;
}
I'm able to record to the SD card successfully, when I import the file into Audacity you can hear music playing in the background but there is so much noise in the recording too.
I know the audio library is set up for 16-bit data and the microphone outputs 24-bit so I don't know if that's the problem, however, I have seen in others have managed to get this microphone working.
Does anyone have any ideas how to fix this please? The recording and wiring photos can be found here: https://drive.google.com/drive/folders/1hZaF_oPHFYxPeRJft5oK1y-HC7paSW5i?usp=sharing
Thanks in advance!