Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Arduino-Based Rain-Sensing Motor Controller with L298N Driver

View and Edit Project In Cirkit DesignerImage of Arduino-Based Rain-Sensing Motor Controller with L298N Driver

Circuit Documentation

Summary

This circuit is designed to control a DC motor based on the input from a rain sensor. The system uses an Arduino 101 microcontroller to read the rain sensor's output and drive the motor through an L298N motor driver. The motor will rotate forward when rain is detected and reverse when the rain stops. Power is supplied by a 12V battery, regulated to 5V using a 7805 voltage regulator.

Component List

Arduino 101

RAIN SENSOR

L298N DC Motor Driver

7805 Voltage Regulator

12V Battery (Small Size)

DC Mini Metal Gear Motor

Wiring Details

Arduino 101

RAIN SENSOR

L298N DC Motor Driver

7805 Voltage Regulator

12V Battery (Small Size)

DC Mini Metal Gear Motor

Code Documentation

// Define the pins
const int rainSensorPin = A0;     // Pin connected to the rain sensor
const int motorForwardPin = 3;   // Pin connected to the motor for forward rotation
const int motorReversePin = 4;   // Pin connected to the motor for reverse rotation

// Variables to track the state of the sensor and motor
bool previousSensorState = LOW;  // Stores the previous state of the sensor

void setup() {
  pinMode(rainSensorPin, INPUT);
  pinMode(motorForwardPin, OUTPUT);
  pinMode(motorReversePin, OUTPUT);
  
  // Ensure motor is off at the start
  digitalWrite(motorForwardPin, LOW);
  digitalWrite(motorReversePin, LOW);
}

void loop() {
  int currentSensorState = digitalRead(rainSensorPin);

  if (currentSensorState == HIGH && previousSensorState == LOW) {
    // Rain starts: sensor goes from LOW to HIGH
    digitalWrite(motorForwardPin, HIGH);  // Turn motor forward
    delay(2000); // Run motor forward for 2 seconds
    digitalWrite(motorForwardPin, LOW);   // Stop the motor
  } 
  else if (currentSensorState == LOW && previousSensorState == HIGH) {
    // Rain stops: sensor goes from HIGH to LOW
    digitalWrite(motorReversePin, HIGH);  // Turn motor in reverse
    delay(2000); // Run motor in reverse for 2 seconds
    digitalWrite(motorReversePin, LOW);   // Stop the motor
  }

  // Update the previous sensor state
  previousSensorState = currentSensorState;
}

This code reads the state of the rain sensor and controls the motor accordingly. When rain is detected, the motor runs forward for 2 seconds. When the rain stops, the motor runs in reverse for 2 seconds. The motor is controlled using the L298N motor driver, and the rain sensor is read through the analog pin A0 on the Arduino 101.