1. Home
  2. ESP-8266
  3. Sensors
  4. PIR Motion Sensor HC-SR501

PIR Motion Sensor HC-SR501

There are cheap PIR motion sensors available for the ESP8266. The HC-SR501 is such a module. It can be configured through two potis at the back regarding sensitivity and delay after a motion detection, before the output is reset again.

The sensor has three pins: One for +5V, one for ground, and the middle pin is a 3.3v output, which can be connected to a digital input pin from the ESP. Getting notified about motion is very easy like this.

 

Example code to connect the sensor:


inpin=1
gpio.mode(inpin, gpio.INT)

startTime = 0

function motionStart()
start=tmr.time()
print('Motion detected!')
tmr.delay(1000000)
gpio.trig(inpin, "down", motionStop)
end

function motionStop()
duration = tmr.time() - start
tmr.delay(1000000)
gpio.trig(inpin, "up", motionStart)
end

gpio.trig(inpin, "up", motionStart)

Updated on February 11, 2017

Was this article helpful?

Related Articles