Tech
Forums
Jobs
Books
Events
Interviews
Live
More
Learn
Training
Career
Members
Videos
News
Blogs
Contribute
Article
Blog
Video
Ebook
Interview Question
Collapse
Feed
Dashboard
Wallet
Learn
Achievements
Network
Rewards
SharpGPT
Premium
Contribute
Article
Blog
Video
Ebook
Interview Question
Register
Login
Connecting The PIR With Arduino In Python
WhatsApp
Sr Karthiga
4y
13.7k
0
2
100
Article
Pirsensor.zip
Introduction
In this article, I am going to explain about connecting the PIR with Arduino In Python.The output will be displayed on both sides in Python and the Arduino. It will send the message of what we have given to print in the input.
Parts Of List
Hardware Requirement
Arduino Uno
PIR Sensor
IR Sensor
Bread Board
Hook Up wires
Software Requirement
Arduino IDE
Python 3.5.2
PIR Sensor
Figure 1: PIR Sensor
It is used to detect human movement around approximately 10m from the sensor.
The actual range is between 5m to 12m.
It is of high sensitivity and low noise.
Programming
Arduino Programming
int
pirPin = 2;
int
minSecsBetweenEmails = 60;
// 1 min
long
lastSend = -minSecsBetweenEmails * 1000l;
void
setup()
{
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void
loop()
{
long
now = millis();
if
(digitalRead(pirPin) == HIGH)
{
if
(now > (lastSend + minSecsBetweenEmails * 1000l))
{
Serial.println(
"MOVEMENT"
);
lastSend = now;
}
else
{
Serial.println(
"Too soon"
);
}
}
delay(500);
}
Explanation
In the Arduino part, it has been explained about the distance of the person at the time, it will have the output at the Serial monitor.
The output will be displayed as "Too Soon".
Python Program
import time
import serial
import smtplib
TO =
'
[email protected]
'
GMAIL_USER =
'
[email protected]
'
GMAIL_PASS =
'putyourpasswordhere'
SUBJECT =
'Intrusion!!'
TEXT =
'Your PIR sensor detected movement'
ser = serial.Serial(
'COM4'
, 9600)
def send_email():
print(
"Sending Email"
)
smtpserver = smtplib.SMTP(
"smtp.gmail.com"
,587)
smtpserver.ehlo()
smtpserver.starttls()
smtpserver.ehlo
smtpserver.login(GMAIL_USER, GMAIL_PASS)
header =
'To:'
+ TO +
'\n'
+
'From: '
+ GMAIL_USER
header = header +
'\n'
+
'Subject:'
+ SUBJECT +
'\n'
print header
msg = header +
'\n'
+ TEXT +
' \n\n'
smtpserver.sendmail(GMAIL_USER, TO, msg)
smtpserver.close()
while
True:
message = ser.readline()
print(message)
if
message[0] ==
'M'
:
send_email()
time.sleep(0.5)
Explanation
In this Python coding, it will be used to connect the PIR sensor and will give the output in Python shield. It will also be displayed as Too Soon.
Output
Figure 2:Output
Arduino
IoT
PIR
Python
Up Next
Ebook Download
View all
Raspberry Pi -Sensorial Symphony of Connectivity
Read by 693 people
Download Now!
Learn
View all
Membership not found