錄音模組控制

今天我們要使用 Arduino® 來操作兩個與聲音有關的模組:

  1. ISD-1820 錄音模組,具有錄音與播放功能
  2. HC-SR04 超音波感測器,可感測前方物體距離

錄音模組 ISD1820 如下圖所示,由於該模組可接受 3V 或是 5V 電源,因此使用時可將左側 Vcc 接上 3V/5V 電源 GND 接上電源負極,右側的 SP1 接頭則是接上喇叭。它提供下列操作方式,我們的應用會採用 前兩項錄音與 PLAYE 播放功能:

  1. 錄音 – 按下紅色 REC 鍵可錄製聲音 10 秒鐘
  2. PLAYE 播放 – 按下或拿開 PLAYE 按鈕時播放先前錄製的聲音
  3. PLAYL 播放 – 按下 PLAYL 按鈕時播放,拿開時立即停止
  4. 連續播放 – 將排針 P-E 短路則連續播放

 

HC-SR04 超音波感測器可發出頻率 40kHz 超音波,當超音波碰觸物體時反彈再由 HC-SR04 接收,由於聲音的速度為已知條件約為 343m/s (20 °C) ,藉由量測聲音發出與回音之間的時間差可推算物體與超音波收發端的間距。假設量測到的時間差為 t 秒則距離 d = ( t * 343m/s)/2,由於應用上我們想感測較近距離的物體因此採用公分以及微秒為單位,因此算式改寫為 d = ( t * 0.0343cm/us)/2。

接下來我們需要將 Arduino 與超音波感測器以及錄音模組接線在一起

  • 將各個板子 Vcc/GND 連接
  • Arduino Pin5 <-> 錄音模組 P-E
  • Arduino Pin2 <-> 超音波感測器 Trig
  • Arduino Pin4 <-> 超音波感測器 Echo

接線如下圖:

當然最重要的是燒錄程式至 Arduino 開發板,程式流程為:

  1. Arduino 發訊號給超音波模組的 Trig pin 發出超音波
  2. 等待超音波模組接收回波 Echo pin 的回傳訊號
  3. 取得時間差並計算距離
  4. 判斷距離與前次距離是否有劇烈變化(差異 1.6 倍以上)
  5. 入差異變化劇烈則觸發錄音模組發出聲音
  6. 重複步驟 1.

將下列程式碼貼到 Arduino IDE 中編譯並燒錄到開發板上。如果不熟悉 Arduino 開發過程,建議閱讀 Arduino 初探

const int trigPin = 2;
const int echoPin = 4;
const int soundPin = 5;
long t, cm, previous_cm;
  
void setup() {
  // initialize serial communication:
  Serial.begin(9600);
  Serial.setTimeout(0); 
  // initialize pins
  pinMode(trigPin, OUTPUT);
  pinMode(soundPin, OUTPUT);
  pinMode(echoPin, INPUT);
  // initialize varibles
  cm=1;
  previous_cm=1;
}

void loop()
{
  // The sensor is triggered by a HIGH pulse of 10 or more microseconds.
  // Give a short LOW pulse beforehand to ensure a clean HIGH pulse:
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(10);
  digitalWrite(trigPin, LOW);

  // Read the signal from the sensor
  t = pulseIn(echoPin, HIGH);

  // convert the time into a distance
  cm = microsecondsToCentimeters(t);
  if (previous_cm > cm*1.6){
    Serial.print("Oh, somthing getting closer!");
    digitalWrite(soundPin, HIGH);
    delayMicroseconds(10);
    digitalWrite(soundPin, LOW);    
  }
  Serial.print(cm);
  Serial.print("cm");
  Serial.println();
  
  previous_cm = cm;
  delay(100);
}

long microsecondsToCentimeters(long microseconds)
{
  // The speed of sound is 0.0343cm/us.
  // To find the distance of the object 
  // we take half of the distance travelled.
  return (microseconds * 0.0343) / 2;
}

 

最後讓我們來測試吧!移動物體接近超音波模組,Arduino 經由計算比對後察覺有物體接近,接著觸發錄音模組的 P-E 使錄音模組發出聲音!

2 Comments

  1. T.M.2022-09-30

    我對ISD-1820 錄音模組,具有錄音與播放功能和HC-SR04 超音波感測器,可感測前方物體距離結合的成效有興趣
    請問如貴司網頁上整個package(包含燒錄程式至 Arduino 開發板;錄音文字自行修改即可)出貨給我,是否OK?若可以,請問要多少費用

    回覆
  2. 越港國小創客師-林柏安2022-10-20

    出現以下問題
    能解決的話我可以付錢
    Connecting to programmer: .
    Found programmer: Id = “CATERIN”; type = S
    Software Version = 1.0; No Hardware Version given.
    Programmer supports auto addr increment.
    Programmer supports buffered memory access with buffersize=128 bytes.

    Programmer supports the following devices:
    Device code: 0x44

    回覆

發佈回覆給「T.M.」的留言 取消回覆

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Scroll to top