In the tutorial Wireless Remote Control Lighting System | Smart Lighting Control Systems I have done the control of AC light using Raspberry Pi and Cayenne App. I got a request from my friend to control the Water Pumping motor at a specific interval. Currently they have to switch on and off the motor manually. So we will be working on AC motor and Raspberry pi which will act as automatic watering system. We are not making any plant watering system here.
You could have seen other articles on controlling DC Motors Using Python With a Raspberry Pi, however here we are using the Raspberry Pi to control AC electric power . The connections are almost similar to the light control system, you need to replace the light with motor and we will be using Python script.
SINCE 120/230V AC IS DANGEROUS, DO NOT TOUCH WIRES FROM OUTLET WHILE IT IS POWERED ON! AND ALSO RECOMMEND YOU TO CHECK WITH ELECTRICIAN FOR WIRING. YOUR SAFETY IS MORE IMPORTANT.
Automatic Water Pump Using Raspberry Pi:
For making automate motor control with raspberry pi, first connect the VCC of the Relay to GPIO Pin 2 and Relay GND to GPIO 39 which is GND in Pi. Connect the Raspberry Pi GPIO Pin 19 to Relay IN pin which will control the NO and NC connection of Relay . Connect the AC line 120/230V AC from Plug point to COM of Relay and other wire from NO or NC on relay to the Motor. The neutral from pug point will go directly to Motor.
Python Script For Raspberry Pi 3 Automatic Watering System:
The raspberry pi ac motor control will run based on interval we define in the script. What we are doing here is to capture the last run time of the motor in to a notepad and check that with current time. If the motor last run was 1 hour before the motor will start pumping the water for 15 min and will stop.
import RPi.GPIO as GPIO
from time import sleep
runhour = (time.strftime("%H%M%S"))
add = 4000
file1 = open("/home/pi/Motor_Running_Project.txt","r",encoding = 'utf-8')
f = file1.read(6)
intrunhour = int (f)
now = int(time.strftime("%H%M%S"))
x = now - intrunhour
intrunhour = now + add
nextrun = str (intrunhour)
file1 = open("/home/pi/Motor_Running_Project.txt","w")
print("program not started")
The Python program is also attached Motor Control With Raspberry Pi_Project.py.
For DC Motor Control With Raspberry Pi I will update in new tutorial.