5V pump acting strange

I’m trying to control a 5v water pump with a button and a relay, but there’s a really strange thing (at least for me) happening.

When I use the “Button” example code, both relay alone and relay with the pump connected work as expected. I’m using JC_Button library for buttons and relay alone also works fine, but when I connect the pump, it rapidly turns off and on when I hold down the button. When I connect a different kind of pump or any other device, it works fine.

I also tried controlling the power source with relay, not the pump directly, but the same thing happned.

#include <JC_Button.h>

const int buttonPin = D2;     // the number of the pushbutton pin
const int ledPin =  13;      // the number of the LED pin

int buttonState = 0;         // variable for reading the pushbutton status
Button my_button(buttonPin);

void setup() {
  // initialize the LED pin as an output:
  pinMode(D3, OUTPUT);
  // initialize the pushbutton pin as an input:
  pinMode(buttonPin, INPUT);


void loop() {
    if (my_button.wasReleased()) { // you just pressed the pushbutton
                digitalWrite(D3, LOW);

    if (my_button.wasPressed()) { // you just release the push button

          digitalWrite(D3, HIGH);


Please post a complete wiring diagram (hand drawn, not Fritzing, with pins and parts labeled). We need to see exactly how the Arduino and button is wired and powered.

No external pullup resistor for your buttonPin? If not then try INPUT_PULLUP.

I have the resistor, but the button control works perfectly when I use arduino button example and it works with this library too, when I don't use this specific model of pump.

Edit: there's a flyback diode on the pump too.

That relay needs 5V for power. Have a spare 5V power source? Not sure what digital pins would work best, or if 3.3V signal level would be a problem. Do you have a link to the relay module?

I have tried it with 5V too, same thing.

To me, it seems that it:

  • isn't a relay problem as it works fine with without the small pump and with the bigger, 25W pump
  • isn't pump problem because it works with "button" example code
  • isn't pump problem also because it acts the same with the flyback diode, relay with an optocoupler and without even being connected to the relay directly when I use the JC_Button library
  • probably isn't JC_Button problem as it all works with a different kind of pump
  • isn't push button problem as it works fine without the JC library

It would be good to know if the same code and circuit would work using a 5V Arduino. Anyways, I suspect a borderline voltage level issue.

Unfortunately, the same thing happens with Arduino Uno, too.

Have you taken any voltage measurements across the motor ?

I'm suspecting opto-isolation could solve this, but your diagram shows a non-opto type relay module. If you do have a relay module with opto isolator, then it'll have a JD-VCC jumper that could be removed. Then powering it with a separate 5V supply and only wiring Aduino 5V and 1 control wire is all that's needed to the Arduino (there's no ground connection from relay module to Arduino).

Have a link to your relay module?

No opto on the one in the picture, but I'm using one with opto isolation and I have tried the method you suggested.:

Amazon.com: SunFounder 2 Channel DC 5V Relay Module with Optocoupler Low Level Trigger Expansion Board for Arduino R3 MEGA 2560 1280 DSP ARM PIC AVR STM32 Raspberry Pi: Computers & Accessories

I haven't done any measurements, I will try that.

OK, so to enable opto isolation, connections are like this (except you have the 2 relay version and 5V Arduino is shown):

It is easy to know whether the relay works: you can hear it click. If there's no click, it's not switching.

You have a pull-down resistor wired to the button, yet the JC_Button class
defaults to using internal pull-ups, so you have two resistors pulling against each other
I think.

Its normal to wire buttons to ground and use internal pull-ups, which is what the JC_Button
library is expecting.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.