Presence Sensor With NodeMCU ESP8266


Introduction: Presence Sensor With NodeMCU ESP8266

About: Do you like technology? Follow my channel on Youtube and my Blog. In them I put videos every week of microcontrollers, arduinos, networks, among other subjects. New tutorials on Tuesdays and Fridays.

Our goal today is to create a motion detection system in an environment using a PIR HC-SR501 presence sensor and a continuous buzzer to trigger as an alarm. Our assembly today looks like a little robot, but that's just because I used a soldering iron to fix the sensor and make it easier to view on the video. The microcontroller we use in this project is the WiFi ESP8266 NodeMcu ESP12E.

In the video, you can see that the circuit we set up is very simple. The NodeMCU is powered by USB, and this microcontroller is connected directly to the 5v buzzer. When a 3v3 output occurs on top of this buzzer, it works perfectly. As for the PIR sensor, it has positive / negative power, and it has the signal pin that only sends 0 or 1. It is pretty simple to move because it is binary. Next to it, we have two control trimpots, one for time and one for sensitivity.

Step 1: WiFi ESP8266 NodeMcu ESP-12E

Here, I will show you the photo of the NodeMCU, and all of its pinning.

Step 2: PIR HC-SR501 - Features

In the sensor image on the left, I point to where the two Trimpots are, which are the adjustment knobs. In the device that controls the time, you will adjust the delay, which is the time it will take for the assembly to reactivate after the alarm is triggered. With the sensitivity button, you will point out the trigger distance of the sensor. The right side shows the top view of the sensor.

The model PIR HC-SR501 also has three pins: VCC, GND, and OUT.

Power supply: from 5V to 20V.

Output Voltage: 3.3V.

The potentiometer is for time and sensitivity adjustment.

And, finally, the opening angle: approximately 120 degrees.

Step 3: Continuous Buzzer

Here, we have the buzzer that, for those who do not know, works as a kind of speaker. There are two types. We have a micro-controlled model, which is the one we use today, which receives power and beeps, in addition to the model that doesn’t have the microcontroller inside it, that is, it doesn’t have the oscillator inside it. Instead, you have to send the oscillation. The most common, however, is the first model I mentioned.

Step 4: Assembly

In this image, we have today’s design scheme. Let's connect the sensor to the NodeMCU via input D4. The output of the microcontroller to the buzzer is D1.

Step 5: Code

We begin by defining the constants we will use during our code.

//definição do pino de saída do sensor PIR
#define PIN_SENSOR D4 //definição do pino de entrada do Buzzer #define PIN_BUZZER D1


In the setup () function, we will configure the pins used for communicating with the sensor and the buzzer, by means of the "pinMode" command.

void setup() {
//abaixo vamos confirar cada um dos pinos como entrada ou saída de dados pinMode(PIN_SENSOR, INPUT); pinMode(PIN_BUZZER, OUTPUT); }


In the loop () function, we will read the sensor to see if it has detected any movement. If it is detected, we will trigger the buzzer to sound like an alarm.

void loop() {
//faz a leitura do sensor de presença (retorna HIGH ou LOW) int sinal = digitalRead(PIN_SENSOR); //HIGH : movimento detectado if(sinal == HIGH){ //aciona o Buzzer digitalWrite(PIN_BUZZER, HIGH); } //LOW : nenhum movimento detectado else{ //desativa o buzzer digitalWrite(PIN_BUZZER, LOW); } }



    • Water Contest

      Water Contest
    • Creative Misuse Contest

      Creative Misuse Contest
    • Oil Contest

      Oil Contest