rain sensor with stepper

hi, all,

thanks tom for breifing me on how to use this forum.

So,,first of all the hardware works,the motor turns BUT;;;;

My overall aim is to make the motor turn back and forth with respect to the voltage (A0) from a rain sensor.
There are 2 ways.
1- to increment it in step with A0 voltage or
2- rotate it 25 degreees when it reaches a certain level.
My priority is the simplest code.

I was previously using a smaller nema 17 with an L298N Dual H Bridge but it was underpowered and using the stepper library with pins 8,9,10,11 so I upgraded to more power with a dm320t driver BUT this uses step and dir pins so the previous code does not work...

I am now trying to understand how to make this work.
From my research the basic code is this.

digitalWrite(dirPin, HIGH); //sets the direction
digitalWrite(stepPin, HIGH); //steps the motor
delayMicroseconds (500); //delay of 500
digitalWrite(stepPin, LOW);

because if I place this in front of it

"for (int x = 0; x < 600; x++) //rotates motor 600 steps

then it works.

So now I have this code that works with my sensor

const int enable = 6;
const int stepPin = 5;
const int dirPin = 4;

void setup() {
  pinMode(A0, INPUT);          //rain sensor analog  in from sensor
  pinMode(6, OUTPUT);          //sets pin enable
  pinMode(5, OUTPUT);         //sets step
  pinMode(4, OUTPUT);         //sets direction
  digitalWrite(6, HIGH);      //set enable low,dm320 not enabled

void loop() {

  int sensor = analogRead(A0);     //rain sensor reading on A0
  Serial.println(sensor);          //prints to monitor

  if (sensor < 600) {

    digitalWrite(dirPin, HIGH);       //sets the direction

    for (int x = 0; x < 600; x++) {   //rotates motor 600 steps
      digitalWrite(stepPin, HIGH);     //steps the motor
      delayMicroseconds (1500);        //delay of 500
      digitalWrite(stepPin, LOW);

BUT what this does is rotate the motor 600 steps when the sensor is <600 ,which it will be till it stops raining but i dont wont the motor to keep rotating,it cannot open more than 600 which is about 45 degrees,,,,I want it to remain there untill it its >600 then rotate back to zero position,,I asssume its an IF THEN statement or a SWITCH CASE scenario but not sure about it,,,

would you have any suggestions?
thank you.

inbetween my first post I got it to work to this point ,sorry for wasting your time CHUCKYX who replied but you got me thinking about it with your answer...

You can also use the code tags instead of uploading a .txt

const int enable = 6;
const int stepPin = 5;
const int dirPin = 4;

Why are you defining these if you later use the numbers anyway?

  pinMode(6, OUTPUT); //enable
  pinMode(5, OUTPUT); //step
  pinMode(4, OUTPUT); //dir

I was too lazy to google datasheets myself, and also I don't have any schematic of your wiring.

  digitalWrite(6, HIGH); //set enable low,dm320 not enabled

So this is active low? Do you need to set 6 to LOW to enable the dm320? If so, then

  if (sensor < 900)
  //for(int x = 0; x < 50; x++) //test purpose line 20
    digitalWrite(4, HIGH);//sets the direction
    digitalWrite(5, HIGH); //steps the motor
    delayMicroseconds (500); //delay of 500
    digitalWrite(5, LOW);

you don't have it enabled here.

If the motor is actually working with just the if portion replaced with the for loop, then I don't know why it wouldn't work then.

Welcome to the forum.

Please read the first post in any forum entitled how to use this forum.
http://forum.arduino.cc/index.php/topic,148850.0.html then look down to item #7 about how to post your code.
It will be formatted in a scrolling window that makes it easier to read.

Can you please post a copy of your circuit, in CAD or a picture of a hand drawn circuit in jpg, png?

Thanks.. Tom... :slight_smile: