[Diaoye learns programming] Arduino smart agriculture uses raindrop sensors to realize rain sensing to stop irrigation

Insert image description here
Arduino is an open source electronics prototyping platform that allows you to create a variety of interactive projects using simple hardware and software. The core of Arduino is a microcontroller board that can connect various sensors, actuators, displays and other external devices through a series of pins. Arduino programming is based on the C/C++ language. You can use the Arduino IDE (Integrated Development Environment) to write, compile and upload code to the Arduino board. Arduino also has a rich library and community that you can use to extend the functionality of Arduino and learn about Arduino.

The characteristics of Arduino are:
1. Open source: Arduino’s hardware and software are open source, and you can freely modify, copy and share them.
2. Easy to use: Arduino’s hardware and software are designed for beginners and non-professionals, you can easily get started and use them.
3. Cheap: Arduino’s hardware and software are very economical, and you can realize your ideas at a very low cost.
4. Diversity: Arduino has many models and versions. You can choose the appropriate Arduino board according to your needs and preferences.
5. Innovation: Arduino allows you to express your creativity and imagination electronically. You can use Arduino to make a variety of interesting and useful projects, such as robots, smart homes, and art. Devices etc.

Insert image description here
The main features of Arduino smart agriculture:
1. Sensor and actuator integration: Arduino smart agriculture system can integrate various sensors (such as temperature sensor, humidity sensor, soil moisture sensor, etc. ) and actuators (such as water pumps, motors, lights, etc.) to monitor and control the agricultural environment.
2. Data collection and analysis: Arduino smart agricultural system can collect agricultural environment data and conduct real-time analysis and processing. These data can be used to monitor plant growth status, soil conditions, climate changes, etc., and help farmers make appropriate decisions.
3. Remote monitoring and control: The Arduino smart agricultural system can achieve remote monitoring and control through network connection. Farmers can remotely monitor the conditions of farmland through mobile phones, computers and other devices, and perform corresponding control operations, such as remote irrigation, temperature adjustment, etc.
4. Automation and intelligence: Arduino smart agricultural system can automatically perform a series of tasks, such as automatic watering, automatic adjustment of lighting, etc., reducing farmers' labor burden and improving work efficiency. At the same time, through intelligent algorithms and decision-making models, the system can make automated decisions based on real-time data, making agricultural production more intelligent.

The core advantages of Arduino smart agriculture:
1. Low cost: Arduino is an open source hardware platform, the hardware cost is relatively low, and it is easy to obtain and use. Farmers can assemble and customize smart agricultural systems themselves according to their needs and budgets.
2. Flexibility: The Arduino platform has good scalability and compatibility and can be combined with various sensors and actuators to adapt to different agricultural environments and needs. Farmers can choose appropriate components and functions based on their actual conditions.
3. Ease of use: The Arduino platform has simple and easy-to-use programming interfaces and development tools. Even non-professional farmers or beginners can quickly get started and develop. The Arduino community provides a large number of tutorials and sample codes for easy learning and reference.

Limitations of Arduino smart agriculture:
1. Limited processing power: Arduino is a small embedded system with relatively limited processing power. For some complex agricultural applications, more powerful hardware platforms may be needed to handle large amounts of data and complex algorithms.
2. Limited network connection capabilities: Arduino usually communicates through short-distance connections such as wired or Bluetooth. For remote farmland or scenarios that require wide area network connections, additional equipment may be needed to achieve network connections.
3. Lack of standardization and supervision: Since Arduino is an open source platform, it lacks unified standards and supervision mechanisms. This may lead to compatibility issues between different systems and increase the difficulty of system maintenance and management.
4. Requires certain technical knowledge: Although the Arduino platform is relatively easy to use, for some farmers, it still requires certain electronic and programming knowledge. For farmers who lack relevant technical knowledge, additional training and support may be needed.

Insert image description here
Using raindrop sensors to implement rain sensing to stop irrigation is a common application in Arduino smart agriculture. Below I will explain in detail its main features, application scenarios and matters needing attention from a professional perspective.

main feature:

High sensitivity: The raindrop sensor can detect the presence of raindrops and output a corresponding signal. It has high sensitivity and can quickly sense rainfall conditions and provide real-time feedback to the Arduino system.

Digital output: Raindrop sensors usually have a digital output interface that can be connected directly to digital pins on the Arduino board. The sensor outputs a high level to indicate that raindrops have been detected, and a low level to indicate that no raindrops have been detected.

Adjustable sensitivity: Raindrop sensors usually have a sensitivity adjustment function that can adjust the threshold for sensing raindrops as needed. This makes the sensor more adaptable to different rainfall intensities and environmental conditions.

Simple and easy to use: Using the raindrop sensor for rain sensing to stop irrigation does not require complex circuitry or programming. By connecting the sensor to the Arduino development board and writing simple program logic, you can realize the function of automatically sensing rainwater and stopping irrigation.

Application scenarios:

Farmland irrigation: In farmland irrigation, intelligent automatic irrigation control can be achieved using raindrop sensors. When the sensor detects raindrops falling on the soil surface, the Arduino system can automatically stop irrigation according to the set logic to avoid over-irrigation and waste of water resources.

Plant growing: In indoor or outdoor plant growing environments, raindrop sensors can help control the amount of watering of plants. When the sensor detects raindrops, the irrigation of the plants can be stopped through the Arduino system to avoid the adverse effects of an overly humid environment on plant growth.

Garden Automation: In a garden or yard, automated watering control can be achieved using raindrop sensors. When the sensor detects raindrops, the work of the automatic watering device can be stopped through the Arduino system to save water and protect plants.

Things to note:

Sensor installation: Raindrop sensors should be installed where they are exposed to rainwater, such as suspended above plants or placed on the soil surface. Make sure the sensor is appropriately distanced and positioned from the irrigation area to obtain accurate raindrop detection results.

Debugging and calibration: Before starting to use, the sensor should be debugged and calibrated. According to the actual situation, adjust the sensitivity of the sensor to ensure that it can accurately detect raindrops and output the correct signal.

Power Supply: Ensure stable power supply to the sensor and Arduino system. Use a suitable power supply module or battery to ensure proper operation of the sensor and Arduino system.

System Reliability: Consider system reliability when designing and implementing a rain sensing stop irrigation system. Ensure the stability of hardware and software to avoid false positives or false negatives.

Maintenance and maintenance: Regularly check the working status of the sensor to ensure that the sensor surface is clean and free of water or dirt. At the same time, sensors are regularly calibrated to ensure their accuracy and reliability.

Summary:
Using raindrop sensors to implement rain sensing to stop irrigation is a common application in Arduino smart agriculture. Its key features include high sensitivity, digital output, adjustable sensitivity and ease of use. Applicable application scenarios include farmland irrigation, plant cultivation and garden automation. When using it, you need to pay attention to the installation, debugging and calibration of the sensor, power supply, system reliability, maintenance and upkeep, etc.

Insert image description here
Case 1: Simple implementation of stopping irrigation

const int rainSensorPin = A0; // 雨滴传感器连接到模拟引脚A0
const int irrigationPin = 8; // 灌溉系统控制引脚连接到数字引脚8

void setup() {
    
    
  pinMode(rainSensorPin, INPUT);
  pinMode(irrigationPin, OUTPUT);
}

void loop() {
    
    
  int moisture = analogRead(rainSensorPin);

  if (moisture < 500) {
    
    
    digitalWrite(irrigationPin, HIGH); // 停止灌溉
  } else {
    
    
    digitalWrite(irrigationPin, LOW); // 启动灌溉
  }

  delay(1000);
}

Interpretation:
In the setup() function, set the raindrop sensor pin to input mode and the irrigation system control pin to output mode.
In the loop() function, read the analog value of the raindrop sensor.
If the humidity value of the sensor is lower than the threshold (500), set the control pin of the irrigation system to high level and stop irrigation; otherwise, set the control pin to low level, Start irrigation.

Case 2: Add delay protection mechanism

const int rainSensorPin = A0; // 雨滴传感器连接到模拟引脚A0
const int irrigationPin = 8; // 灌溉系统控制引脚连接到数字引脚8
const int irrigationDuration = 60000; // 灌溉持续时间(毫秒)
const int delayAfterRain = 300000; // 雨水感应后的延时时间(毫秒)

bool isRaining = false;
unsigned long lastRainTime = 0;

void setup() {
    
    
  pinMode(rainSensorPin, INPUT);
  pinMode(irrigationPin, OUTPUT);
}

void loop() {
    
    
  int moisture = analogRead(rainSensorPin);

  if (moisture < 500) {
    
    
    if (!isRaining) {
    
    
      isRaining = true;
      lastRainTime = millis();
    }

    if (millis() - lastRainTime > irrigationDuration) {
    
    
      digitalWrite(irrigationPin, HIGH); // 停止灌溉
    }
  } else {
    
    
    isRaining = false;
    digitalWrite(irrigationPin, LOW); // 启动灌溉
  }

  delay(1000);
}

Interpretation:
In the setup() function, set the raindrop sensor pin to input mode and the irrigation system control pin to output mode.
In the loop() function, read the analog value of the raindrop sensor.
If the sensor's humidity value is below the threshold (500), it means it is raining. If no rain has been detected before (isRaining is false), isRaining is set to true and the current time (lastRainTime) is recorded.
If the time interval between the current time and the last time rain was detected exceeds the irrigation duration (irrigationDuration), the control pin of the irrigation system is set to high level to stop irrigation.
If the sensor's humidity value is above the threshold, indicating that it is not raining, set isRaining to false and set the control pin of the irrigation system to low to start irrigation.

Case 3: Using the anti-shake function

const int rainSensorPin = 2; // 雨滴传感器连接到数字引脚2
const int irrigationPin = 8; // 灌溉系统控制引脚连接到数字引脚8
const int debounceDelay = 50; // 防抖void setup() {
    
    
  pinMode(rainSensorPin, INPUT);
  pinMode(irrigationPin, OUTPUT);
}

void loop() {
    
    
  static bool lastState = HIGH;
  bool currentState = digitalRead(rainSensorPin);

  if (currentState != lastState) {
    
    
    delay(debounceDelay);
    currentState = digitalRead(rainSensorPin);
  }

  if (currentState == LOW) {
    
    
    digitalWrite(irrigationPin, HIGH); // 停止灌溉
  } else {
    
    
    digitalWrite(irrigationPin, LOW); // 启动灌溉
  }

  lastState = currentState;

  delay(1000);
}

Interpretation:
In the setup() function, set the raindrop sensor pin to input mode and the irrigation system control pin to output mode.
In the loop() function, anti-shake technology is used to eliminate interference when reading the sensor.
Use a static variable lastState to save the last sensor state.
At the beginning of each cycle, the current sensor status is read and compared with the previous status. If different, an anti-shake delay is performed.
If the sensor status is low level, it means it is raining. Set the control pin of the irrigation system to high level to stop irrigation; otherwise, set the control pin to low level. Start irrigation.
These case codes provide the basic functionality of using a raindrop sensor in Arduino to implement rain sensing to stop irrigation. Based on the humidity value read by the sensor, it can be determined whether it is raining, thereby controlling the start and stop of the irrigation system. Adding delay protection mechanism and anti-shake function can help improve the sensitivity and stability of the system. According to specific needs, parameters such as threshold, delay time and anti-shake delay can be adjusted. These codes can serve as a basis for further expansion and optimization based on specific needs.

Insert image description here
Case 4: Basic Rain Sensing Stop Irrigation Program:

#define SENSOR_PIN 2
#define PUMP_PIN 3

void setup() {
    
    
  pinMode(PUMP_PIN, OUTPUT);
  digitalWrite(PUMP_PIN, HIGH);  // 初始状态为停止灌溉
  Serial.begin(9600);
}

void loop() {
    
    
  int sensorValue = digitalRead(SENSOR_PIN);

  if (sensorValue == LOW) {
    
    
    Serial.println("Rain detected! Stopping irrigation.");
    digitalWrite(PUMP_PIN, HIGH);  // 停止灌溉
  } else {
    
    
    Serial.println("No rain detected. Continuing irrigation.");
    digitalWrite(PUMP_PIN, LOW);  // 继续灌溉
  }

  delay(1000);
}

This program uses digital pin 2 to connect the rain sensor and digital pin 3 to the water pump. In the setup() function, set the water pump pin to output mode and set its initial state to stop irrigation. Initialize the monitor via serial communication. In the loop() function, use the digitalRead() function to read the status of the raindrop sensor. If the sensor detects rain (sensor output is low level), a message to stop irrigation is output through the serial port, and the water pump pin is set to high level to stop irrigation. On the contrary, if no rainwater is detected, a message to continue irrigation will be output through the serial port, and the water pump pin will be set to low level to continue irrigation. The program will update the sensor status every 1 second.

Case 5: Threshold-based rain sensing to stop irrigation program:

#define SENSOR_PIN 2
#define PUMP_PIN 3
#define THRESHOLD 500

void setup() {
    
    
  pinMode(PUMP_PIN, OUTPUT);
  digitalWrite(PUMP_PIN, HIGH);  // 初始状态为停止灌溉
  Serial.begin(9600);
}

void loop() {
    
    
  int sensorValue = analogRead(SENSOR_PIN);

  if (sensorValue > THRESHOLD) {
    
    
    Serial.println("Rain detected! Stopping irrigation.");
    digitalWrite(PUMP_PIN, HIGH);  // 停止灌溉
  } else {
    
    
    Serial.println("No rain detected. Continuing irrigation.");
    digitalWrite(PUMP_PIN, LOW);  // 继续灌溉
  }

  delay(1000);
}

This program uses digital pin 2 to connect the rain sensor and digital pin 3 to the water pump. In the setup() function, set the water pump pin to output mode and set its initial state to stop irrigation. Initialize the monitor via serial communication. In the loop() function, use the analogRead() function to read the analog value of the raindrop sensor. If the sensor reading exceeds the preset threshold (THRESHOLD), a message to stop irrigation is output through the serial port, and the water pump pin is set to high level to stop irrigation. On the contrary, if the reading is lower than the threshold, a message to continue irrigation will be output through the serial port, and the water pump pin will be set to low level to continue irrigation. The program will update the sensor readings every 1 second.

Case 6: Delayed restoration of irrigation program:

#define SENSOR_PIN 2
#define PUMP_PIN 3
#define THRESHOLD 500
#define RECOVERY_TIME 60000  // 停止灌溉后的恢复时间(60秒)

bool isIrrigationStopped = false;
unsigned long stopTime = 0;

void setup() {
    
    
  pinMode(PUMP_PIN, OUTPUT);
  digitalWrite(PUMP_PIN, HIGH);  // 初始状态为停止灌溉
  Serial.begin(9600);
}

void loop() {
    
    
  int sensorValue = analogRead(SENSOR_PIN);

  if (!isIrrigationStopped && sensorValue > THRESHOLD) {
    
    
    Serial.println("Rain detected! Stopping irrigation.");
    digitalWrite(PUMP_PIN, HIGH);  // 停止灌溉
    isIrrigationStopped = true;
    stopTime = millis();
  }

  if (isIrrigationStopped && (millis() > stopTime + RECOVERY_TIME)) {
    
    
    Serial.println("Resuming irrigation.");
    digitalWrite(PUMP_PIN, LOW);  // 恢复灌溉
    isIrrigationStopped = false;
  }

  delay(1000);
}

This program uses digital pin 2 to connect the rain sensor and digital pin 3 to the water pump. In the setup() function, set the water pump pin to output mode and set its initial state to stop irrigation. Initialize the monitor via serial communication. In the loop() function, use the analogRead() function to read the analog value of the raindrop sensor. If irrigation is not currently stopped (isIrrigationStopped is false) and the sensor reading exceeds the preset threshold (THRESHOLD), a message to stop irrigation is output through the serial port, and the water pump pin is set to high level to stop irrigation. At the same time, set the isIrrigationStopped flag to true and record the time stopTime when irrigation is stopped. Then, if irrigation has been stopped (isIrrigationStopped is true) and the current time is greater than the time when irrigation was stopped plus the recovery time (RECOVERY_TIME), a message to resume irrigation is output through the serial port, and the water pump pin is set to low level to resume irrigation. At the same time, set the isIrrigationStopped flag to false. The program will update the sensor readings every 1 second and perform corresponding judgment and control operations to achieve the function of delayed restoration of irrigation.

These program examples provide basic rain sensing stop irrigation functionality. By reading the status of the raindrop sensor, you can determine whether to stop irrigation based on the threshold and control the switch of the water pump. In the sixth case, a delayed resumption of irrigation function was also added to avoid resuming irrigation immediately after the rain stops. You can expand these programs according to actual needs, adding more sensors, data processing and decision-making logic to meet specific smart agriculture application scenarios.

Note that the above cases are only for expansion of ideas and are for reference only. They may have errors, be inapplicable, or fail to compile. Your hardware platform, usage scenario, and Arduino version may affect the choice of usage method. When actually programming, you need to make adjustments based on your own hardware configuration, usage scenarios, and specific needs, and conduct multiple actual tests. You'll also want to connect the hardware correctly and understand the specifications and characteristics of the sensors and devices you use. For codes involving hardware operations, you must confirm the correctness and safety of parameters such as pins and levels before use.

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_41659040/article/details/135002687