Passive Infrared Sensor - Python with Raspberry Pi

Download Report

Transcript Passive Infrared Sensor - Python with Raspberry Pi

Passive Infrared Sensor

The PIR Sensor
detects motion
Copyright (c) 2017 by Dr. E. Horvath
Passive Infrared Sensor
Copyright (c) 2017 by Dr. E. Horvath
PIR Sensor Circuit

Leads left to right on the back of the PIR Sensor

Breadboard locations

GND VCC OUT

j1 j2 j3

breadboard location f2 jumper wire to 3.3 V PWR (Pin 1)

breadboard location f3 jumper wire to GPIO 17 (Pin 11)

breadboard location f1 jumper wire to GND (Pin 6).

Connect the GND last.

Remove the GND first, when taking the circuit apart.
Copyright (c) 2017 by Dr. E. Horvath
PIR Sensor Circuit
Copyright (c) 2017 by Dr. E. Horvath
Passive Infrared Sensor

from time import sleep

import RPi.GPIO as GPIO


GPIO.setmode(GPIO.BCM)


Import the GPIO library to interact with the GPIO
Set to the Broadcom mode to access the pins by the functions
PIR = 17

Monitor GPIO 17

GPIO.setup(PIR, GPIO.IN)

try:

while True:

print("Waiting...")

sleep(0.1)
# Do not use up CPU cycles
Passive Infrared Sensor
if GPIO.input(PIR) == 1:


print("PIR detected")

sleep(1)
else:


print("Waiting for passive infrared sensor")

sleep(0.1)

except KeyboardInterrupt:
print(“Exiting ...”)

finally:
GPIO.cleanup()

Set the pins back to a safe state
Sending emails

import smtplib

from email.mime.text import MIMEText

fromaddr = 'Your email address'

toaddr = 'Reciever email address'

username = 'Your email address'

password = 'Your password'
Copyright (c) 2017 by Dr. E. Horvath
Sending emails

Create a gmail account

Go to https://www.google.com/settings/security/lesssecureapps

Access for less secure apps Turn on
Copyright (c) 2017 by Dr. E. Horvath
Sending emails
•
msg = MIMEText('This Raspberry PI stuff is awesome')

msg['Subject']='PYTHON WITH RASPBERRY PI ANOTHER TEST'

msg['From'] = fromaddr

msg['To'] = toaddr

server = smtplib.SMTP('smtp.gmail.com:587')

server.ehlo()

server.starttls()

server.login(username,password)

server.send_message(msg)

server.quit())
Copyright (c) 2017 by Dr. E. Horvath
LEDs

Color
Maximum Voltage (check datasheets)

Red
2V

Green/Yellow/Orange 2.2V

Blue/White 3.7V

Violet

Ultraviolet
4.4V

Infrared
1.6V
4V
Ohm's Law

V = IR

Voltage = Current * Resistance

Current – measured in Ampheres

Use mA in the lab

1000mA = 1A

1mA = 0.001A

Resistance – measured in Ohms
Copyright (c) 2017 by Dr. E. Horvath
Ohm's Law

Maximum current on any GPIO pin 16mA

Output from a GPIO pin is 3.3 V

3.3V – 2.2V = 1.1V

2.2V to power the green LED

V = IR

1.1V = 16 * 0.001 * R

R = 1.1/(16 *0.001) = 68.75 Ohms for maximum brightness

The minimum you should use is 100 Ohms

Use higher resistance over 1KOhm when experimenting
Copyright (c) 2017 by Dr. E. Horvath
Compute the Current

I = V/R

I = 1.1/R

R is your resistor's value in Ohms

Refer to the color chart
Copyright (c) 2017 by Dr. E. Horvath
Resistor Color Chart
Copyright (c) 2017 by Dr. E. Horvath
Four Band Resistor Code

Bands 1 and 2
Band 3

First Digit and Second Digit
Multiplier or Number of Zeroes

Black 0
Black * 1

Brown 1

Red 2
Brown * 10
Red * 100
Orange * 1,000

Orange 3
Yellow * 10,000

Yellow 4
Green * 100,000

Green 5
Blue * 1000,000

Blue 6

Purple 7
White * 100,000,000

White 8
Gray * 1,000,000,000

Gray 9
Gold *0.1
Purple * 10,000,000
Copyright (c) 2017 by Dr. E. Horvath
Silver * 0.01
Four Band Resistor Code

Band 4 Percent Tolerance

Purple
0.1%

Black -

White
-

Brown
1%

Gray
-

Red
2%

Gold
5%

Orange
-

Silver
10%

Yellow
-

No color 20%

Green
0.5%

Blue
0.25%
Copyright (c) 2017 by Dr. E. Horvath
Connecting an LED

Light emitting diode (LED) connections

Longer lead is positive (anode) breadboard location j24

Shorter lead is negative (cathode) breadboard location j22

1.2KOhm Resistor

breadboard location i30

breadboard location i24

breadboard location h30 jumper wire to GPIO 22 (Pin 15)

breadboard location f22 jumper wire to GND (Pin 9)
Copyright (c) 2017 by Dr. E. Horvath
Connecting an LED
Copyright (c) 2017 by Dr. E. Horvath
Turning on an LED
•
import RPi.GPIO as GPIO

green_led = 23

GPIO 23
GPIO.setup(green_led,GPIO.OUT)


Set the pin to be an output
GPIO.output(pin_number,True)


Turn on the LED

sleep(interval)

GPIO.output(pin_number,False)

sleep(interval)


Turn off the LED
GPIO.cleanup()

Set the GPIOs back to a safe state
Copyright (c) 2017 by Dr. E. Horvath