PIR Sensor with IFTTT Integration: Trigger Anything in Your Smart Home

Project Overview

This project connects a PIR sensor to IFTTT (If This Then That), allowing you to trigger thousands of smart home actions. When motion is detected, you can turn on lights, send notifications, log data to spreadsheets, control smart plugs, or even trigger security cameras.

Difficulty: Intermediate
Estimated time: 1-2 hours
Estimated cost: $15-25

How It Works

An ESP32 detects motion from a PIR sensor and sends an HTTP request to IFTTT’s Webhooks service. IFTTT then triggers any action you’ve configured. Since the ESP32 uses Wi-Fi, it needs to be within range of your network, but the IFTTT integration works from anywhere.

Materials Needed

  • ESP32 or ESP8266 (1)
  • HC-SR501 PIR sensor (1)
  • Jumper wires
  • Power supply (5V USB)
  • IFTTT account (free)

IFTTT Setup

  1. Create an account at ifttt.com (free).
  2. Click your profile → CreateIf This.
  3. Search for and select Webhooks.
  4. Select Receive a web request.
  5. Enter an event name (e.g., motion_detected) → Create trigger.
  6. Click Then That and choose your desired action (e.g., Philips Hue, Smart Life, Email, SMS).
  7. Configure the action (e.g., turn on living room light).
  8. Click ContinueFinish.
  9. Go to Webhooks service page → Documentation to get your API key.

Circuit Diagram

Connection Table

Arduino Code

// PIR Sensor with IFTTT Integration
#include <WiFi.h>
#include <HTTPClient.h>

// Wi-Fi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";

// IFTTT configuration
const char* iftttKey = "YourIFTTTKey";
const char* eventName = "motion_detected";

const int pirPin = 4;

unsigned long lastTriggerTime = 0;
const unsigned long minInterval = 10000; // 10 seconds between triggers

void setup() {
  Serial.begin(115200);
  pinMode(pirPin, INPUT);
  
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("WiFi connected");
  
  Serial.println("IFTTT Motion Sensor Ready");
  delay(60000); // PIR warm-up
}

void triggerIFTTT() {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = "http://maker.ifttt.com/trigger/" + String(eventName) + "/with/key/" + String(iftttKey);
    http.begin(url);
    int httpCode = http.GET();
    
    if (httpCode > 0) {
      Serial.printf("IFTTT trigger sent, response: %d\n", httpCode);
    } else {
      Serial.printf("IFTTT trigger failed: %s\n", http.errorToString(httpCode).c_str());
    }
    http.end();
  }
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  
  if (motion && (millis() - lastTriggerTime > minInterval)) {
    lastTriggerTime = millis();
    Serial.println("Motion detected - triggering IFTTT");
    triggerIFTTT();
  }
  
  delay(100);
}

Enhanced Version with Multiple Actions

Send additional data like time and sensor ID:

void triggerIFTTTWithData(int sensorId) {
  if (WiFi.status() == WL_CONNECTED) {
    HTTPClient http;
    String url = "http://maker.ifttt.com/trigger/" + String(eventName) + "/with/key/" + String(iftttKey);
    http.begin(url);
    http.addHeader("Content-Type", "application/json");
    
    String payload = "{\"value1\":\"" + String(sensorId) + "\",\"value2\":\"" + String(millis()) + "\",\"value3\":\"Motion\"}";
    int httpCode = http.POST(payload);
    
    http.end();
  }
}

Popular IFTTT Actions for PIR Sensors

Notifications

  • Send SMS via Twilio
  • Send push notification via IFTTT app
  • Send email to yourself or family
  • Post to Twitter or Facebook

Smart Home Control

  • Turn on Philips Hue lights
  • Activate TP-Link Kasa smart plug
  • Control Sonos speakers
  • Trigger Ring camera recording
  • Open/close smart blinds

Data Logging

  • Add row to Google Sheets
  • Log to Google Drive
  • Post to Adafruit IO for graphing

Low-Power Battery Version

For battery operation, add deep sleep:

#include <esp_sleep.h>

void setup() {
  // ... existing setup ...
  esp_sleep_enable_ext0_wakeup((gpio_num_t)pirPin, 1);
}

void loop() {
  bool motion = digitalRead(pirPin) == HIGH;
  if (motion) {
    triggerIFTTT();
    delay(5000);
  }
  esp_deep_sleep_start();
}

Installation Steps

  1. Set up IFTTT account and create Webhooks trigger.
  2. Create desired actions (lights, notifications, etc.).
  3. Get IFTTT API key from Webhooks documentation.
  4. Update code with Wi-Fi credentials and IFTTT key.
  5. Upload to ESP32 and test with hand motion.
  6. Verify IFTTT triggers in the activity log.
  7. Place sensor in desired location.

Project Extensions

  • Multiple sensors: Use different event names for each sensor.
  • Temperature data: Add DHT22 to send temperature with motion events.
  • Schedule filtering: Only trigger during certain hours.
  • Google Sheets logging: Create a spreadsheet to track all motion events.
  • Dashboard: Create a Google Data Studio dashboard from your spreadsheet.

Troubleshooting

  • No IFTTT trigger: Check API key and event name. Verify Wi-Fi connection.
  • Delayed triggers: IFTTT has a few seconds delay; this is normal.
  • Multiple triggers: Increase minInterval to prevent rapid fire.
  • Wi-Fi not connecting: Check credentials. ESP32 may need external antenna for remote locations.

Example: Google Sheets Logger

Create a Google Sheets log of all motion events:

  1. Create a new Google Sheet.
  2. Set up IFTTT action “Google Sheets” → “Add row to spreadsheet”.
  3. Configure columns: Timestamp, Sensor ID, Event Type.
  4. Each motion will add a new row with date/time.

Conclusion

This IFTTT integration opens up countless possibilities for your PIR sensor. With minimal coding, you can connect your sensor to virtually any smart home device or online service.

Leave a Reply

Your email address will not be published. Required fields are marked *

Component Pin ESP32 Pin
PIR Sensor VCC 3.3V PIR Sensor GND GND PIR Sensor OUT GPIO 4