|
您是否曾经想过购买一台小型录音机来录制环境中的声音并将其存储在内存中?您只需执行几个简单的步骤即可轻松制作自己的录音机。该项目主要是制作基于Arduino的录音机,也可以将其用作Spy Bug(间谍窃听器)。我们的Spy Bug使用小型麦克风记录语音,并将所记录的语音存储到SD卡中。它可以录制2分钟长的音频片段,每2分钟长的片段将被顺序编号。通电后,录制过程将持续进行,直到电池没电或没有更多空间存储录制的音频。我们还在Arduino上连接了一个LED,指示录制过程已经开始。这些听起来很有趣,对吧?让我们开始吧。
所需的组件 ● Arduino Nano开发板 ● MAX9814放大器板 ● SD卡模块 ● SD卡 ● 5V电源
基于Arduino的Spy Bug –原理图 下面显示了基于Arduino的Spy Bug的完整示意图。您可以在原理图中看到该项目的主控是Arduino Nano。
该项目的主要组件是MAX9814,这是一款低成本、高性能麦克风放大器,具有自动增益控制(AGC)和低噪声麦克风偏置。该器件具有低噪声前端放大器、可变增益放大器(VGA)、输出放大器、麦克风偏置电压发生器和AGC控制电路。
MAX9814(如上图所示)记录声音并将其传输到Arduino。然后,Arduino将声音转换为.wav格式,并将其存储在与Arduino连接的MicroSD卡模块中。
MAX9814模块中有许多引脚,但是主要使用VCC、GND和out引脚。还有一个增益引脚可以连接到GND或VDD,以获得50dB和40dB的受控增益。但是我们没有使用增益控制。默认情况下,模块提供的未压缩增益设置为60dB。
音频以2分钟的格式存储,然后开始存储在新文件中。因此,每个.wav文件的长度为2分钟。
代码说明 基于Arduino的Spy Bug录音设备的代码非常简单,以下将简要说明。 - #include <TMRpcm.h>
- #include <SD.h>
- #include <SPI.h>
复制代码在上面代码中,包含了基于SPI、SD和TMR的PCM库。要编译代码,请使用此库。您可以从GitHub下载TMRpcm库。 - TMRpcm audio; // make instance variable
- int file_number = 0;
- char filePrefixname[50] = "spy";
- char exten[10] = ".wav";
复制代码在上述代码中,设置了名称前缀和转换文件类型以及将存储音频的起始文件号。文件名将是spyxxx.wav - const int recordLed = 2;
- const int mic_pin = A0;
- const int sample_rate = 16000;
复制代码在代码中,选择“正在录制”通知LED,这是Arduino中可用的LED。mic引脚在A0引脚中可用,采样率为16000位。 - #define SD_CSPin 10 // defines the CS pin of the SD card.
- // delay function for serial log.
- void wait_min(int mins) {
- int count = 0;
- int secs = mins * 60;
- while (1) {
- Serial.print('.');
- delay(1000);
- count++;
- if (count == secs) {
- count = 0;
- break;
- }
- }
- Serial.println();
- return ;
- }
复制代码上面的代码用于打印序列号。
- void setup() {
- // put your setup code here, to run once:
- //initializes the serial connection between the Arduino and any connected serial device(e.g. computer, phone, raspberry pi...)
- Serial.begin(9600);
- //Sets up the pins
- pinMode(mic_pin, INPUT);
- pinMode(recordLed, OUTPUT);
- Serial.println("loading... SD card");
- if (!SD.begin(SD_CSPin)) {
- Serial.println("An Error has occurred while mounting SD");
- }
- while (!SD.begin(SD_CSPin)) {
- Serial.print(".");
- delay(500);
- }
- audio.CSPin = SD_CSPin;
- }
复制代码在setup函数代码中,设置了引脚方向并安装了SD卡。如果有问题,代码将在串口监视器输出错误。 - void loop() {
- Serial.println("####################################################################################");
- char fileSlNum[20] = "";
- itoa(file_number, fileSlNum, 10);
- char file_name[50] = "";
- strcat(file_name, filePrefixname);
- strcat(file_name, fileSlNum);
- strcat(file_name, exten);
- Serial.print("New File Name: ");
- Serial.println(file_name);
- digitalWrite(recordLed, HIGH);
- audio.startRecording(file_name, sample_rate, mic_pin);
- Serial.println("startRecording ");
- // record audio for 2mins. means, in this loop process record 2mins of audio.
- // if you need more time duration recording audio then
- // pass higher value into the wait_min(int mins) function.
- wait_min(2); // This is the length of the audio file which is set to 2 minutes
- digitalWrite(recordLed, LOW);
- audio.stopRecording(file_name);
- Serial.println("stopRecording");
- file_number++;
- Serial.println("####################################################################################");
- }
复制代码
在loop()函数代码中,录音器无限循环记录语音。 然后将它们存储在SD卡中,每次完成文件写入操作时,文件编号都会增加。
测试基于Arduino的Spy Bug 连接所有组件并烧录Arduino的代码。 它将按预计的方式工作。
使用手机录制和测试音频。如果您喜欢这篇文章,请在本帖下面进行回复。 |