Working with relay and PIR sensor

As we are dealing with high voltage and higher currents, we would be using a relay. To do this, connect the wires as follows:

Once you are done connecting it, upload the following code and let's see what happens:

import RPi.GPIO as GPIO
import time

LIGHT = 23

GPIO.setmode(GPIO.BCM)
GPIO.setwarnings(False)
GPIO.setup(LIGHT,GPIO.OUT)

import datetime

H = datetime.datetime.now().strftime('%H')
M = datetime.datetime.now().strftime('%M')


while True:

if H = '06'and M < 20 :
GPIO.output(LIGHT,GPIO.HIGH)

else:
GPIO.output(LIGHT,GPIO.LOW)

OK, then it is a fairly simple code with not much explanation needed. We have done a very similar code before as well. Do you remember when? It was in the first few chapters when we were making a gardening robot where we had to fetch water to the plants at a certain time. All it is doing at this time is to check the time and whether the time is 06 hours and the minute is less than 20. That is, the light would be switched on between 07:00 hours to 07:19 hours. Thereafter, it would switch off.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset