The topic of today's lesson is a motion sensor based on the pyroelectric effect (PIR, passive infrared motion sensor). Such sensors are often used in security systems and in everyday life to detect movement in a room. For example, the principle of motion detection is based on the automatic switching on of the light in the entrance or in the bathroom. Pyroelectric sensors are quite simple, inexpensive and unpretentious in installation and maintenance. By the way, there are other ways to detect motion. Today, computer vision systems are increasingly used to recognize objects and the trajectory of their movement. In the same security systems, laser detectors are used, which give an alarm signal when the beam is crossed. Thermal imaging sensors are also used, capable of detecting the movement of only living beings.
HC-SR501 | GND | VCC | OUT |
Arduino Uno | GND | +5V | 2 |
PIR (passive infrared sensors) sensors react to movement, through which they are often used in alarm systems. These sensors are small, cheap, energy-saving, easy to operate, and virtually indestructible. In addition to PIR, such sensors are called pyroelectric and infrared motion sensors.
PIR motion sensors essentially consist of a pyroelectric sensing element (cylindrical part with a rectangular crystal in the center) that detects the level of infrared radiation. Everything around emits a small level of radiation. How more temperature, the higher the radiation level. The sensor is actually divided into two parts. This is due to the fact that it is not the level of radiation that is important to us, but directly the presence of movement within its sensitivity zone. The two parts of the sensor are set up so that if one half picks up more radiation than the other, the output signal will generate a high or low value.
PIR sensors are great for projects that need to detect the presence or absence of a person within a specific workspace. In addition to the advantages of such sensors listed above, they have a large sensitivity zone. However, please note that pyroelectric sensors will not give you information about how many people are around and how close they are to the sensor. In addition, they can work on pets.
These specifications refer to PIR sensors sold in the Adafruit store. The principle of operation of similar sensors is similar, although the technical specifications may differ. So before working with a PIR sensor, check out its datasheet.
PIR sensors are not as simple as it might seem at first glance. The main reason - a large number of variables that affect its input and output signals. To explain the basics of how PIR sensors work, we use the figure below.
The pyroelectric motion sensor consists of two main parts. Each part includes a special material that is sensitive to infrared radiation. In this case, the lenses do not particularly affect the operation of the sensor, so we see two areas of sensitivity of the entire module. When the sensor is at rest, both sensors detect the same amount of radiation. For example, this could be room radiation or environment on the street. When a warm-blooded object (human or animal) passes by, it crosses the sensitivity zone of the first sensor, as a result of which two different meanings radiation. When a person leaves the sensitivity zone of the first sensor, the values are aligned. It is the changes in the readings of the two sensors that are recorded and generate HIGH or LOW pulses at the output.
The sensitive elements of the PIR sensor are installed in a metal hermetic case, which protects against external noise, temperature changes and humidity. The rectangle in the center is made of a material that lets through infrared radiation(usually a silicone-based material). Behind this plate are two sensitive elements.
Figure from Murata datasheet:
Infrared motion sensors are almost identical in their structure. The main differences are sensitivity, which depends on the quality of the sensitive elements. In this case, optics play a significant role.
The figure above shows an example of a plastic lens. This means that the sensitivity range of the sensor consists of two rectangles. But, as a rule, we need to provide large viewing angles. To do this, you can use lenses similar to those used in cameras. In this case, the lens for the motion sensor should be small, thin and made of plastic, although it adds noise to the measurements. Therefore, most PIR sensors use Fresnel lenses (figure from Sensors Magazine):
Fresnel lenses concentrate radiation, significantly expanding the sensitivity range of pyro sensors (Figure from BHlens.com)
Figure from Cypress appnote 2105:
Now we have a much larger sensitivity range. At the same time, we remember that we have two sensitive elements and we need not so much two large rectangles as a large number of small sensitivity zones. To do this, the lens is divided into several sections, each of which is a separate Fresnel lens.
Most infrared motion sensor modules have three connectors on the back. The pinout may vary, so check it before connecting! Usually, corresponding inscriptions are made next to the connectors. One connector goes to the ground, the second gives the signal we are interested in from the sensors, the third - the ground. The supply voltage is typically 3-5 volts DC. However, sometimes there are sensors with a supply voltage of 12 volts. Some large sensors do not have a separate signal pin. Instead, a relay with ground, power, and two switches is used.
To prototype your device using infrared motion sensor, it is convenient to use circuit board, since most of these modules have three connectors, the distance between which is calculated exactly for the breadboard holes.
In our case, the red cable corresponds to power, black to ground, and yellow to signal. If you connect the cables incorrectly, the sensor will not fail, but it will not work.
Assemble the circuit according to the picture above. As a result, when the PIR sensor detects movement, a HIGH signal will be generated at the output, which corresponds to 3.3 V, and the LED will light up.
Please note that the pyroelectric sensor must "stabilise". Install batteries and wait 30-60 seconds. During this time, the LED may flash. Wait until the blinking stops and you can start waving your arms and walking around the sensor, watching the LED light up!
Many infrared motion sensors, including those from Adafruit, have a small potentiometer to adjust the sensitivity. Turning the potentiometer clockwise adds sensitivity to the sensor.
When we consider PIR sensors, two "delay" times are important. First time -Tx: how long the LED stays on after motion is detected. On many pyroelectric modules, this time is controlled by a built-in potentiometer. The second time interval is Ti: how long the LED is guaranteed not to light up when there was no movement. Changing this parameter is not so easy, you may need a soldering iron for this.
Let's take a look at the BISS datasheet:
Sensors from Adafruit have a potentiometer marked TIME. This is a 1 mega ohm variable resistor added to the 10 k ohm resistors. Capacitor C6 has a capacitance of 0.01uF, so:
Tx = 24576 x (10 kOhm + Rtime) x 0.01 uF
When the Rtime potentiometer is in the "zero" - fully counter-clockwise - position (0 MΩ):
Tx = 24576 x (10 kΩ) x 0.01 uF = 2.5 seconds (approx.) When the Rtime potentiometer is turned fully clockwise (1 MΩ):
Tx = 24576 x (1010 kΩ) x 0.01 uF = 250 seconds (approx.)
In the middle position of RTime, the time will be about 120 seconds (two minutes). That is, if you want to track the movement of an object at a rate of once per minute, turn the potentiometer 1/4 turn.
Let's write a program for reading values from a pyroelectric motion sensor. Connecting the PIR sensor to the microcontroller is simple. The sensor outputs a digital signal, so all you need to do is read the HIGH (motion detected) or LOW (no motion) signal from the Arduino pin.
When doing this, do not forget to set the connector to position H!
Apply 5 volts to the sensor. The earth is connected to the earth. After that, connect the signal pin from the sensor to the digital pin on the Arduino. IN this example pin 2 is used.
The program is simple. In fact, it monitors the state of pin 2. Namely: what signal is on it: LOW or HIGH. In addition, a message is displayed when the state of the pin changes: there is movement or there is no movement.
* check PIR motion sensor
int ledPin = 13; // initialize the pin for the LED
int inputPin = 2; // initialize the pin to receive a signal from the pyroelectric motion sensor
int pirState = LOW; // start the program, assuming there is no movement
intval = 0; // variable for reading pin state
pinMode(ledPin, OUTPUT); // declare LED as OUTPUT
pinMode(inputPin, INPUT); // declare the sensor as an INPUT
Serial.begin(9600);
val = digitalRead(inputPin); // read the value from the sensor
if (val == HIGH) ( // check if the read value is HIGH
digitalWrite(ledPin, HIGH); // turn on the LED
if (pirState == LOW) (
// we just included
Serial.println("Motion detected!");
pirState=HIGH;
digitalWrite(ledPin, LOW); // turn off the LED
if (pirState == HIGH)(
// we just turned it off
Serial.println("Motion ended!");
// we're displaying the change, not the state, on the serial monitor
Do not forget that a microcontroller is not always needed to work with a pyroelectric sensor.
Today we will analyze the project of connecting a PIR sensor (motion) to Arduino and organize automatic sending of e-mail when the sensor is triggered. Arduino is the heart of this project - it reads the IR sensor, and when motion is detected, it gives it to the computer via USB port command to send a letter. Processing of the signal coming to the computer is implemented using the Python program.
To build the project described in this tutorial, you will need the following parts:
You will also need a computer with an Internet connection, through which we will send e-mail! The role of the computer in this lesson can be played.
Only the PIR sensor needs to be connected to the Arduino in this project, so the wires from the sensor can be connected directly to the Arduino. But since in this case, it is more convenient to hold the wires a little looser using the c brainboard scheme:
The Arduino will send a message over the USB Serial connection when motion is detected. But if you send an e-mail every time the sensor is triggered, you can get a huge amount of letters. Therefore, if too little time has passed since the last signal, we will send another message.
int pirPin = 7;
int minSecsBetweenEmails = 60; // 1 min
long lastSend = -minSecsBetweenEmails * 1000;
void setup()
{
pinMode(pirPin, INPUT);
Serial.begin(9600);
}
void loop()
{
long now = millis();
if (digitalRead(pirPin) == HIGH)
{
if (now > (lastSend + minSecsBetweenEmails * 1000))
{
Serial.println("MOVEMENT"); lastSend = now;
}
else
{
Serial.println("Too soon"); )
}
delay(500);
}
The "minSecsBetweenEmails" variable can be changed to another reasonable value. In the example it is set to 60 seconds and emails will not be sent for more than one minute. To keep track of when last time the command to send e-mail was given, the variable "lastSend" is used. We initialize it with a negative number, equal to the number milliseconds specified in the "minSecsBetweenEmails" variable. This ensures that we handle the PIR sensor triggering as soon as the Arduino sketch is launched. The loop uses the Millis() function to get the number of milliseconds from the Arduino and compare it to the time since the last sensor triggered and the corresponding "MOVEMENT" message sent. If the comparison shows that too little time has passed since the last sensor operation, then despite the fact that the movement was detected, we send the message "Too soon" (Too early). Before writing a Python program to process the signal coming from the Arduino to a computer or Raspberry Pi via USB, you can check the operation of the program on the Arduino by simply opening the Serial Monitor on Arduino IDE.
If the project uses a Linux computer, such as a Raspberry Pi, Python is already installed. If you are using a computer with an operating Windows system, then Python needs to be installed. In any case, you will need to install the PySerial library in order to communicate with the Arduino.
To install Python on Windows, download the installer from https://www.python.org/downloads/ . There have been reports of problems with PySerial on Windows when using Python 3, so we are using Python 2. Once Python is installed, a group will appear in the Start menu. But to install PySerial, you will need to use Python from command line, so let's add the appropriate directory to the Windows PATH variable. To do this, you need to go to the Windows Control Panel, find System Properties (System Properties). Then click on the button labeled Environment Variabes and in the window that appears select Path at the bottom of System variables. Press the Edit button, and then at the end of the "Variable value", without deleting the existing text, add "; C:\Python27". Do not forget ";" after each specified folder. To check that the PATH variable has been changed correctly, at the command line, enter the command "python". A similar picture should appear:
Regardless of the used operating system, download the .tar.gz installation package for PySerial 2.6 from https://pypi.python.org/pypi/pyserial Get a file called pyserial-2.6.tar.gz When using Windows you need to unzip the file into a folder. Unfortunately, this is not a regular ZIP file, so you may need to download eg 7-zip (http://www.7-zip.org/). When using a Linux computer, such as when using a Raspberry Pi in this project, you need to open a terminal session, execute the "CD" command specifying the folder where pyserial-2.6.tar.gz was downloaded, and then run the following command to unpack the installer :
$ tar -xzf pyserial-2.6.tar.gz
Next, regardless of the operating system used, on the command line, execute the “CD” command specifying the pyserial-2.6 folder and execute the command:
sudo python setup.py install
Now let's create a Python program. To do this, copy given code to a file named "movement.py". On Linux a "nano" editor can be used, on Windows probably the easiest way to make a file is with the Python 'IDLE' editor (accessible from the Python program group on the start menu).
import time
import serial
import smtplib
TO=" [email protected]"
GMAIL_USER=" [email protected]"
GMAIL_PASS = "putyourpasswordhere"
SUBJECT = "Intrusion!!"
TEXT = "Your PIR sensor movement detected"
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 == "M" :
send_email()
time.sleep(0.5)
Before running the Python program, we make some changes (all of them at the top of the program). The program assumes that emails are generated from a Gmail account. If it doesn't exist, register it (even if it's only for this project). Change the value of the "TO" variable to the email address where notifications will be sent. We change the value of "GMAIL_USER" to the Gmail email address and, accordingly, the password in the next line (GMAIL_PASS). You can also change the subject and text of the message to be sent ("SUBJECT" and "TEXT"). You need to set the serial port that the Arduino is connected to in the line ser = serial.Serial("COM4", 9600) For Windows, it will be something like "COM4" for Linux - something like "/dev/tty.usbmodem621" . To which port of the computer the board is connected, we look in the Arduino IDE in the lower right corner. After these changes, run the program from the command line / terminal: python movement.py Done! When the PIR sensor is triggered, a message will be sent to the specified e-mail shortly.
Now, having mastered the means of sending email with Arduino, you can start expanding the capabilities of the project. You can add other sensors, and, for example, send yourself e-mail hourly temperature reports. Of course, the PIR sensor can be used directly with the Arduino without connecting to a computer. In this case, when the sensor is triggered, you can turn on a warning sound signal, flash the LED, or turn on the lighting in the room (via a high-voltage relay).
In this tutorial, we will show you how to make a motion sensor using an ultrasonic sensor (HC-SR04) that will turn on the LED every time. Components for this lesson can be ordered at any convenient store, and eventually on our website.
The lesson is suitable for beginners, but it will also be interesting for more experienced engineers.
Below is the entire list of components that will be useful to us for our lesson.
1 x Arduino board(we used Arduino Uno)
1 x LED (LED, color doesn't matter)
1 x Resistor/resistance 220 ohm
1 x development board
1 x Arduino USB cable
1 x 9V battery with clip (optional)
6 x Leads
First, connect the ultrasonic sensor and LED on the breadboard. Connect the short LED cable (cathode) to the GND (ground) pin of the sensor. Then install the resistor in the same row as the longer LED wire (anode) so they are connected.
Now you need to connect some wires on the back of the sensor. There are four pins - VCC, TRIG, ECHO and GND. After inserting the wires, you need to make the following connections:
The resistor end to the digital pin of your choice, just remember to change it later in the code.
Sensor -> Arduino
VCC -> 5V (power)
TRIG -> 5*
ECHO -> 4*
GND -> GND (ground)
* - can be connected to any two digital pins Arduino, just make sure you change them in the code later.
Now you can connect your Arduino to your computer with a USB cable. open software Arduino and download the code you can find below. The constants are commented, so you know exactly what they do and may change them.
Const int ledPin = 6; // LED digital output const int trigPin = 5; // Digital output for connecting TRIG const int echoPin = 4; // Digital output for connecting ECHO const int ledOnTime = 1000; // Time the LED stays on after motion is detected (in milliseconds, 1000ms = 1s) const int trigDistance = 20; // Distance (and smaller value) at which the sensor is triggered (in centimeters) int duration; int distance; void setup() ( pinMode(ledPin, OUTPUT); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); ) void loop() ( digitalWrite(trigPin, LOW); digitalWrite(trigPin, HIGH); delay(1) ; digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = duration * 0.034 / 2; if (distance<= trigDistance) { digitalWrite(ledPin, HIGH); delay(ledOnTime); digitalWrite(ledPin, LOW); } delay(100); }
The final result of the motion sensor and its operation can be seen in the video below.
All good projects!
PrincipleworkPIR (Passive Infra Red)-sensors
Any object with a certain temperature becomes a source of electromagnetic (thermal) radiation, including the human body. The wavelength of this radiation depends on temperature and is in the infrared part of the spectrum. This radiation is invisible to the eye and is captured only by sensors. They are also called PIR sensors.
This is an abbreviation for the words "passive infrared" or "passive infrared" sensors. Passive - because the sensors themselves do not emit, but only perceive radiation with a wavelength of 7 to 14 µm.
The person radiates warmth. Its infrared thermal image shows the temperature distribution over the surface of the body. Hotter objects look lighter, colder objects look darker, because. radiate less heat.
The PIR sensor contains a sensitive element that reacts to changes in thermal radiation. If it remains constant, no electrical signal is generated.
In order for the sensor to respond to movement, special lenses (Fresnel lenses) are used with several focusing areas, which break the overall thermal picture into active and passive zones arranged in a checkerboard pattern. A person, being in the field of operation of the sensor, occupies several active zones in whole or in part.
Therefore, even with minimal movement, there is a movement from one active zone to another, which triggers the sensor. The background thermal picture, as a rule, changes very slowly and evenly. The sensor does not respond to it. The high density of active and passive zones allows the sensor to reliably detect the presence of a person even with the slightest movement.
nanbaby.ru - Health and beauty. Fashion. Children and parents. Leisure. Gen. House