Arduino 代碼同影片,用於 KY-024 霍爾感測器模組
喺呢個教學入面,我哋會探討點樣用 KY-024 霍爾感測器模組配合 Arduino 嚟偵測磁場。霍爾感測器會用嚟喺偵測到磁場嘅時候觸發一個動作,而我哋會將結果顯示喺 Serial Monitor 上面。結果就係一個簡單但有效嘅方法,將磁性感應整合入你嘅項目入面,畀你一個基礎去進一步擴展。想睇更詳細嘅解釋,記得睇吓條片(片入面 00:00)。


硬件解說
霍爾感測器模組係設計嚟偵測磁場嘅,通常用喺各種應用入面,包括近接感應同速度偵測。當有磁場存在嘅時候,感測器會輸出一個訊號,Arduino 可以讀取呢個訊號。咁樣 Arduino 就可以知道有帶磁場嘅物體喺附近。
喺呢個項目入面,我哋亦都會用一個蜂鳴器作為動作裝置。當霍爾感測器偵測到磁場嘅時候,Arduino 會啟動蜂鳴器嚟發出聲音警報。基本涉及嘅組件包括霍爾感測器、Arduino 板同蜂鳴器。
Datasheet 詳細資料
| 製造商 | Honeywell |
|---|---|
| 零件編號 | SS495A1 |
| 邏輯/IO 電壓 | 4.5 – 10 V |
| 供電電壓 | 4.5 – 10 V |
| 輸出電流(每通道) | 最大 20 mA |
| 峰值電流(每通道) | 最大 50 mA |
| PWM 頻率指引 | 不適用 |
| 輸入邏輯閾值 | 0.7 V(高),0.3 V(低) |
| 電壓降 / RDS(on) / 飽和 | 最大 0.4 V |
| 熱限制 | -40 至 +85 °C |
| 封裝 | TO-92 |
| 備註 / 變體 | 有唔同靈敏度可供選擇 |
- 確保供電喺指定電壓範圍內。
- 留意輸出電流限制,避免損壞模組。
- 如有需要,使用上拉電阻以獲得穩定讀數。
- 將感測器遠離可能造成干擾嘅強電磁場。
- 根據特定應用需求校準感測器。
接線說明


要將霍爾感測器模組接駁到 Arduino,首先將感測器嘅 VCC 腳連接到 Arduino 嘅 5V 腳嚟供電。跟住將 GND 腳連接到 Arduino 嘅 GND 腳嚟完成電路。霍爾感測器嘅輸出腳,通常標示為 OUT,應該連接到 Arduino 上面嘅數碼腳 2。
至於蜂鳴器,將正極端子連接到 Arduino 嘅數碼腳 8,然後將負極端子連接到 GND。呢個設定容許 Arduino 讀取感測器嘅輸出,並且喺偵測到磁場嘅時候啟動蜂鳴器。

程式碼範例同講解
以下程式碼初始化霍爾感測器同蜂鳴器。佢定義咗感測器同動作嘅腳位。主迴圈讀取感測器狀態,並相應啟動蜂鳴器。
#define DETECT 2 // pin 2 for sensor
#define ACTION 8 // pin 8 for action to do something
void setup() {
Serial.begin(9600);
pinMode(DETECT, INPUT); // define detect input pin
pinMode(ACTION, OUTPUT); // define ACTION output pin
}
喺呢段程式碼入面,DETECT 變數被指派到腳 2,用嚟讀取感測器。ACTION 變數被指派到腳 8,即係蜂鳴器連接嘅位置。setup 函數初始化串列通訊並設定腳位模式。


void loop() {
int detected = digitalRead(DETECT); // read Hall sensor
if (detected == LOW) {
digitalWrite(ACTION, HIGH); // set the buzzer ON
Serial.println("Detected!");
} else {
digitalWrite(ACTION, LOW); // Set the buzzer OFF
Serial.println("Nothing");
}
delay(200);
}
呢部分程式碼持續檢查霍爾感測器嘅狀態。如果感測器偵測到磁場(當 detected 係 LOW 嘅時候),佢會將蜂鳴器打開,並喺 Serial Monitor 度印出 "Detected!"。如果冇偵測到磁場,佢會將蜂鳴器關閉,並印出 "Nothing"。
示範 / 預期效果
當你執行呢段程式碼嘅時候,Arduino 會持續監察霍爾感測器有冇任何磁場。如果偵測到磁場,蜂鳴器會響起,而你會喺 Serial Monitor 度見到 "Detected!" 印出嚟。如果冇磁場存在,蜂鳴器會保持關閉,並顯示 "Nothing"。接線時要小心,避免反接,否則可能導致偵測失敗(片入面 06:15)。
影片時間標記
- 00:00 - 霍爾感測器模組簡介
- 02:30 - 接線解說
- 05:00 - 程式碼講解
- 08:15 - 示範
/*
* This is the Arduino code for a Hall Sensor module for Arduino.
// Written for Robojax.com video
* A Hall sensor switch will detect the magnetic field, and the module is used to trigger something.
* Watch the video for details: https://youtu.be/QH1Lw9BwTJI
* Code is available at: http://robojax.com/learn/arduino
*
Written by Ahmad Nejrabi for Robojax.com
on January 28, 2018 at 19:41 in Ajax, Ontario, Canada
* Permission granted to share this code, given that this
* note is kept with the code.
* Disclaimer: This code is "AS IS" and for educational purposes only.
*
*/
/*
*/
// Hall Sensor code for Robojax.com
#define DETECT 2 // pin 2 for sensor
#define ACTION 8 // pin 8 for action to do something
void setup() {
Serial.begin(9600);
Serial.println("Robojax.com Hall Module Test");
pinMode(DETECT, INPUT);//define detect input pin
pinMode(ACTION, OUTPUT);//define ACTION output pin
// Flame sensor code for Robojax.com
}
void loop() {
// Hall Sensor code for Robojax.com
int detected = digitalRead(DETECT);// read Hall sensor
int detectedAn = analogRead(A0);// read flame analog value
if( detected == LOW)
{
digitalWrite(ACTION,HIGH);// set the buzzer ON
Serial.println("Detected!");
// Serial.println(detectedAn);// print analog value
}else{
digitalWrite(ACTION,LOW); // Set the buzzer OFF
Serial.println("Nothing");
// Hall Sensor code for Robojax.com
}
delay(200);
}
你可能需要嘅嘢
-
亞馬遜
-
亞馬遜
-
阿里巴巴速卖通Buy KY-024 Linear Magnetic Hall Switch from AliExpresss.click.aliexpress.com
-
阿里巴巴速卖通Buy KY-024 Linear Magnetic Hall Switch from AliExpress (2)s.click.aliexpress.com
資源與參考資料
-
外部Hall Sensor Datasheet (PDF)elecrow.com
文件📁
数据表 (pdf)
-
SS49E_SS39ET_SS59ET_hall_sensor_honeywell_49EThe SS39ET/SS49E/SS59ET Series low-cost linear Hall-effect sensor ICs are small, versatile devices that are operated by the magnetic field from a permanent magnet or an electromagnet. They are designed and manufactured for cost competitiveness. The linear sourcing output voltage is set by the supply voltage and varies in proportion to the strength of the magnetic field. Low voltage capability as low as 2.7 Vdc and reduced current consumption of only 6 mA typically at 5 Vdc help make this product energy efficient. The integrated circuitry features low noise output, which makes it unnecessary to use external filtering. These sensor ICs Interface with many electrical components without buffering. They also include thin film resistors to provide increased temperature stability and accuracy. These linear Hall-effect sensor ICs have an operating temperature range of -40 °C to 100°C [-40 °F to 212 °F], appropriate for industrial and medical environments. Thermal balancing allows for stable operation over the full temperature range. They are available in three package styles, all of which may be supplied on tape for automated, lower-cost assembly: • SOT-23: SS39ET. This small footprint takes up less space on the PC board, typically allowing for more components. • Flat TO-92-style, with different lead configurations: SS49E, SS49E-L, SS49E-F. • SOT-89B: SS59ET.
SS49E_SS39ET_hall_sensor_honeywell_49E.pdf0.98 MB -
49E YANGZHOU Hall-effect linear hall sensorThe 49E Series Economical Linear Hall-effect sensors are small, versatile linear Hall-effect devices that are operated by the magnetic field from a permanent magnet or an electromagnet. The linear sourcing output voltage is set by the supply voltage and varies in proportion to the strength of the magnetic field. The integrated circuitry features low noise output, which makes it unnecessary to use external filtering. It also includes thin film resistors to provide increased temperature stability and accuracy. These linear Hall sensors have an operating temperature range of -40 °C to 100 °C, appropriate for commercial, consumer, and industrial environments.
49E_YANGZHOU_Hall-effect_linear_hall_sensor.pdf0.49 MB -
SS49E SS39ET Hall Sensor Honeywell 49EDatasheet for Honeywell SS49E and SS39ET linear Hall-effect sensor ICs. These sensors provide analog output proportional to magnetic flux density, operate from 4.5V to 10.5V, and are available in a small SIP package.
SS49E_SS39ET_hall_sensor_honeywell_49E.pdf0.98 MB