This circuit is designed to detect rain using a rain sensor and control a stepper motor to move a rack to a sheltered area when rain is detected. The system is built around an Arduino Nano microcontroller, which reads the rain sensor data and controls the stepper motor via an A4988 stepper motor driver. The circuit is powered by a 12V battery.
Arduino Nano
RAIN SENSOR
Stepper Motor (Bipolar)
A4988 Stepper Motor Driver (Red)
Battery 12V
#include <AccelStepper.h>
const int rainSensorPin = A0; // Pin connected to the rain sensor
const int threshold = 500; // Rain detection threshold value
const int stepsPerRevolution = 200; // Steps per revolution of the stepper motor
const int stepsFor5Meter = 10000; // Adjust this based on your motor and mechanism
AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // Pins connected to step and direction
void setup() {
pinMode(rainSensorPin, INPUT);
stepper.setMaxSpeed(1000); // Set maximum speed of the motor
stepper.setAcceleration(500); // Set acceleration for smooth motion
}
void loop() {
int sensorValue = analogRead(rainSensorPin); // Read the rain sensor
if (sensorValue > threshold) {
// If rain is detected, move the rack to the sheltered area
if (stepper.currentPosition() != stepsFor5Meter) {
stepper.moveTo(stepsFor5Meter); // Move forward to the sheltered area
}
} else {
// If no rain, retract the rack
if (stepper.currentPosition() != 0) {
stepper.moveTo(0); // Move back to the original position
}
}
stepper.run(); // Moves the motor to the target position
delay(1000); // Delay to reduce noise and avoid false triggering
}
This code initializes the rain sensor and stepper motor driver, reads the rain sensor value, and moves the stepper motor to a sheltered area if rain is detected. If no rain is detected, the stepper motor retracts the rack to its original position.