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);
my_button.begin();
}
void loop() {
my_button.read();
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.
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.
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?
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
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).
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.