搜尋程式碼

使用簧片開關控制繼電器同交直流負載,配合Arduino

使用簧片開關控制繼電器同交直流負載,配合Arduino

喺呢個教學入面,我哋會探討點樣用一個磁簧開關配合Arduino嚟控制一個可以管理AC同DC負載嘅繼電器。磁簧開關係一個磁性感應器,等我哋可以根據磁石嘅接近程度嚟觸發繼電器。呢個配置好適合用喺警報器、自動燈光,或者任何你想根據磁場存在與否嚟控制裝置嘅場景。最終結果會係一個完全運作嘅電路,可以根據磁簧開關嘅狀態嚟開關燈或者警報器。

Reed Switch Schematic
Reed Switch module

當我哋建造呢個項目嘅時候,我哋會討論涉及嘅主要元件,包括磁簧開關、繼電器同Arduino。磁簧開關有三個引腳:VCC(電源)、GND(接地)同OUTPUT(訊號)。當磁石靠近嘅時候,開關會閉合,令訊號引腳變為HIGH,跟住就可以觸發繼電器。繼電器作為高電壓負載嘅開關,令佢適合用嚟控制燈或者警報器呢啲裝置。

如果想睇視覺示範同進一步嘅解釋,記得睇吓相關嘅影片(喺影片00:00位置)。

Reed Switch module to view

硬件解釋

呢個項目嘅主要元件包括一個磁簧開關、一個繼電器模組同一個Arduino板。磁簧開關係一個磁性感應器,當磁石靠近嘅時候會閉合佢嘅觸點,令電流可以流過OUTPUT引腳。呢個引腳會發送訊號畀繼電器,繼電器就可以安全噉控制高電壓裝置。

繼電器模組係設計嚟控制更大嘅負載。佢有三個主要連接:Common(COM)、Normally Open(NO)同Normally Closed(NC)。當繼電器被Arduino啟動嘅時候,佢會將COM引腳連接到NO引腳,令電流可以流到連接嘅裝置。呢個設置容許多種電器裝置嘅靈活控制,而唔需要直接將Arduino同高電壓連接。用嚟放大磁簧開關訊號嘅主要放大器係LM393運算放大器。

模組嘅數據表

製造商未知
邏輯/IO電壓3.3V-5V
供應電壓5V
輸出電流(每通道)最大10A
峰值電流(每通道)最大16A
PWM頻率指引唔適用
輸入邏輯閾值最低2.5V(HIGH)
電壓降 / RDS(on) / 飽和典型0.1V
熱限制最高85°C
封裝PCB安裝

接線指示

Arduino wiring for Reed switch module
Arduino wiring for Reed switch module

要接線呢個系統,首先連接磁簧開關。將磁簧開關嘅VCC引腳連接到Arduino嘅5V輸出,然後將GND引腳連接到Arduino嘅其中一個接地引腳。磁簧開關嘅OUTPUT引腳應該連接到Arduino嘅數碼引腳2。咁樣就可以令Arduino讀取磁簧開關嘅狀態。

跟住,接線繼電器模組。將繼電器嘅VCC引腳連接到Arduino嘅5V輸出,GND引腳連接到接地。繼電器嘅輸入引腳,即係控制繼電器操作嗰個,應該連接到Arduino嘅數碼引腳10。最後,將負載(好似燈膽)連接到繼電器嘅Common同Normally Open端子,確保負載適合繼電器嘅規格。

代碼示例同講解

以下嘅代碼片段初始化引腳同設定串行通訊用嚟除錯。磁簧開關連接到引腳2,而繼電器就透過引腳10嚟控制。


int LD = 200; // 喺再做另一次讀取之前等待嘅時間(毫秒)。

void setup() {
    Serial.begin(9600);
    pinMode(10, OUTPUT); // 連接到繼電器
    pinMode(2, INPUT); // 磁簧開關輸入引腳2  
    Serial.println("Robojax測試:磁簧開關");
}

喺loop函數入面,會持續檢查磁簧開關嘅狀態。如果開關閉合(表示有磁石靠近),繼電器就會開啟。如果開關打開,繼電器會喺延遲5秒之後關閉,畀一個寬限期先至停用。


void loop() {
    if(digitalRead(2)){
      Serial.println("開關開啟 "); 
      digitalWrite(10, LOW); // 開啟繼電器    
      delay(LD); 
    }else{
      delay(5000); // 等5秒先至關閉警報器
      digitalWrite(10, HIGH); // 關閉繼電器
    }
}

呢個代碼容許根據磁簧開關嘅狀態靈活噉控制繼電器。你可以透過更改延遲值或者根據開關輸入嚟控制繼電器嘅方式,嚟修改時間同行為。

示範 / 預期效果

當設置完成之後,將磁石靠近磁簧開關應該會啟動繼電器,開啟連接嘅燈或者警報器。如果移走磁石,燈會保持開啟一段指定嘅延遲時間先至關閉,畀用戶一個緩衝時間。確保測試系統,確認繼電器嘅行為符合預期,並且正確噉回應磁簧開關。

影片時間戳

  • 00:00 - 項目介紹
  • 02:15 - 硬件解釋
  • 05:30 - 接線示範
  • 08:45 - 代碼講解
  • 12:00 - 預期結果同疑難排解貼士

圖片

Reed Switch module top view
Reed Switch module top view
Reed Switch Schematic
Reed Switch Schematic
reed_switch-4
reed_switch-4
Reed Switch module
Reed Switch module
Reed Switch module to view
Reed Switch module top view
Reed Switch module
Reed Switch module
Arduino wiring for Reed switch module
Arduino wiring for Reed switch module
22-Reed Magnetic Switch Arduino with Relay code
語言: C++
++
//*
 * This is the Arduino code for a reed switch to
 * turn a relay ON, which the relay
 * can turn a light or alarm ON.
 * The reed switch is connected to pin 2.
 * The relay is connected to pin 10.
 
 * Watch the video for this code: https://www.youtube.com/watch?v=2RBFKqoauaI
 * 
 * Written by Ahmad Shamshiri for Robojax.com Video
 * Date: December 5, 2017, in Ajax, Ontario, Canada
 * Permission granted to share this code, given that this
 * note is kept with the code.
 
 * Please keep this note with the code.
 * This code is available on Robojax.com
 * 
 * This code is "AS IS" without warranty or liability. Free to be used as long as you keep this note intact.
 * This code has been downloaded from Robojax.com
    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */


 int LD = 200; // time in milliseconds to wait before making another reading.

void setup() {
    Serial.begin(9600);
    // output pins
    pinMode(10, OUTPUT);// connected to relay
         

    // input pins
    pinMode(2, INPUT);// reed switch  input pin 2  


    Serial.println("Robojax Test: Reed switch");

}

void loop() {

    // read the REED switch
    if(digitalRead(2)){
      Serial.println("Switch ON "); 
      digitalWrite(10, LOW); // Turn the relay ON    
      delay(LD); 
    }else{
      delay(5000);// wait 5 seconds before turning the alarm off
				// remove this line if you don't want any delay
      digitalWrite(10, HIGH);// Turn the relay OFF
      
    }
  




}// loop

資源與參考資料

暫時冇資源。

文件📁

冇文件可用。