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

Arduino Nano Rain-Sensing Stepper Motor System with A4988 Driver

View and Edit Project In Cirkit DesignerImage of Arduino Nano Rain-Sensing Stepper Motor System with A4988 Driver

Circuit Documentation

Summary

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.

Component List

  1. Arduino Nano

    • Description: A small, complete, and breadboard-friendly board based on the ATmega328P.
    • Pins: D1/TX, D0/RX, RESET, GND, D2, D3, D4, D5, D6, D7, D8, D9, D10, D11/MOSI, D12/MISO, VIN, 5V, A7, A6, A5, A4, A3, A2, A1, A0, AREF, 3V3, D13/SCK
  2. RAIN SENSOR

    • Description: A sensor module used to detect rain.
    • Pins: AO, DO, GRD, VCC
  3. Stepper Motor (Bipolar)

    • Description: A bipolar stepper motor used for precise control of rotational position.
    • Pins: D, B, C, A
  4. A4988 Stepper Motor Driver (Red)

    • Description: A microstepping driver for controlling bipolar stepper motors.
    • Pins: ENABLE, MS1, MS2, MS3, RESET, SLEEP, STEP, DIR, VMOT, GND, 2B, 2A, 1A, 1B, VDD
  5. Battery 12V

    • Description: A 12V battery used to power the stepper motor driver and motor.
    • Pins: +, -

Wiring Details

Arduino Nano

RAIN SENSOR

Stepper Motor (Bipolar)

A4988 Stepper Motor Driver (Red)

Battery 12V

Code Documentation

Arduino Nano Code

#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.