I am trying to create a water fountain controlled with a button. When someone presses a button, the water fountain will turn on. I thought I could substitute the LED with a 5V mini water pump. When the LED is connected it works, but when I disconnect the LED and substiture it with the water pump it does not. Do I have to modify the code for the water pump?
int led = 13;
int button = 12;
int ledState = HIGH;
int buttonCurrent;
int buttonPrevious = LOW;
void setup() {
// put your setup code here, to run once:
pinMode(button, INPUT);
pinMode(led, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
buttonCurrent = digitalRead(button);
if (buttonCurrent == HIGH && buttonPrevious == LOW)
{
if (ledState == HIGH)
{
ledState = LOW;
}
else
{
ledState = HIGH;
}
}
digitalWrite(led, ledState);
}
![water pump picture|690x400](upload://fOBq9lh3PzufUh6ZKsHi1SBb38p.png)
No, BUT you cannot power a motor from an Arduino pin. You can control the motor circuit with a transistor or a relay module and separate power.
Why are you using an Arduino if all you need is a switch?
Much more likely that you need to modify the circuit- you can't power a motor out of an i/o pin which can supply 20mA safely. You will need a decent power supply for the motor and control it (as opposed to power it) with the i/o pin.
Choose the motor driver based on the stall current of the motor and the motor supply voltage. The stall current can be several times the running current. The stall current will be drawn, briefly, every time that the motor is started. The stall current should be listed in the motor data sheet. In the absence of a data sheet, the stall current can be estimated. To estimate the stall current, measure the motor winding resistance. Zero your meter lead resistance before measuring the motor coil resistance. Take several measurements rotating the motor a bit between readings. Use the lowest reading in the calculation. The estimated stall current is the motor supply voltage divided by the measured resistance.
The reason I am using the arduino is because I wanted to create a cat water fountain in which my cat will press a button, the water fountain will turn on for 20 seconds to preserve battery life, then turn off. I already have the correct button for my cat to press, which is a modified button, kind of like the ones used to get ice from a refrigerator. I will also need to modify the code and put a delay, but I wanted to see if it this worked first. If I connect a relay, I'm guessing I will need to modify the code?
Use a PIR sensor too so kitty just has to approach. A sonar module would act as a range gate to step up the action when kitty gets near.
But this doesn’t need a computer. A 555 timer and a short flip flop chain as a divider. i can do it in two ic's and a few r's & c's.
Which pin do you actually have connected to the relay? The diagram doesn't match the code.
The side of the relay that is connected to the pump normally has 3 terminals...
COM (Common) - this should be connected to the power source (+) for the pump.
NO (Normally open) - this should be connected to your pump.
NC (Normally closed) - not required.
When you close the relay, COM is connected to NO.
You also need to connect the other side of your power source (-) to the pump.
Thanks Madmark2150! I have been working on having a PIR sensor in which once my cat comes near it, the water pump turns on. I removed the button and put a PIR sensor. The mini water pump works as it is constantly on, which means that the PIR sensor is not detecting movement. When I completely disconnect the PIR sensor, the water pump will not turn on. Im guessing its the code. The serial monitor constantly says "Motion detected". I wanted the water pump to turn on for 10 seconds once it detects my cat near it, then turn off. Then turn on if she comes near it again so that it is not constantly on. I'm guessing there's an incorrect order in my code:
int led = 10;
int sensor = 3;
int state = LOW;
int val = 0;
int relayPin = 10;
void setup() {
// put your setup code here, to run once:
pinMode(led, OUTPUT);
pinMode(sensor, INPUT);
Serial.begin(9600);
pinMode(relayPin, OUTPUT);
}
void loop() {
// put your main code here, to run repeatedly:
val = digitalRead(sensor);
if (val == HIGH){
delay(100);
if (state == LOW){
Serial.println("Motion detected!");
state = HIGH;
digitalWrite(relayPin, HIGH);
delay(10000);
}
}
else{
digitalWrite(led, LOW);
delay(2000);//500
if(state == HIGH){
Serial.println("Motion stopped!");
state = LOW;
digitalWrite(relayPin, LOW);
delay(2000);
}
}
}
Use Ctrl-T for autoformatting. And don't spread your code sooo wide with a lot of empty lines.
I made a WOWKI-simulation of your setup
The basic principles works.
You are using this sequential pattern of non-responsive code based on the command delay()
It is very common that beginners are using delay() because the Arduino-development team decided to keep the example-codes constant instead of improving them.
As long as a delay is active nothing else can be done. Not even serial output.
I added serial output to your code to make visible what the program is doing at the moment.
The WOKWI-simulation uses an LED instead of a relay
As your code is working as expected it might be that your PIR-sensor is keeping the output too long high
This could be analysed with a code that completely avoids using delay() and uses non-blocking timing.
This requires to learn some new things about non-blocking coding.
Not sure if you want to learn this
Hello spiderman288888
Keep it simple and stupid.
To avoid using the delay() function, which blocks the expected real-time behaviour, design your own timer manager.
It is recommended to start with the mother of all Arduino timers, the IDE's BLINKWITHOUTDELAY example.
Using this example, you can see how millis() function works and extend this with some additional functions.
startTimer() --> starts the timer
stopTimer() --> stops the timer
and
actionTimer() --> performs an action when the timer is triggered.
Have a nice day and enjoy programming in C++ and learning.
Errors and omissions excepted.
use this badly coded hard to understand The learning child severely neglecting mother of all Arduino-Timers
that cause
every week new
1 to 5 new postings "I don't understand millis()-questions"
a first part to explain how non-blocking coding works is posted here.
I'm not satisfied with it yet because there must be more explained how to let do loop() all looping by easy to understand everyday analogons explaining pictures and multiple demo-codes that show how it works on small examples.
@StefanL38 I know you are passionate about what you believe, but... your examples are supposedly "easier to understand"... but then I see statements like...
and
Do you think these are terms that a beginner will understand?
...and abstracting fairly simple code into, for example, TimePeriodIsOver does not help understanding... it simply hides stuff from the developer... so they never really learn and understand the fundamental concepts of non-blocking code.
Your "everyday analogy" of the pizza, explains the BlinkWithoutDelay example nicely but you then hide the simple code that sits behind it in a function that requires an understanding of "pass by reference" - something that is above "beginner" level.
Maybe the BlinkWithoutDelay example needs a refresh... but I'm not sure your dumbed-down examples are the best alternatives.
Thank you all who responded. I am taking all who responded into consideration.
StefanL38, I loved the Wokwi Arduino and ESP32 Simulator! If you know the solution, these simulators are great to show where the problem was and how to go around it instead of throwing endless hints that some of us may never get.
The cat water fountain works however the 5V mini water pump, as seen in the pic, seems to burn out in a day. I have already burned out 2 water pumps. Any idea which water pump will last longer? Does the water pump burn out because it is constantly switching on and off? I was going to remove the black tape and put velcro so you wont see the tape, for better presentation.
If you ever see your cat pawing at its water bowl, its because they don't really like drinking from still water as bacteria grows in still water and they can smell it, so they are trying to simulate moving water by pawing at it, which tends to make it spill out of their water bowl. They did seem to enjoy the running water as no more pawing. Here is my cat water fountain you all helped me to create! I will replace the tape with velcro soon. My cats thank you all.