Arduino - 水位檢測器/感測器



水感測器模組專為水位檢測而設計,可廣泛用於感知降雨、水位,甚至液體洩漏。

Water Detector / Sensor

將水感測器連線到 Arduino 是檢測洩漏、溢位、洪水、雨水等的好方法。它可以用來檢測水的存在、水位、體積和/或水的缺失。雖然這可以用來提醒你給植物澆水,但有一個更好的 Grove 感測器可以做到這一點。該感測器有一組裸露的焊盤,當檢測到水時會讀取為低電平。

在本章中,我們將把水感測器連線到 Arduino 的數字引腳 8,並將使用非常方便的 LED 來幫助識別水感測器何時與水源接觸。

所需元件

您將需要以下元件 -

  • 1 × 麵包板
  • 1 × Arduino Uno R3
  • 1 × 水感測器
  • 1 × LED
  • 1 × 330 歐姆電阻

步驟

按照電路圖,將元件連線到麵包板上,如下圖所示。

Water Sensor Circuit Connection

草圖

在您的電腦上開啟 Arduino IDE 軟體。使用 Arduino 語言進行編碼將控制您的電路。透過點選“新建”開啟一個新的草圖檔案。

Sketch

Arduino 程式碼

#define Grove_Water_Sensor 8 // Attach Water sensor to Arduino Digital Pin 8
#define LED 9 // Attach an LED to Digital Pin 9 (or use onboard LED)

void setup() {
   pinMode(Grove_Water_Sensor, INPUT); // The Water Sensor is an Input
   pinMode(LED, OUTPUT); // The LED is an Output
}

void loop() {
   /* The water sensor will switch LOW when water is detected.
   Get the Arduino to illuminate the LED and activate the buzzer
   when water is detected, and switch both off when no water is present */
   if( digitalRead(Grove_Water_Sensor) == LOW) {
      digitalWrite(LED,HIGH);
   }else {
      digitalWrite(LED,LOW);
   }
}

程式碼說明

水感測器有三個端子 - S、Vout(+) 和 GND (-)。連線感測器如下 -

  • 將 +Vs 連線到 Arduino 板上的 +5v。
  • 將 S 連線到 Arduino 板上的數字引腳 8。
  • 將 GND 連線到 Arduino 上的 GND。
  • 將 LED 連線到 Arduino 板上的數字引腳 9。

當感測器檢測到水時,Arduino 上的引腳 8 變為低電平,然後 Arduino 上的 LED 就會亮起。

結果

當感測器檢測到水時,您將看到指示 LED 亮起。

廣告