[Diao Ye learns programming] Arduino hands-on (52)---MicroSD card reading and writing module 4

The idea of ​​37 sensors and modules has been widely circulated on the Internet. In fact, there must be more than 37 sensor modules compatible with Arduino. In view of the fact that I have accumulated some sensor and actuator modules on hand, according to the concept of true knowledge (must be hands-on), for the purpose of learning and communication, I am going to try and do more experiments one by one here, and I will record them whether I succeed or not.

[Arduino] 168 kinds of sensor module series experiments (data code + simulation programming + graphics programming)
Experiment 52: SPI interface Micro SD card module TF card reader (with level conversion chip)

insert image description here

Project: Make a Simple Audio Recorder

1. Download and install the TMRpcm library, the address is https://github.com/TMRh20/TMRpcm
https://gitee.com/hu1023/TMRpcm

insert image description here

2. After decompression, open the folder and open pcmConfig.h with Notepad

insert image description here

3. There is also a method for recording in TMRpcm, which is commented out in the source code. After installing the library, you need to open the pcmConfig.h file in the source code library and uncomment the following lines, otherwise the compilation will fail:

#define buffSize 128  //must be an even number
#define ENABLE_RECORDING // Amount of space to pre-allocate for recording
#define BLOCK_COUNT 10000UL  // 10000 = 500MB   2000 = 100MB

Save, then add the library manually.

[Arduino] 168 sensor module series experiments (data code + simulation programming + graphic programming)
Experiment 52: SPI interface Micro SD card module TF card reader (with level conversion chip) program
: MAX9814 simple recorder
Wiring: MAX9814 connected to A0
MicroSD uno
CS-------------10
MOSI (DI)------11
MISO (DO)------12
CLK------------13

/*
  【Arduino】168种传感器模块系列实验(资料代码+仿真编程+图形编程)
  实验五十二:SPI接口Micro SD卡模块TF卡读写卡器 (带电平转换芯片)
  程序:MAX9814简易录音机
  接线:MAX9814接A0
  MicroSD       uno
  CS-------------10
  MOSI(DI)------11
  MISO(DO)------12
  CLK------------13
*/

#define buffSize 128  //must be an even number
#define ENABLE_RECORDING // Amount of space to pre-allocate for recording
#define BLOCK_COUNT 10000UL  // 10000 = 500MB   2000 = 100MB

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

#define SD_ChipSelectPin 10
TMRpcm audio;   //  创建一个用于此程序的对象

void setup() {
    
    
  audio.speakerPin = 4;
  Serial.begin(115200);
  if (!SD.begin(SD_ChipSelectPin)) {
    
    
    Serial.println("SD Fail");
    return;
  } else {
    
    
    Serial.println("SD OK");
  }
  // 音频库需要知道使用哪个 CS pin 进行录音
  audio.CSPin = SD_ChipSelectPin;
}

void loop() {
    
    
  if (Serial.available()) {
    
                            //通过串行发送命令进行播放
    char c = Serial.read();
    Serial.println(c);
    switch (c) {
    
    
      case 'r': audio.startRecording("test.wav", 16000, A0); break;  //在引脚 A0 上以 16khz 采样率记录
      case 'R': audio.startRecording("test.wav", 16000, A0, 1); break; //录音,但要直通到扬声器
      case 't': audio.startRecording("test.wav", 16000, A0, 2); break; //不记录, 直接输出到扬声器
      //注意:如果样本在写入前被丢弃,则
      //在直通模式下不会被听到
      case 's': audio.stopRecording("test.wav"); break;              //停止录音
      case 'p': audio.play("test.wav"); break;                       //播放录音
      case '=': audio.volume(1); break;                              //音量加1,不影响录音
      case '-': audio.volume(0); break;                              //音量减1,不影响录音
      case 'S': audio.stopPlayback(); break;                         //停止所有播放

    }
  }
}

Arduino experiment scene diagram

insert image description here

Use the serial port command in the experiment, and return the screenshot of the situation

insert image description here

Open the TF card on the computer, and found that there is indeed a recording file named "test.wav". Play the file directly, and the sound is very small. It seems that it is just a simple demonstration experiment.

insert image description here
Through the small speaker mode, the sound is too small to hear

insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/131863403