Introduction to Auto-Rain Sensor Coding Using an IR Sensor and Arduino Uno

Introduction to Auto-Rain Sensor Coding Using an IR Sensor and Arduino Uno

In this article, we will delve into the basics of implementing an automatic rain sensor in a car wiper system using an IR sensor and an Arduino Uno board. The goal is to allow the car wipers to activate based on the presence of rain rather than relying on manual control. Let's explore the process step-by-step.

Why an IR Sensor?

The Infrared (IR) sensor is a simple and reliable component used to detect the presence of objects based on the reflection of infrared light. While it's true that IR sensors can be used for various applications, in this context, we aim to detect rain. The sensor used for rain detection will not rely on IR light but rather on changes in conductivity when moisture is present. However, for simplicity and ease of use, we will utilize a digital rain sensor, which works similarly to an IR sensor in its detection mechanism.

Choosing the Right Sensor

There are two types of output available for the rain sensor - analog and digital. We recommend using a digital rain sensor due to its simplicity and reliability. With a digital rain sensor, you can directly connect it to the Arduino Uno using a digital input pin. The digital sensor will output a HIGH signal (5V) when the sensor detects moisture.

Hardware Setup

Here is a brief overview of the hardware setup:

Connect the digital rain sensor to digital pin 7 of the Arduino Uno. Connect the output pin of the relay module to digital pin 13 of the Arduino Uno. Add a pull-down resistor (10kΩ) to the sensor pin to ensure the pin remains LOW when there is no rain.

Sample Code and Explanation

Below is a basic code example to control the relay based on the rain sensor detection:

int RelayPin  13;  // Relay connected to digital pin 13
int SensorPin  7;  // Sensor connected to digital pin 7
void setup() {
  pinMode(RelayPin, OUTPUT);  // Set the RelayPin as an output
  pinMode(SensorPin, INPUT);   // Set the SensorPin as an input
  digitalWrite(RelayPin, LOW); // Turn the relay off
}
void loop() {
  if (digitalRead(SensorPin)  HIGH) {
    digitalWrite(RelayPin, HIGH); // Turn the relay on and keep it on for 30 seconds
    delay(30000); // Wait for 30 seconds
  } else {
    digitalWrite(RelayPin, LOW); // Turn the relay off
  }
}

Explanation:

Setup Section: Initializes the relay and sensor pins. It sets the relay pin to output mode and the sensor pin to input mode. Additionally, the relay is turned off at the start of the program. Loop Section: Continuously reads the state of the sensor pin. If the sensor detects moisture (State HIGH), it turns on the relay for 30 seconds. If no moisture is detected, the relay remains off.

Advancing the Code

To further enhance the functionality, you can use the millis() function in the Arduino library to perform more tasks and reduce the need for delays.

Enhanced Code Example

#include 
int RelayPin  13;  // Relay connected to digital pin 13
int SensorPin  7;  // Sensor connected to digital pin 7
long delayTime  30000; // 30 seconds
long lastTimeRelayChanged  0;
void setup() {
  pinMode(RelayPin, OUTPUT);  // Set the RelayPin as an output
  pinMode(SensorPin, INPUT);   // Set the SensorPin as an input
  digitalWrite(RelayPin, LOW); // Turn the relay off
}
void loop() {
  if (digitalRead(SensorPin)  HIGH) {
    if (millis() - lastTimeRelayChanged  delayTime) {
      digitalWrite(RelayPin, HIGH);  // Turn the relay on
      lastTimeRelayChanged  millis(); // Record the time relay was last toggled
    }
  } else {
    digitalWrite(RelayPin, LOW); // Turn the relay off
  }
}

Explanation: This code uses millis() to implement better time management. The relay remains on for the specified delay time only if the moisture detection is continuous and the relay has not been toggled in the past 30 seconds.

Conclusion

By following the steps outlined in this guide, you can successfully implement an automatic rain sensor using an IR sensor and an Arduino Uno. This project not only simplifies the process of controlling car wipers but also enhances safety and convenience. For any questions or further improvements, feel free to comment below. Happy coding!