// Yo, Yo, yo, Yo, yo, here's my code for the water pump!
//Red LED is in place of water pump on this diagram
// Connect Sensor IR Sensor + to 5v, and IR Sensor - to arduino GND
// Connect Relay VCC to 5v, and Relay GND to arduino GND
// Connect Water Pump + to NO on Relay, Connect Power (battery or power cable) to COM on Relay
// Connect Water Pump - to battery - , power cable - or Arduino GND
int relay = 13; // Connect Relay IN to
int sensor = 2; // Connect Sensor D to
int state = LOW; // by default, no motion detected by Sensor
int val = 0; // variable to store the sensor status (value)
void setup() {
pinMode(relay, OUTPUT); // initalize relay as the output
pinMode(sensor, INPUT); // initialize sensor as the input
Serial.begin(9600); // initialize serial
}
void loop(){
val = digitalRead(sensor); // read sensor value
if (val == HIGH) { // check if the sensor is HIGH
digitalWrite(relay, HIGH); // activate relay
delay(100); // delay 100 milliseconds
if (state == LOW) {
Serial.println("Motion detected, Pump On!");
state = HIGH; // update variable state to HIGH
}
}
else {
digitalWrite(relay, LOW); // activate relay
delay(100); // delay 100 milliseconds
if (state == HIGH){
Serial.println("Motion stopped, Pump Off!");
state = LOW; // update variable state to LOW
}
}
}