How to connect soil moisture sensor to Arduino?

Table of contents:

How to connect soil moisture sensor to Arduino?
How to connect soil moisture sensor to Arduino?
Anonim

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 be negligent about this. As a result, by your arrival, the plants will feel bad. To prevent this from happening, you can make an automatic irrigation system. For this purpose, we need an Arduino and a soil moisture sensor. In the article, we will consider an example of connecting and working with the FC-28 sensor. He has proven himself on the positive side, with the help of thousands of projects have been created.

About FC-28

There are a great variety of sensors for determining the humidity of the earth, but the most popular is the FC-28 model. It has a low price, due to which it is widely used by all radio amateurs in their projects. Soil moisture sensor with Arduino is used. He has two probes that conduct electrical current through the ground. It turns out that if the soil is wet, then the resistance between the probes is less. With dry ground, respectively, the resistance is greater. Arduino accepts these values, compares and, if necessary, turns on, for example, a pump. The sensor is able to work with both digital and analog modes, we will consider both connection options. FC-28 is used mainly in small projects, for example, when automatically watering one particular plant, since it is inconvenient to use it on a large scale due to its size and disadvantages, which we will also consider.

Soil Moisture Sensor FC-28
Soil Moisture Sensor FC-28

Where to 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 markup is huge. Of course, you can still make a sensor for measuring soil moisture with your own hands, but more on that below.

About connection

Connecting the humidity sensor to the Arduino is very easy. It comes with a comparator and a potentiometer for adjusting the sensitivity of the sensor, as well as for setting the limit value when connected using a digital output. The output signal, as mentioned above, can be digital and analog.

Soil Moisture Sensor Pinout
Soil Moisture Sensor Pinout

Connecting with digital output

Connected in almost the same way as analog:

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

As mentioned above, a comparator and a 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. Let's say the sensor values are above the threshold, in which case the soil moisture sensor on the Arduino transmits 5V, if less - 0V. Everything is very simple, but analog mode has more accurate values, so it is recommended to use it.

Connecting using digital mode
Connecting using digital mode

The wiring diagram looks like the photo above. way

The programming 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? First, two variables were identified. The first variable - led_pin - serves to designate the LED, and the second - to designate the ground moisture sensor. Next, we declare the LED pin as an output, and the sensor pin as an input. This is necessary so that we can get 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 higher than the limit, turn on the LED, if it is lower, turn it off. Instead of an LEDmaybe a pump, it's all up to you.

Analog mode

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

  • VCC connect 5V to Arduino.
  • GND on the sensor is connected to GND on the Arduino board.
  • A0 connect to A0 on Arduino.

Next, write the code below in Arduino.


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

So what does this code do? The first step was to 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 using the sensor. Next, we read the data. In the loop, we write the values from the sensor to the output_value variable we created. Then the percentage of soil moisture is calculated, after which we display them on the port monitor. The wiring diagram is shown below.

Soil moisture sensor analog connection
Soil moisture sensor analog connection

DIY

It was discussed above how to connect the soil moisture sensor to the Arduino. The problem with these sensors is that they are short-lived. The fact is that they are very prone tocorrosion. Some companies make sensors with a special coating to increase the service life, but it's still not the same. 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 the soil moisture values. You can extend the service life if you turn it on, 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 will not feel the difference. Basically, the system is the same. Simply, instead of two sensors, you can put your own and use a material that is less susceptible to corrosion. Ideally, of course, use gold, but given its price, it will come out very expensive. In general, it is cheaper to buy, given the price of FC-28.

DIY soil moisture sensor
DIY soil moisture sensor

Pros and cons

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

Pros:

  • Price. This sensor has a very low price, so every radio amateur will be able to buy and build his own automatic watering 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 rather large amount for it.
  • Simplicity. Mastering the work with this soil moisture sensor in Arduino caneach. Just a few wires, a couple of lines of code - and that's it. Soil moisture control done.

Cons:

Recommended: