Arduino - 水檢測器/感測器

水感測器磚是專為水質檢測而設計,可廣泛用於感應降雨,水位,甚至液體洩漏。

水檢測器/感測器

將水感測器連線到 Arduino 是檢測洩漏、溢位、洪水、雨水等的好方法。它可用於檢測水的存在,水平,體積和是否缺水。雖然這可以用來提醒你給植物澆水,但有一個更好的 Grove 感測器。感測器有一系列暴露的走線,當檢測到水時讀數為低。

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

需要的元件

你將需要以下元件 -

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

程式

按照電路圖並連線面包板上的元件,如下圖所示。

水感測器電路連線

草圖

在你的計算機上開啟 Arduino IDE 軟體。用 Arduino 語言編碼將控制你的電路。單擊“新建”開啟新的草圖檔案。

草圖

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,V out (+)和 GND(-)。按如下方式連線感測器 -

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

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

結果

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