I have my arduino connected to a water pump that connects with a 4 aa battery case, and a water moisture sensor that is connected to a relay, which finally connects to my arduino uno. The program runs just fine when I only have the water moisture sensor connected, but when I connect the water pump to it, the program stops running. Does anyone know how I can fix this? ( The code here:)
void setup() {
pinMode(5,OUTPUT);
Serial.begin(9600); // open serial port, set the baud rate to 9600 bps
}
void loop() {
int val;
val = analogRead(0); //connect sensor to Analog 0
if (val < 270) {
Serial.println("Wet"); //print the value to serial
Serial.println(val); //print the value to serial
pinMode(5,LOW);
//delay(500);//To Soil "i", number 189
}
if (val > 270) {
Serial.println("Dry"); //print the value to serial
Serial.println(val); //print the value to serial
pinMode(5,HIGH);
// //delay(500);
}
delay(1000);
}
The problem is the pump.
You are not the first person to have interference problems with that particular pump.
You will need to buy a 1N4002 diode and a 0.1uF ceramic capacitor.
Connect everything like this:
Apparently, those pumps create a lot of electromagnetic interference (noise) that is picked up by the Arduino, the moisture sensor and all the wires connecting everything together. The noise will interfere with the operation of the moisture sensor and most analog sensors in general.
The capacitor helps to reduce the noise created by the motor when it is running. The diode helps suppress the large voltage surge created by the motor when you turn it off.
You should also make your set-up nice and tidy without wires all crisscrossing each other. Keep the motor wires away from all other wires.