Automated Motor Control With Raspberry Pi | Controlling AC motor with raspberry


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.

Components Needed:

  1. Raspberry Pi
  2. 5V Relay Module
  3. AC Motor/Water Pump
  4. Connecting wires
  5. Power supply for Pi

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.

Raspberry Pi Motor Control

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
import os
import sys
import datetime
import time
from time import sleep
GPIO.setmode(GPIO.BCM)
GPIO.setup(17, GPIO.OUT)
runhour = (time.strftime("%H%M%S"))
add = 4000
file1 = open("/home/pi/Motor_Running_Project.txt","r",encoding = 'utf-8')
f = file1.read(6)
print(f)
file1.close()
intrunhour = int (f)
while True:

now = int(time.strftime("%H%M%S"))
print(now)
x = now - intrunhour
print(x)

       if x>=0:

         GPIO.output(17,GPIO.HIGH)
         print("program started")
         time.sleep(900)
         intrunhour = now + add
         nextrun = str (intrunhour)
         file1 = open("/home/pi/Motor_Running_Project.txt","w")
         file1.write(nextrun)
         file1.close()
         print(nextrun)
         GPIO.output(17,GPIO.LOW)

       else:
                print("program not started")

GPIO.output(17,GPIO.LOW)
GPIO.cleanup()

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.