In some situations we may need motion detection for ease of use or security. The microwave radar sensor RCWL-0516, which works on the principle of the Doppler effect, is of great help in this.
These will be required:
RCWL-0516 Motion Detection, Microwave Radar Sensor
HC-SR501 PIR Motion Detector Sensor
This sensor also works on its own, its output is 3.3V for 2 seconds when motion is detected. However, we are now using a NodeMCU ESP8266 to send the information to the ioBroker via the MQTT protocol.
You can then start a corresponding event on ioBroker, e.g. B. turn on a light, send a push message and much more.
The circuit is not complicated either:

And the sample code for the RCWL-0516 microwave sensor:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "192.168.X.XXX";
int DetectionPin = D5;
bool Detection = false;
bool previousDetection = false;
WiFiClient espClient;
PubSubClient client(espClient);
void reconnect()
{
while (!client.connected())
{
String clientId = "ESP8266_Radar";
if (client.connect(clientId.c_str()))
{
// pass
}
else
{
delay(6000);
}
}
}
void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
client.setServer(mqtt_server, 1883);
pinMode (DetectionPin, INPUT);
}
void loop()
{
if(!client.connected())
{
reconnect();
}
client.loop();
Detection = digitalRead(DetectionPin);
if(Detection == true)
{
if(previousDetection == false)
{
client.publish("motion", "true");
previousDetection == false;
}
}
else
{
if(previousDetection == true)
{
client.publish("motion", "false");
previousDetection == false;
}
}
}
You can also replace the RCWL-0516 with the PIR sensor HC-SR501. Don’t forget to change the code, however, as the HC-SR501’s output will be low when motion is detected and therefore the opposite of how our radar sensor works.
We will see:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "SSID";
const char* password = "PASSWORD";
const char* mqtt_server = "192.168.X.XXX";
int PirPin = D5;
bool Detection = false;
bool previousDetection = false;
WiFiClient espClient;
PubSubClient client(espClient);
void reconnect()
{
while (!client.connected())
{
String clientId = "ESP8266_HC-SR501_PIR";
if (client.connect(clientId.c_str()))
{
// pass
}
else
{
delay(6000);
}
}
}
void setup()
{
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
client.setServer(mqtt_server, 1883);
pinMode (PirPin, INPUT);
}
void loop()
{
if(!client.connected())
{
reconnect();
}
client.loop();
Detection = digitalRead(PirPin);
if(Detection == false)
{
if(previousDetection == false)
{
client.publish("motion", "true");
previousDetection == false;
}
}
else
{
if(previousDetection == true)
{
client.publish("motion", "false");
previousDetection == false;
}
}
}
The connection is as follows:




