No. 17 – Moisture sensor

Task: We want to measure moisture and display the read out value on the serial monitor.

Required equipment: Arduino / moisture sensor / cables

Feu1

As the name suggests, the moisture sensor is able to measure moisture. That means that it can measure the directly adjacent moisture, like Skin moisture or ground humidity, but not the air moisture. One example of use can be the moisture sensor used to measure the
ground humidity of a plant. If the ground of the plant gets to dry, an alarm could set off or an automatic water pump system could water the plant. The moisture sensor is also suitable for measuring a water level, in the region of the sensor.
The way of functioning is quite simple. There is a voltage on the two contacts of the moisture sensor. The higher the level of moisture gets between the contacts, the better the currant can flow from one contact to the other. This value gets electronically processed from the moisture sensor and gets transmitted as an analog signal to an analog input of the board. Since the board, as described in previous tutorials, isn’t able to measure electrical voltage as such, it converts the analog signal into a numerical value. 0V to 12V corresponds to a numerical value from 0 to 1023 (That are 1024 numbers, since the zero is counted as the first numerical value).

But the upper level of the moisture sensor is around 800, if the sensor gets completely under water. The accurate calibration depends on the sensor and the type of liquid/ moisture that is measured (e.g. salt water has a better conductivity so the value would be higher).

Setup:

Feu2

The programming of the moisture sensor isn’t that complicated and is very similar to the programming of the potentiometer, because there is just an analog value read out.

Code:


int measurement=0; //Variable for the measurement with 0 as starting value

void setup()

{ //The setup begins here

Serial.begin(9600); //Starting the serial communication to show the sensor values on the serial monitor //later on

}

void loop()

{ //The loop part starts here

measurement=analogRead(A0); //The voltage on the moisture sensor gets read out and gets saved under //“measurement”

Serial.print(Moisture measurement:); //Show the words “Moisture measurement:” on serial monitor and …

Serial.println(measurement); //…show the read out moisture sensor value

delay(500); //Short break to avoid to many confusing values on serial monitor

}


Extension of the code:

Now we want a piezo speaker to beep if the value gets under a certain limit.

Here we are going to define “200” as limit.


int measurement=0;

int beep=6; //”beep” stands for the pin 6, which gets connected with the piezo

void setup()

{

Serial.begin(9600);

pinMode (6,OUTPUT); //The pin 6 gets defined as an output

}

void loop()

{

measurement=analogRead(A0);

Serial.print(Moisture measurement:“);

Serial.println(measurement);

delay(500);

if (measurement <200) //Starting IF command: If the sensor value gets under “200”…

{

digitalWrite(beep, HIGH); //…the piezo should beep…

}

else //..if not…

{

digitalWrite(beep, LOW); //…the piezo should be quiet

}

}