---
# System prepended metadata

title: Arduino 教學 9：線性霍爾磁力感測器
tags: [Physics, Arduino]

---

# Arduino 教學 9：線性霍爾磁力感測器

> 作者：王一哲
> 日期：2020/11/25

## 元件基本資料

KY-024 是一種常見的線性霍爾磁力感測器，售價大約90元。測量原理是利用外加磁場使晶片中的載子偏轉，使晶片於電流、磁場同時垂直的方向上產生電壓。但是 KY-024 只能測量磁場強度的相對值，無法精確地測量磁場強度的絕對值，因此在使用上比較適合作為開關，或是利用磁場量值隨時間的變化測量時間間隔。從下方的照片中可以看到由上至下的接腳功能分別為**類比訊號**、**接地**、**電源**、**數位訊號**，分別將它們接到 Arduino 開發板上的 **類比輸入**、**GND**、**5V**、**數位輸入**。

<img style="display: block; margin-left: auto; margin-right: auto" height="60%" width="60%" src="https://i.imgur.com/s6b308N.jpg">
<div style="text-align:center">KY-024 照片</div>
<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="60%" width="60%" src="https://imgur.com/l4zn3wH.png">
<div style="text-align:center">KY-024 電路圖</div>
<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="60%" width="60%" src="https://imgur.com/QWee7yA.jpg">
<div style="text-align:center">KY-024 裝置照片</div>
<br />

## 測試用程式碼

```arduino=
#define ANALOG A0
#define DIGITAL 3

int analog, digital;

void setup() {
    pinMode(ANALOG, INPUT);
    pinMode(DIGITAL, INPUT);
    Serial.begin(9600);
    Serial.println("t (ms), analog, digital");
}

void loop() {
    analog = analogRead(ANALOG);
    digital = digitalRead(DIGITAL);
    Serial.print(millis());
    Serial.print(", ");
    Serial.print(analog);
    Serial.print(", ");
    Serial.println(digital);
    delay(100);
}
```
<br />

測試結果如下：

1. 沒有磁鐵靠近時，類比輸入讀取數值約為530，數位輸入讀取值為0。
2. 磁鐵N極靠近晶片正面或S極靠近晶片背面，類比輸入讀取數值約為870，數位輸入讀取值為0。
3. 磁鐵N極靠近晶片背面或S極靠近晶片正面，類比輸入讀取數值約為435，數位輸入讀取值為1。

<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="60%" width="60%" src="https://imgur.com/pk53Npw.jpg">
<div style="text-align:center">磁鐵 N 極靠近 KY-024 晶片正面</div>
<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="60%" width="60%" src="https://imgur.com/FJJJIfC.jpg">
<div style="text-align:center">磁鐵 N 極靠近 KY-024 晶片背面</div>
<br />

## 測量複擺週期

將強力磁鐵吸附在鐵尺的下方，再將鐵尺懸掛起來，將鐵尺向右拉到某個角度後由靜止釋放。當鐵尺擺動到最低點時，下方磁鐵的 N 極會接近磁力感測器晶片的背面，使類比輸入讀取到的數值降低到 430 左右，從數值 - 時間的資料及關係圖中可以看出鐵尺擺動到最低點的時間間隔，這應該是鐵尺擺動的半個週期，大約為 374 ms。為了盡量縮短測量的時間間隔，實驗時會將程式碼中最後一行的 delay(100) 註解掉，但理論上磁場變化影響到感測器輸出數值需要一些時間，這部分還需要再研究看看。

<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="40%" width="40%" src="https://imgur.com/rAOhKXk.jpg">
<div style="text-align:center">測量複擺週期裝置照片</div>
<br />

<img style="display: block; margin-left: auto; margin-right: auto" height="75%" width="75%" src="https://imgur.com/ug2MGo9.png">
<div style="text-align:center">測量複擺週期實驗結果</div>
<br />

## 參考資料

KY-024 線性霍爾磁力感測器資料表：https://arduinomodules.info/ky-024-linear-magnetic-hall-module

---

###### tags:`Arduino`、`Physics`