Relay keeps clicking continously - help

Hello. I am making a water fountain for my cat. I have an Arduino Nano, powered via miniUSB from a 5v phone charger. I have a 5v relay and a motion PIR sensor, powered from arduino's 5v. The relay clicks when moving, seems fine. When I connect the pump, the pump works but the relay keeps clicking. The voltage slightly drops to 4.7 - 4.8v. Pump is supposed to be 3-6v 100-200mah. The pump is connected to another charger. The pump works, but the relay clicks like once every 0.1 seconds. This is the code.

const int MOTION_SENSOR_PIN = 8;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int RELAY_PIN         = 6;  // Arduino pin connected to the IN pin of relay
int motionStateCurrent      = LOW; // current  state of motion sensor's pin
int motionStatePrevious     = LOW; // previous state of motion sensor's pin

void setup() {
  Serial.begin(9600);                // initialize serial
  pinMode(MOTION_SENSOR_PIN, INPUT); // set arduino pin to input mode
  pinMode(RELAY_PIN, OUTPUT);        // set arduino pin to output mode
}

void loop() {
  motionStatePrevious = motionStateCurrent;             // store old state
  motionStateCurrent  = digitalRead(MOTION_SENSOR_PIN); // read new state

  if (motionStatePrevious == LOW && motionStateCurrent == HIGH) { // pin state change: LOW -> HIGH
    Serial.println("Motion detected!");
    digitalWrite(RELAY_PIN, HIGH); // turn on
  }
  else
  if (motionStatePrevious == HIGH && motionStateCurrent == LOW) { // pin state change: HIGH -> LOW
    Serial.println("Motion stopped!");
    digitalWrite(RELAY_PIN, LOW);  // turn off
  }
}
1 Like

Use the phone charger as the power supply for all the devices. Arduino is a microcontroller, not a power supply.

Also, tho' it's not shown I'll guess you're driving the relay with an Arduino output?

The above is all the code. What do you mean?

If I power the relay from the same psu, it does not work at all. 5v 2.4A psu.

Then you have "it" incorrectly connected... according to your drawing.


It is exactly like this, but a small pump not a bulb, and Nano not Uno

Where is the 5vdc phone charger?

I know, I know... everything is connected right and it all works, but it doesn't work... and I am wrong... so that means, if you hook it up like this, and it works, you will be saying I am right... but since you have everything right and it works but it doesn't work... this must be wrong. Dilemma.
Corrected image shows the missing 5vdc phone charger power going to the coil side of the relay and sharing ground... or not.

I just did as you said, relay does not work at all now


Relay ground to arduino ground, red wire from VCC to charger.

@lupuom
The diagram you show in post #7 is fine and it should work.
Does it work OK if you have the USB connected to a computer?
Is there a sensitivity adjustment on the PIR device?

With a computer USB, it is the same. The thing is, the relay clicks and works perfectly when NOT connected to the pump. It respects the set time on the PIR sensor, everything. When I connect the pump... Clickclickclickclick. Pump works. The clicking is extremely annoying though.

Your assumption that connecting the pump should not have an effect on the relay is correct but it is happening.
One bad thing is that you are running a 3-6V pump on 12V, that is not good.
Another bad thing is that you don't have a flyback diode on the pump.

The pump runs on a phone charger via usb cable cut up, it is 5v.

Is there anything else different from what you show in your diagram in post #7 ?

Hi @lupuom ,

is it intended that your drawing and the sketch do not comply?

Sketch:

const int MOTION_SENSOR_PIN = 8;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int RELAY_PIN         = 6;  // Arduino pin connected to the IN pin of relay

Drawing:

If the drawing is correct it should read

const int MOTION_SENSOR_PIN = 7;   // Arduino pin connected to the OUTPUT pin of motion sensor
const int RELAY_PIN         =  A5;  // Arduino pin connected to the IN pin of relay

Try putting a 1N4001 diode like shown in this diagram

1 Like

Yes, it is an internet scheme, I put mine on 8 and 6, Digital.

Thank you, will try it out.

Please don't post diagrams that don't show exactly how you have things connected and what powers sources you are using. It just makes us go on wild goose chases and we get nowhere in trying to help you.

Please show us exactly how you have things connected and how things are powered.

1 Like


Done ! I have trouble finding that diode without quite some time of shipping