ESP8266 - Motion Sensor

by DIYables in Circuits > Microcontrollers

13 Views, 0 Favorites, 0 Comments

ESP8266 - Motion Sensor

esp8266-motion-sensor-cover.jpg

This is a compact build guide. The full tutorial, with the wiring diagram and a line-by-line walkthrough of the code, is here: https://newbiely.com/tutorials/esp8266/esp8266-motion-sensor

Have you ever asked yourself, “How can it do that?” when you come across places with doors that open and close automatically, light bulbs that turn on and off, or escalators that activate without any human intervention? If so, this tutorial will not only answer your question, but also show you how to make it happen by using ESP8266 and motion sensor. Let's get started!

Supplies

What It Is

The HC-SR501 PIR sensor is a module that can sense the movement of people (or animals). It is often utilized in many applications to detect the presence of humans, such as automatically turning on/off a light bulb, opening/closing a door, activating/deactivating an escalator, or detecting an intruder.

Wiring

esp8266-motion-sensor-wiring-diagram.jpg

The HC-SR501 motion sensor has three pins:

GND pin: This must be linked to GND (0V)

VCC pin: This must be linked to VCC (5V)

OUTPUT pin: This is an output pin. It will be LOW when no motion is detected and HIGH when motion is detected. This pin needs to be connected to an ESP8266's input pin.

How It Works

The HC-SR501 sensor is able to detect motion based on changes in the infrared radiation coming from a moving object. In order for the HC-SR501 sensor to identify the object, two criteria must be met:

The object must be in motion or vibrating

The object must emit infrared radiation

Code

esp8266-motion-sensor.ino - the sketch from the tutorial, unchanged.

prev_motion_state = motion_state; // store old state
motion_state = digitalRead(MOTION_SENSOR_PIN); // read new state

if (prev_motion_state == LOW && motion_state == HIGH) { // pin state change: LOW -> HIGH
Serial.println("Motion detected!");
}

Full Tutorial

The complete tutorial, wiring diagram and code explanation: https://newbiely.com/tutorials/esp8266/esp8266-motion-sensor