• Skip to main content
  • Skip to primary sidebar

RNTLab.com

The Ultimate Shortcut to Learn Electronics and Programming with Open Source Hardware and Software

  • Courses
  • Forum
    • Forum
    • Ask Question
  • Shop
  • Account
  • Blog
  • Login

Motion is not detected in serial monitor

Q&A Forum › Category: Home Automation › Motion is not detected in serial monitor
0 Vote Up Vote Down
Iliuta Ovidiu Dragos asked 2 days ago

My PIR sensor does not detects motion, in unit 7.4 Motion Detector with Notifications, from Smart_Home ebook.
I used the code provided in ebook. I use the GPIO 4 from ESP 32 for PIR out, VCC to 3v3 ESP PIN, GND to GND.  I tried to test the sensor on other simple example and it worked.

Question Tags: Arduino IDE, ESP32, home automation, PIR sensor
3 Answers
0 Vote Up Vote Down
Sara Santos Staff answered 1 day ago

Hi.
Can you share with me the example that worked?
 
Regards,
Sara

0 Vote Up Vote Down
Iliuta Ovidiu Dragos answered 1 day ago

Hi, this is the code that works.

Start your code here

#include <Arduino.h>
#include <WiFi.h>
extern "C" {
#include "freertos/FreeRTOS.h"
#include "freertos/timers.h"
}
#include <AsyncMqttClient.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
// WiFi
#define WIFI_SSID ""
#define WIFI_PASSWORD ""
// MQTT
#define MQTT_HOST IPAddress()
#define MQTT_PORT 1883
#define BROKER_USER ""
#define BROKER_PASS ""
#define MQTT_PUB_TEMP "esp/bme280/temperature"
#define MQTT_PUB_HUM "esp/bme280/humidity"
#define MQTT_PUB_PRES "esp/bme280/pressure"
#define MQTT_PUB_MOTION "esp/motion/notification"
// PIR
const int PIR_PIN = 4;
// BME280
Adafruit_BME280 bme;
// MQTT
AsyncMqttClient mqttClient;
TimerHandle_t mqttReconnectTimer;
TimerHandle_t wifiReconnectTimer;
// Publish interval
unsigned long previousMillis = 0;
const long interval = 10000;
// PIR debounce
volatile bool pirFlag = false;
volatile unsigned long lastPirTime = 0;
const unsigned long PIR_DEBOUNCE = 2500;
// ===== ISR PIR =====
void IRAM_ATTR detectsMovement() {
unsigned long now = millis();
if (now - lastPirTime > PIR_DEBOUNCE) {
pirFlag = true;
lastPirTime = now;
}
}
// ===== WIFI =====
void connectToWifi() {
Serial.println("Connecting to Wi-Fi...");
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
}
void WiFiEvent(WiFiEvent_t event) {
Serial.printf("[WiFi-event] event: %d\n", event);

switch(event) {
case ARDUINO_EVENT_WIFI_STA_GOT_IP:
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
mqttClient.connect();
break;
case ARDUINO_EVENT_WIFI_STA_DISCONNECTED:
Serial.println("WiFi lost connection");
xTimerStop(mqttReconnectTimer, 0);
xTimerStart(wifiReconnectTimer, 0);
break;
}
}
// ===== MQTT =====
void connectToMqtt() {
Serial.println("Connecting to MQTT...");
mqttClient.connect();
}
void onMqttConnect(bool sessionPresent) {
Serial.println("Connected to MQTT.");
}
void onMqttDisconnect(AsyncMqttClientDisconnectReason reason) {
Serial.println("Disconnected from MQTT.");
if (WiFi.isConnected()) {
xTimerStart(mqttReconnectTimer, 0);
}
}
// ===== SETUP =====
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println();
pinMode(PIR_PIN, INPUT_PULLDOWN);
attachInterrupt(digitalPinToInterrupt(PIR_PIN), detectsMovement, RISING);
if (!bme.begin(0x77)) {
Serial.println("Could not find a valid BME280 sensor!");
while (1);
}
mqttReconnectTimer = xTimerCreate("mqttTimer", pdMS_TO_TICKS(2000), pdFALSE, 0, (TimerCallbackFunction_t)connectToMqtt);
wifiReconnectTimer = xTimerCreate("wifiTimer", pdMS_TO_TICKS(2000), pdFALSE, 0, (TimerCallbackFunction_t)connectToWifi);
WiFi.onEvent(WiFiEvent);
mqttClient.onConnect(onMqttConnect);
mqttClient.onDisconnect(onMqttDisconnect);
mqttClient.setServer(MQTT_HOST, MQTT_PORT);
mqttClient.setCredentials(BROKER_USER, BROKER_PASS);
connectToWifi();
}
// ===== LOOP =====
void loop() {
// ======= PIR MQTT =======
if (pirFlag) {
uint16_t packetIdPub = mqttClient.publish(MQTT_PUB_MOTION, 1, true, "Motion Detected!");
Serial.printf("Publishing on topic %s at QoS 1, packetId: %i Motion Detected! \n", MQTT_PUB_MOTION, packetIdPub);
pirFlag = false;
}
// ======= BME280 MQTT =======
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure() / 100.0F;
uint16_t id1 = mqttClient.publish(MQTT_PUB_TEMP, 1, true, String(t).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %iMessage: %.2f \n", MQTT_PUB_TEMP, id1, t);
uint16_t id2 = mqttClient.publish(MQTT_PUB_HUM, 1, true, String(h).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId %i: Message: %.2f \n", MQTT_PUB_HUM, id2, h);
uint16_t id3 = mqttClient.publish(MQTT_PUB_PRES, 1, true, String(p).c_str());
Serial.printf("Publishing on topic %s at QoS 1, packetId: %iMessage: %.3f \n", MQTT_PUB_PRES, id3, p);
}
}

0 Vote Up Vote Down
Sara Santos Staff answered 5 hours ago

Hi.
That’s weird… because the only thing that you changed seems to be adding a debounce timer to the PIR sensor, and changing the GPIO.
 
Maybe that GPIO doesn’t work as the one we use in our code (GPIO 26)??…
 
I’m not sure exactly why the other example didn’t work for you…
Regards,
Sara

Primary Sidebar

Login to Ask or Answer Questions

This Forum is private and it’s only available for members enrolled in our Courses.

Login »

Latest Course Updates

  • [eBook Updated] Smart Home with Raspberry Pi, ESP32, and ESP8266 V1.6 September 9, 2025
  • [eBook Updated] Learn LVGL: Build GUIs for ESP32 Projects – Version 1.5 July 7, 2025

You must be logged in to view this content.

Contact Support - Refunds - Privacy - Terms - MakerAdvisor.com - Member Login

Copyright © 2013-2025 · RandomNerdTutorials.com · All Rights Reserved

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.