How to connect soil moisture sensor to Arduino?

When do you go somewhere far away for a certain period of time? There is no one to water your indoor flowers, so you have to ask for help from your neighbors, who, in turn, may neglect this. As a result, the plants will feel bad for your arrival. To prevent this, you can make an automatic watering system. For this purpose we need an Arduino and a soil moisture sensor. In the article, we consider an example of connecting and working with the FC-28 sensor. He proved himself on the positive side, with the help of him thousands of projects were created.

About FC-28

There are a great many sensors for determining the moisture content of the earth, but the most popular model is the FC-28. It has a low price, due to which it is widely used by all hams in their projects. Soil moisture sensor with Arduino is used. He has two probes that conduct electric current through the earth. It turns out that if the soil is wet, then the resistance between the probes is less. With dry soil, respectively, the resistance is greater. Arduino takes these values, compares and, if necessary, turns on, for example, a pump. The sensor is capable of working with both digital mode and analog mode, we will consider both connection options. FC-28 is used mainly in small projects, for example, when automatically irrigating one particular plant, since it is inconvenient to use it on a large scale due to the size and minuses, which we will also consider.

Soil moisture sensor FC-28

Where could I buy

The fact is that in Russian stores, sensors for working with Arduino are relatively expensive. The average price for this sensor in Russia varies from 200 to 300 rubles, while in Aliexpress the same sensor costs only some 30-50. The extra charge is huge. Of course, you can still make a sensor for measuring soil moisture with your own hands, but more on that below.

About connecting

Connecting a humidity sensor to the Arduino is very easy. A comparator and a potentiometer are included with it to control the sensitivity of the sensor, as well as to set the limit value when connected using a digital output. The output signal, as mentioned above, can be digital and analog.

Soil moisture sensor pinout

Digital Output Connection

It connects in almost the same way as analog:

  • VCC - 5V on Arduino.
  • D0 - D8 on the Arduino board.
  • GND is the earth.

As mentioned above, a comparator and potentiometer are located on the sensor module. Everything works as follows: using a potentiometer, we set the limit value of our sensor. FC-28 compares the value with the limit, and then sends the value to the Arduino. Suppose the sensor value is above the threshold, in this case the soil moisture sensor on the Arduino transmits 5V, if less - 0V. Everything is very simple, but the analog mode has more accurate values, therefore it is recommended to use it.

Digital Connection

The wiring diagram looks as shown in the photo above. way

The code for Arduino when using digital mode is shown below.

int led_pin =13; int sensor_pin =8; void setup() { pinMode(led_pin, OUTPUT); pinMode(sensor_pin, INPUT); } void loop() { if(digitalRead(sensor_pin) == HIGH){ digitalWrite(led_pin, HIGH); } else { digitalWrite(led_pin, LOW); delay(1000); } } 

What does our code do? The first step was to designate two variables. The first variable, led_pin, is used to indicate the LED, and the second is to indicate the earth's humidity sensor. Next, we declare the LED contact as an output, and the sensor contact as an input. This is necessary so that we can obtain the values ​​and, if necessary, turn on the LED to visually see that the sensor values ​​are above the threshold. In the loop, we read the values ​​from the sensor. If the value is above the limit - turn on the LED, if lower - turn off. Instead of an LED there may be a pump, it all depends on you.

Analog mode

To connect using the analog output, you will need to work with A0. The capacitive soil moisture sensor in Arduino takes values ​​from 0 to 1023. We connect the sensor as follows:

  • VCC connect at 5V to Arduino.
  • Connect the GND on the sensor to the GND on the Arduino board.
  • A0 connect to A0 on Arduino.

Next, in Arduino, we write the code below.

 int sensor_pin = A0; int output_value ; void setup() { Serial.begin(9600); Serial.println(" "); delay(2000); } void loop() { output_value= analogRead(sensor_pin); output_value = map(output_value,550,0,0,100); Serial.print(" "); Serial.print(output_value); Serial.println("%"); delay(1000); } 

So what does this program code do? The first thing to do was set the variables. The first variable is needed to determine the contact of the sensor, and the other will store the results that we will receive with the sensor. Next we read the data. In the loop, we write the values ​​from the sensor to the variable output_value that we created. Then, the percentage of soil moisture is calculated, and then we display them on the port monitor. The wiring diagram is shown below.

Analogue soil moisture sensor connection

Do it yourself

It was discussed above how to connect a soil moisture sensor to an Arduino. The problem with these sensors is that they are short-lived. The fact is that they are very prone to corrosion. Some companies make sensors with a special coating to increase the service life, but this is still not the case. Also considered is the option of using the sensor not often, but only when required. For example, there is a program code where every second the sensor reads values ​​about soil moisture. You can extend the life if you include it, for example, once a day. But if this does not suit you, then you can make a soil moisture sensor with your own hands. Arduino won't feel the difference. In principle, the system is the same. Just instead of two sensors, you can put your own and use material that is less susceptible to corrosion. It is ideal, of course, to use gold, but given its price, it will come out very expensive. In general, it’s cheaper to buy, given the price of the FC-28.

DIY soil moisture sensor

Pros and cons

The article examined options for connecting a soil moisture sensor to an Arduino, and examples of program code were also presented. The FC-28 is a really good sensor for determining soil moisture, but what are the specific pros and cons of this sensor?

Pros:

  • Price. This sensor has a very low price, so each hams will be able to buy and build their own automatic irrigation system for plants. Of course, when working with large scales, this sensor will not work, but it is not intended for this. If you need a more powerful sensor - SM2802B, then you will have to pay a considerable amount for it.
  • Simplicity. Everyone can master the work with this soil moisture sensor in Arduino. Just a few wires, a couple lines of code - that's all. Soil moisture control done.

Minuses:

  • Susceptibility to corrosion. This is the only drawback of these sensors. But, considering the price, you can close your eyes to this. First of all, these sensors were made more for training than for practical use in large projects.

Source: https://habr.com/ru/post/G28655/


All Articles