

This circuit is designed to control a bipolar stepper motor using an Arduino Nano microcontroller and an A4988 stepper motor driver. The system also includes a rain sensor that, when detecting rain, triggers the stepper motor to move to a sheltered position. The circuit is powered by a 12V battery, which supplies power to the motor driver and, through voltage regulation, to the Arduino Nano and the rain sensor.
GND connected to the ground of the rain sensor and the A4988 stepper motor driverD2 connected to the STEP pin of the A4988 stepper motor driverD3 connected to the DIR pin of the A4988 stepper motor driverD4 connected to the ENABLE pin of the A4988 stepper motor driver5V provides power to the VDD of the A4988 stepper motor driver and VCC of the rain sensorA0 reads the analog output from the rain sensorAO connected to A0 on the Arduino NanoDO not connectedGRD connected to GND on the Arduino NanoVCC connected to 5V on the Arduino NanoD connected to 2B on the A4988 stepper motor driverB connected to 1B on the A4988 stepper motor driverC connected to 2A on the A4988 stepper motor driverA connected to 1A on the A4988 stepper motor driverENABLE, MS1, MS2, MS3, RESET, SLEEP not connected or configured for basic operationSTEP connected to D2 on the Arduino NanoDIR connected to D3 on the Arduino NanoVMOT connected to + on the battery 12VGND connected to - on the battery 12V and GND on the Arduino Nano2B, 2A, 1A, 1B connected to the respective wires of the stepper motorVDD connected to 5V on the Arduino Nano+ connected to VMOT on the A4988 stepper motor driver- connected to GND on the A4988 stepper motor driver#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 is designed to be uploaded to the Arduino Nano. It initializes the stepper motor and rain sensor, then continuously checks the rain sensor's output. If rain is detected, the stepper motor moves to a predefined position. If no rain is detected, it moves back to the starting position. The AccelStepper library is used for smooth acceleration and deceleration of the stepper motor.