Arduino Nano Rain-Sensing Stepper Motor System with A4988 Driver

Arduino Nano Rain-Sensing Stepper Motor System with A4988 & NEMA 17 | MakeMindz
🌧️ Smart Automation · Weather-Responsive

Arduino Nano
Rain-Sensing Stepper Motor System

Automatically closes windows, covers, or shelters when rain is detected. Precise NEMA 17 stepper control via A4988 driver with smooth AccelStepper acceleration.

Live System States
NEMA 17
via A4988 Driver
🌧️
Rain Detected
Motor → Close (10000 steps)
☀️
No Rain
Motor → Return (0 steps)

What This System Does

The Arduino Nano Rain-Sensing Stepper Motor System is a smart weather-responsive automation project. A rain sensor module continuously monitors rainfall. When water droplets are detected, the Arduino Nano drives a NEMA 17 stepper motor via an A4988 microstepping driver to close a window, cover, or retractable rack — and automatically reopens it when the rain stops.

The system uses the AccelStepper library for smooth trapezoidal motion profiles, protecting both the mechanism and the motor from sudden jerks, making it suitable for real-world deployments like greenhouses, clotheslines, and solar panel covers.

💡
Non-blocking design: stepper.run() called every loop is non-blocking — the motor continuously smooths toward its target while the sensor can be re-read, preventing the system from freezing mid-motion.

Key Components

🔵
Arduino Nano
ATmega328P, analog + digital
🌧️
Rain Sensor Module
Analog out → A0, 5V powered
A4988 Stepper Driver
STEP/DIR, up to 1/16 microstep
⚙️
NEMA 17 Stepper
200 steps/rev, 12V operation
🔋
12V External PSU
Powers VMOT on A4988
🔌
Breadboard + Wires
Jumper male-to-male
🔆
100µF Capacitor
VMOT filter — essential!

How the System Works

1

Rain Sensor Detects Water Droplets

The rain sensor PCB has conductive traces exposed to rainfall. Water bridges these traces and changes resistance, producing a variable analog voltage (0–1023) on the AO pin connected to A0.

2

Arduino Reads & Compares Against Threshold

The Nano reads analogRead(A0) each second. The raw value is compared to the threshold (default 500). Values above 500 indicate rain; values below indicate dry conditions.

3

Rain Detected → Motor Closes Cover

When rain is detected, stepper.moveTo(10000) is called. The A4988 receives STEP pulses on pin D2 and direction on D3. The NEMA 17 rotates precisely 50 revolutions (10000 ÷ 200) to close the cover with smooth acceleration.

4

No Rain → Motor Returns to Home

When rain stops, stepper.moveTo(0) is called. The motor reverses direction and smoothly returns to its home position at step 0, reopening the window or cover automatically.

Rain Sensor Analog Value Range (0–1023)
0 (Soaked)1023 (Bone Dry)
🌧️ Rain Detected
Value > 500 → moveTo(10000)
☀️ Dry / No Rain
Value ≤ 500 → moveTo(0)
⚙️
Calibration: The threshold of 500 may need adjustment depending on your sensor sensitivity and environment. Open Serial Monitor and Serial.println(sensorValue) while placing water droplets on the sensor to find your ideal threshold value.

Why Use the A4988 Driver?

The A4988 sits between the Arduino and NEMA 17, handling the high current the motor needs (up to 2A per phase) while protecting the Nano's 40mA GPIO pins. It also provides microstepping for smoother, quieter motion.

🔬
Microstepping
Up to 1/16 step resolution for smooth, quiet motion
🎚️
Adjustable Current Limit
Vref potentiometer protects motor coils
🌡️
Thermal Shutdown
Auto-disables on overheating — add heatsink!
💪
Better Torque Control
Holds position precisely when stopped
🛡️
Arduino Protection
Isolates 12V motor power from 5V Nano logic
🔄
Simple STEP/DIR Interface
Just 2 pins to control full motor operation

Wiring Diagram & Pin Connections

ARDUINO NANO A0 Rain D2 STEP D3 DIR 5V GND RAIN SENSOR VCC · GND · AO Analog Signal → A0 A4988 DRIVER STEP ← DIR ← VDD ← 5V GND VMOT← 12V GND (pwr) ENABLE→GND Vref NEMA 17 200 steps 1A–2A 12V recommended Motor coils (4 wires) 12V POWER SUPPLY 12V DC 100µF cap Common GND → LEGEND Rain sensor signal (AO→A0) STEP / DIR to A4988 12V motor power 5V / GND (logic)

Wiring: Arduino Nano → Rain Sensor (A0) · A4988 STEP/DIR (D2/D3) · NEMA 17 · 12V External Supply

Rain Sensor → Arduino Nano

Rain Sensor PinArduino Nano PinNotes
VCC5VPower from Nano
GNDGNDCommon ground
AO (Analog Out)A00–1023 analog value

A4988 Driver → Arduino Nano

A4988 PinConnected ToNotes
STEPD2 (Arduino)Step pulses from AccelStepper
DIRD3 (Arduino)Direction signal
ENABLEGNDPull LOW to enable driver always
VDD5V (Arduino)Logic power for A4988
GND (logic)GNDCommon ground
VMOT12V External PSU +Motor power — do NOT use Arduino 5V
GND (motor)12V PSU − + Arduino GNDShared common ground
1A,1B,2A,2BNEMA 17 motor coilsCheck coil pairs with multimeter
⚠️
Critical: Always connect a 100µF electrolytic capacitor across VMOT and GND close to the A4988. Without it, back-EMF from the motor can create voltage spikes exceeding 35V that permanently destroy the driver. Also set Vref = (I_max × 8 × Rsense) before running the motor.

Step-by-Step Instructions

1

Install the AccelStepper Library

In Arduino IDE: Sketch → Include Library → Manage Libraries. Search for AccelStepper by Mike McCauley and install it. This library provides smooth acceleration/deceleration profiles essential for this project.

2

Set the A4988 Current Limit (Vref)

Before connecting the motor, set the A4988 Vref potentiometer. Formula: Vref = I_max × 8 × 0.1Ω. For a 1A NEMA 17, set Vref ≈ 0.8V. Measure between the Vref pin and GND with a multimeter while adjusting the pot. This prevents motor overheating.

3

Wire the Rain Sensor

Connect the rain sensor module's VCC → Nano 5V, GND → Nano GND, and AO → Nano A0. The sensor PCB should face upward and be placed where it will be exposed to rainfall. The comparator module (DO pin) is not used in this analog version.

4

Wire the A4988 Driver

Connect STEP → D2, DIR → D3, ENABLE → GND (always enabled), VDD → 5V, GND → common GND. Do this with the power fully off. Add the 100µF capacitor across VMOT/GND before connecting the 12V supply.

5

Connect the NEMA 17 Motor

Identify the two coil pairs using a multimeter (resistance between paired wires ≈ 1–5Ω; different coils read open). Connect coil 1 to A4988 pins 1A/1B and coil 2 to 2A/2B. Incorrect pairing just reverses direction — it won't damage the motor.

6

Connect the 12V External Power Supply

Connect 12V PSU positive to A4988 VMOT through the 100µF capacitor. Connect PSU negative to a common GND rail shared with the Arduino Nano. Power on the PSU, then the Arduino separately — never connect motor power without the capacitor.

7

Calibrate the Rain Threshold

Uncomment Serial.println(sensorValue) in the code and upload. Open Serial Monitor at 9600 baud. Observe values with the sensor dry, then apply a few drops of water. Set the threshold between the dry and wet readings in the code.

8

Adjust Steps for Your Mechanism

Modify stepsFor5Meter (default 10000) to match your mechanism's travel distance. 200 steps = 1 full revolution. If using a leadscrew with 2mm pitch, 1000 steps ≈ 10mm of linear travel. Test with low speed first.

Complete Arduino Code

Uses the AccelStepper library for smooth motion. Install it via Library Manager before uploading:

🌧️ rain_stepper.ino — Arduino Nano + AccelStepper
/*
 * Arduino Nano Rain-Sensing Stepper Motor System
 * MakeMindz.com | makemindz.com/arduino-nano-rain-sensing-stepper-motor
 *
 * Hardware:
 *   Rain sensor AO  → A0 (analog input)
 *   A4988 STEP      → D2
 *   A4988 DIR       → D3
 *   A4988 ENABLE    → GND (always enabled)
 *   A4988 VMOT      → 12V external supply
 *   100µF cap across VMOT/GND (essential!)
 *
 * Library required: AccelStepper by Mike McCauley
 *   Install via: Sketch → Include Library → Manage Libraries
 */

#include <AccelStepper.h>

// ── Rain Sensor Configuration ─────────────────────
const int rainSensorPin = A0;    // Analog output of rain sensor module
const int THRESHOLD     = 500;   // Rain if value > threshold; calibrate for your sensor

// ── Stepper Motor Configuration ───────────────────
const int STEPS_PER_REV = 200;   // Full steps per revolution for NEMA 17
const long CLOSED_POS   = 10000; // Steps to close fully (50 revs × 200 steps)
                                   // Adjust based on your mechanism travel distance

// ── A4988 Pins ────────────────────────────────────
// AccelStepper::DRIVER = external driver mode (STEP + DIR)
AccelStepper stepper(AccelStepper::DRIVER, 2, 3); // STEP=D2, DIR=D3

// ── State Tracking ────────────────────────────────
bool lastRainState = false;

// ─────────────────────────────────────────────────
void setup() {
  pinMode(rainSensorPin, INPUT);

  // AccelStepper motion parameters
  stepper.setMaxSpeed(1000);      // Max steps/second (reduce if motor skips steps)
  stepper.setAcceleration(500);   // Steps/second² — smooth start/stop

  Serial.begin(9600);
  Serial.println("Rain-Sensing Stepper System Ready");
  Serial.print("Threshold: "); Serial.println(THRESHOLD);
}

// ─────────────────────────────────────────────────
void loop() {
  int sensorValue = analogRead(rainSensorPin);

  // Uncomment next line to calibrate threshold:
  // Serial.println(sensorValue);

  bool isRaining = (sensorValue > THRESHOLD);

  if (isRaining) {
    // ── Rain detected: move to closed position ────
    if (stepper.currentPosition() != CLOSED_POS) {
      stepper.moveTo(CLOSED_POS);
      if (!lastRainState) {
        Serial.println("RAIN detected → Closing cover...");
        lastRainState = true;
      }
    }
  } else {
    // ── No rain: return to home position ──────────
    if (stepper.currentPosition() != 0) {
      stepper.moveTo(0);
      if (lastRainState) {
        Serial.println("Rain STOPPED → Opening cover...");
        lastRainState = false;
      }
    }
  }

  // Non-blocking motor movement — must be called every loop
  stepper.run();

  // Short delay reduces noise; remove if motor motion is jerky
  delay(10);
}
📚
AccelStepper key functions: moveTo(pos) sets target position · run() executes one step if needed (non-blocking) · currentPosition() returns current step count · setAcceleration() enables smooth ramping.

Features & Benefits

Automatic rain detection
Precision stepper motor control
Smooth acceleration & deceleration (AccelStepper)
Adjustable movement distance
Compact Nano-based design
Energy-efficient automation
Non-blocking motor control loop
Serial monitor calibration support

Applications

🪟 Automatic Window Closing
👕 Smart Clothesline Retractor
🏡 Terrace Roof Automation
☀️ Solar Panel Rain Cover
🌿 Smart Greenhouse Roof
🌧️ IoT Weather-Based Control

Learning Outcomes

Interfacing analog sensors with Arduino
Stepper motor control via A4988 driver
Understanding STEP/DIR signal logic
Using the AccelStepper library
Implementing non-blocking motor control
Real-world weather automation systems
A4988 current limiting and Vref setup
Dual power supply management (5V + 12V)

© 2026 MakeMindz.com — Arduino, Raspberry Pi & IoT Automation Tutorials

Built for engineers, makers & smart-home enthusiasts 🌧️⚙️

Comments

try for free