はじめに
土壌水分センサーをArduino Unoに接続してみました。
参考にしたページは -> ここ
購入したセンサは、 => これ
用意したもの
土壌水分センサー:(MH-Sensor Series)
マイコン:Arduino UNO
環境:Arduino IDE
接続方法
アナログポートのA0からA3に、センサーのピンを直接接続しました。
A0とA1をアナログポートとして利用しました。 A2とA3をデジタル出力に切り替えて、電源として利用します。
A0 アナログ信号(土壌水分量が増えると電圧が下がる)
A1 デジタル信号(土壌水分量の電圧の閾値を設定して、その値を下がると、電圧がLowになる)
A2 GND,デジタルポートのLOWとして設定
A3 VCC,デジタルポートのHighとして設定
プログラム
プログラム例の Basic の中から、 AnalogReadSerial.ino を選び、書き直して利用。
SoilMoistureSensor.ino
/*
SoilMoistureSensor.ino
e-lab2020 Ltd. 2019-07-03
参考にしたサンプルプログラム AnalogReadSerial
Reads an analog input on pin 0, prints the result to the Serial Monitor.
Graphical representation is available using Serial Plotter (Tools > Serial Plotter menu).
Attach the center pin of a potentiometer to pin A0, and the outside pins to +5V and ground.
This example code is in the public domain.
http://www.arduino.cc/en/Tutorial/AnalogReadSerial
*/
// the setup routine runs once when you press reset:
void setup() {
// initialize serial communication at 9600 bits per second:
Serial.begin(9600);
pinMode(A2,OUTPUT);digitalWrite(A2,LOW); //for GND
pinMode(A3,OUTPUT);digitalWrite(A3,HIGH); //for VCC
}
// the loop routine runs over and over again forever:
void loop() {
// read the input on analog pin 0:
int sensorValue = analogRead(A0);
// print out the value you read:
Serial.print(analogRead(A1));
Serial.print(",");
Serial.println(sensorValue);
delay(1); // delay in between reads for stability
}