First, The Question: What is a PIR sensor?
PIR Sensor with mounted Fresnel Lens |
A PIR module is basically made of a pyroelectric sensor (see it below as the round metal can with a rectangular crystal in the center), which can detect levels of infrared radiation. Everything emits some low level radiation, and the hotter something is, the more radiation is emitted. The sensor in a motion detector is actually split in two halves. The reason for that is that we are looking to detect motion (change) not average IR levels. The two halves are wired up so that they cancel each other out. If one half sees more or less IR radiation than the other, the output will swing high or low.
PIR Sensor uncovered |
Bottom Side |
Micro Power PIR Motion Detector IC |
Holtek 3.3V LDO power regulator |
- CBDB Board
- USB adapter (take a look on Part 1 for details how to connect them together)
- PIR Module from above
- LED module x 2
- Main power switch module
PIR Motion Detection Software
For programming CBDB Board and uploading the driver and the software we will continue to use the LuaUploader as before.
PIR “Blinky” running |
gpio.mode(outpin,gpio.OUTPUT)inpin=6 — Select input pin – GPIO12
gpio.mode(inpin,gpio.INT,gpio.PULLUP) — attach interrupt to inpin
print(“Motion Detected!”)
gpio.write(outpin,gpio.HIGH) — Led ON – Motion detected
tmr.delay(5000000) — delay time for marking the movement
gpio.write(outpin,gpio.LOW) — Led OFF
end3. Trigger INPIN on rising edge – based on internal interrupt mechanism
If everything looks great we can move to the next part
Example 2: Web enabled PIR Motion detection
To be able to see online the PIR sensor status we will add a Web Server component to our program
2.1. Web Server
srv=net.createServer(net.TCP)
srv:listen(80,
function(conn)
conn:on(“receive”,function(conn,payload) print(payload)
conn:send(“HTTP/1.1 200 OK\n\n”)
conn:send(“<META HTTP-EQUIV=\”REFRESH\” CONTENT=\”2\”>”)
conn:send(“<html><title>PIR Motion Detector Server – ESP8266</title><body>”)
conn:send(“<h1>PIR Motion Detector Server – ESP8266</h1><BR>”)
conn:send(‘Status: ‘)
if (stat == “ON”) then conn:send(‘<B><font color=red>Movement Detected!</font></B>’)
elseif (stat == “OFF”) then conn:send(‘<B><font color=green>No Movement</font></B>’)
else
conn:send(stat)
conn:send(‘%’)
end
conn:send(“<BR><BR><br>Node.HEAP : <b>” .. node.heap() .. “</b><BR><BR>”)
conn:send(“IP ADDR : <b>”.. wifi.sta.getip() .. “</b><BR>”)
conn:send(“TMR.NOW : <b>” .. tmr.now() .. “</b><BR<BR><BR>”)
conn:send(“</html></body>”)
conn:on(“sent”,function(conn) conn:close() end)
end)
end)
2.2 Motion Detection function.
Splitted in 2 separate functions, to easier understand the mechanism behind:
2.2.1 Trigger on rising edge
function motion()
print(“Motion Detection : ON!”)
stat = “ON”
gpio.write(outpin,gpio.HIGH) — Led ON – Motion detected
gpio.trig(6,”down”,nomotion) — trigger on falling edge
return stat
end
2.2.2 Trigger on falling edge
function nomotion()
print(“Motion Detection : OFF!”)
stat = “OFF”
gpio.write(outpin,gpio.LOW) — Led OFF
gpio.trig(6,”up”,motion) — trigger on rising edge
return stat
end
Save the code on ESP as ‘pir.lua‘, restart ESP and run:
Example 2 Video:
Take a look on the previous project for connections. After properly connecting it, rerun Example 2.
You have a fully functional MAINS power switch with motion detection: