Creating a motion detector with Arduino NodeMCU ESP
In this unit we will see how to create a motion detector with an Arduino board. A useful object in order to detect when there is something moving in a room. This kind of object is typically used in hotels when there is no employee at the reception and
they want to be notified when a guest is coming.
As we will need to connect this object to the internet we won't use the Arduino Uno board but instead the
We will see in this course how you can also connect it to the internet in order to make it even more valuable.
What we will need:
- Arduino Node MCU.
- PIR motion sensor.
- 3 male to female jumper cables.
Optionnal:
- 1 led (you can also use the one of the Arduino card but it is smaller)
- 1 male to male jumper cable (optionnal).
- 1 resistor to avoid your LED to burn.
From: https://www.instructables.com/id/NodeMCU-ESP8266-Details-and-Pinout/
Here is the code:
int led = 12; // we are inserting the led on the pin 12 which is pin D6 on esp
int sensor = 13; // we are inserting the pir on the pin 13 which is pin D7 on esp
void setup() { // initial status of the Arduino board
pinMode(sensor,
INPUT); // declare sensor as input
pinMode(led, OUTPUT); // declare led as output
Serial.begin(115200); // frequence at which the messages will be written in the console
}
void loop() { // what the Arduino
will do continuously
long state = digitalRead(sensor); // a variable named state takes the value of the sensor
if(state == HIGH) { // if there is something passing through
digitalWrite
(led, HIGH); // put the led on
Serial.println("Motion detected!"); // print the message motion detected
delay(2000); // wait 2 seconds
}
else { // if nothing is passing through
digitalWrite (led, LOW); // let the led off
Serial.println("Motion absent!"); // display a message saying that there is no motion detected
delay(2000); // wait 2 seconds
}
}