Relay cannot stop pump from working!

Hi everyone,

I am working on a project that sanitized a door handle every time a user touches it.

To do this, I am using a 3-6V water pump, a relay, and a motion sensor. I am using the following code:

int pumpPin = 8; //relay pin
int motionPin = 7;
int motion;
void setup() {
  pinMode(motionPin, INPUT);
  pinMode(pumpPin, OUTPUT);
}

void loop() {
    motion = digitalRead(motionPin);
    if ( motion == HIGH){
      digitalWrite(pumpPin, LOW);
      delay(200);
      digitalWrite(pumpPin, HIGH); 
      
    }
    else{
    digitalWrite(pumpPin, HIGH);
    delay(100);
    }
    
  
 }

However, despite the delay only being 200 milliseconds, the pump works continues working for more time, as shown here, and it seems as if the relay is unable to stop the pump from working.

Is there a problem with my code or am I using the wrong hardware?

Put some serial prints in there. It may be that the motion detector stays high for a while and the pump comes back on after the delay.

How much current does the pump require? Remember, the Arduino output pins can only supply 40mA MAXIMUM, and that only for a second or two.
OOPS, :-[

Hi.

[edit]This is a cross post with this thread
Don't do that.
As you can see, there's different approaches of different people trying to help you solve your problem, not knowing of the existence of the other thread.
This is not helpful and very disrespectful towards those willing to help you.[/edit]

You seem to be using some 9 volts Li-Ion battery in a 6F22 form factor.
What's the capacity of that battery ?

Is the code you're showing us all you have, or is it a snippet ?

Watching your video, i can see your on board LED (pin 13) blinking.
It seems to me it has some relation to your IR sensor (LED off is relay off).
But this might also be related to some power problem (which includes a combination of those possibilities).
So easiest thing to do, would be to power your Arduino from another source than this battery.
Just to rule out the battery is your problem.

Serial prints are a very good way to debug, as wildbill already mentioned.
Put them in some smart locations in your code and have it output clear information so you can see what's going on.
The same goes for some extra delay()s (just about the only valid reason to use delay()).
The combination will help you see what exactly goes on during your sketch.
If you don't want or can't use serial monitor, figure out a smart use of pin 13, with its on board LED.
You can remove those lines from your sketch once you know it works as desired.

Try using 'state change detection' rather than switch level.

@alexoort

Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.

Continued cross posting could result in a time out from the forum.

Could you take a few moments to Learn How To Use The Forum.
It will help you get the best out of the forum in the future.
Other general help and troubleshooting advice can be found here.