# How to control Rain Sensor and 2 DC Gear Motors?

**URL:** <https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679>\
**Category:** Motors, Mechanics, Power and CNC\
**Created:** [November 3, 2019, 4:32pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679 "2019-11-03T16:32:56Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![Azxce](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Azxce](https://forum.arduino.cc/u/Azxce)\
**Post date:** [November 3, 2019, 4:32pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/1 "2019-11-03T16:32:56Z")

</div>

How to control Rain Sensor and DC Gear Motors? I wanted to rotate clockwise the 2 dc gear motors if the rain sensor is wet and have a duration on it and wanted to rotate counterclockwise the 2 dc gear motors if the rain sensor is dry and also have a duration on it. How can I control it? How to stop and resume it? Please HELP me. I really need your help. I`m a beginner.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [November 3, 2019, 4:58pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/2 "2019-11-03T16:58:30Z")

</div>

What have you tried?

What rain sensor? What drivers for the motors? The more we know about your project the more help we can give.

Provide a schematic and the code for your best effort and we can help.

Read the how to use this forum-please read stickies to see how to post code and some advice on asking questions.

---

<div class="post-metadata">

**Author:** ![Azxce](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Azxce](https://forum.arduino.cc/u/Azxce)\
**Post date:** [November 5, 2019, 3:17pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/3 "2019-11-05T15:17:26Z")

</div>

My problem is how can I put a duration/time or stop and resume to my DC Gear motors and the rain sensor? I wanted that if the rain sensor is WET the DC gear motors will rotate clockwise then put a duration on it like (2 or 3 seconds) then stop. Until/And if the Rain sensor is DRY the DC Gear motors will rotates counterclockwise. Help me please 🙁

I used this ff:

Rain Sensor  
2 DC Gear Motor  
2 Lithium Battery 18650  
Driver Motor L298N  
Breadboard  
Wires

This is my code.

//rain Sensor  
const int capteur\_D = 4;  
const int capteur\_A = A0;

int val\_analogique;

// motor one  
int enA = 10;  
int in1 = 9;  
int in2 = 8;  
// motor two  
int enB = 5;  
int in3 = 7;  
int in4 = 6;  
void setup()  
{  
// set all the motor control pins to outputs  
pinMode(enA, OUTPUT);  
pinMode(enB, OUTPUT);  
pinMode(in1, OUTPUT);  
pinMode(in2, OUTPUT);  
pinMode(in3, OUTPUT);  
pinMode(in4, OUTPUT);  
pinMode(capteur\_D, INPUT);  
pinMode(capteur\_A, INPUT);  
Serial.begin(9600);

}

void loop() {  
if(digitalRead(capteur\_D) == LOW)  
{  
// this function will run the motors in both directions at a fixed speed  
// turn on motor A  
digitalWrite(in1, HIGH);  
digitalWrite(in2, LOW);  
// set speed to 200 out of possible range 0~255  
analogWrite(enA, 200);  
// turn on motor B  
digitalWrite(in3, HIGH);  
digitalWrite(in4, LOW);  
// set speed to 200 out of possible range 0~255  
analogWrite(enB, 200);  
delay(2000);  
// now turn off motors  
digitalWrite(in1, LOW);  
digitalWrite(in2, LOW);  
digitalWrite(in3, LOW);  
digitalWrite(in4, LOW);

}

else  
{  
// now change motor directions  
digitalWrite(in1, LOW);  
digitalWrite(in2, HIGH);  
digitalWrite(in3, LOW);  
digitalWrite(in4, HIGH);  
delay(2000);  
// now turn off motors  
digitalWrite(in1, LOW);  
digitalWrite(in2, LOW);  
digitalWrite(in3, LOW);  
digitalWrite(in4, LOW);

}  
}

---

<div class="post-metadata">

**Author:** ![Azxce](https://avatars.discourse-cdn.com/v4/letter/a/dec6dc/32.png) [@Azxce](https://forum.arduino.cc/u/Azxce)\
**Post date:** [November 5, 2019, 3:26pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/4 "2019-11-05T15:26:05Z")

</div>

I already get the thing that if the rain sensor is WET the DC Gear motors will rotates clockwise and if the rain sensor is DRY the DC Gear motors will rotates counterclockwise. My problem now is if the Rain Sensor is WET or DRY it just continue in rotating. how can I put a duration or stop the DC motors after the duration runs out? Please Help.

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [November 5, 2019, 3:32pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/5 "2019-11-05T15:32:37Z")

</div>

What does the posted code actually do? How does that differ from what you want?

Using the delay() function will make the program unresponsive. Better to use millis() or micros() for timing.  
Non-blocking timing tutorials:  
[Several things at a time.](https://forum.arduino.cc/index.php?topic=223286.0)  
[Beginner's guide to millis().](https://forum.arduino.cc/index.php?topic=503368.0)  
[Blink without delay().](https://www.arduino.cc/en/tutorial/BlinkWithoutDelay)

It may help if you try to learn how to use a [state machine](https://www.sparkfun.com/news/1801). Google "arduino state machine" for more pages.

Read the [how to use this forum-please read sticky](https://forum.arduino.cc/index.php/topic,148850.0.html) to see how to properly post code and some advice on how to ask an effective question. Remove useless white space and format the code with the IDE autoformat tool (crtl-t or Tools, Auto Format) before posting code.

---

<div class="post-metadata">

**Author:** ![slipstick](https://avatars.discourse-cdn.com/v4/letter/s/50afbb/32.png) [@slipstick](https://forum.arduino.cc/u/slipstick)\
**Post date:** [November 5, 2019, 3:42pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/6 "2019-11-05T15:42:02Z")

</div>

You need to look at the StateChangeDetection example in the IDE because I don't think you're describing your requirements properly.

What you actually want is when the detector first becomes wet (i.e. changes from dry to wet) you start the motors clockwise then stop them a few seconds later. And then later on when the detector changes from wet back to dry you run the motors counterclockwise for a few seconds. That's very different from what you're currently doing.

Steve

---

<div class="post-metadata">

**Author:** ![groundFungus](https://avatars.discourse-cdn.com/v4/letter/g/73ab20/32.png) [@groundFungus](https://forum.arduino.cc/u/groundFungus)\
**Post date:** [November 5, 2019, 3:42pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/7 "2019-11-05T15:42:42Z")

</div>

You want to run the motors only when the condition changes from dry to wet or wet to dry (transition) not when the conditions **are** wet or dry (state)? So the transitions from one to the other is what is important, not the state. The [state change detection example](https://www.arduino.cc/en/Tutorial/StateChangeDetection) shows how to detect the transitions by keeping track of the last state and only responding on the changes.

---

<div class="post-metadata">

**Author:** ![MarkT](https://avatars.discourse-cdn.com/v4/letter/m/c89c15/32.png) [@MarkT](https://forum.arduino.cc/u/MarkT)\
**Post date:** [November 5, 2019, 3:45pm UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/8 "2019-11-05T15:45:37Z")

</div>

And perhaps learn about state machines, that provides a clear framework for programming a behaviour pattern.

---

<div class="post-metadata">

**Author:** ![system](https://dub1.discourse-cdn.com/arduino/user_avatar/forum.arduino.cc/system/32/1140315_2.png) [@system](https://forum.arduino.cc/u/system)\
**Post date:** [May 6, 2021, 9:51am UTC](https://forum.arduino.cc/t/how-to-control-rain-sensor-and-2-dc-gear-motors/618679/9 "2021-05-06T09:51:44Z")

</div>

Closed on May 6, 2021, 9:51am UTC


