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 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 have 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
- Create an account at ifttt.com
- Click Create → If This
- Search for and select Webhooks
- Select Receive a web request
- Enter an event name (e.g., motion_detected) → Create trigger
- Click Then That and choose your desired action (Philips Hue, Smart Life, Email, SMS)
- Configure the action (e.g., turn on living room light)
- Click Continue → Finish
- Go to Webhooks service page → Documentation to get your API key
Circuit Diagram
Connection Table
| Component |
Pin |
ESP32 Pin |
PIR Sensor |
VCC
3.3V
PIR Sensor |
GND
GND
PIR Sensor |
OUT
GPIO 4
表
Arduino Code
// PIR Sensor with IFTTT Integration
#include <WiFi.h>
#include <HTTPClient.h>
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* iftttKey = "YourIFTTTKey";
const char* eventName = "motion_detected";
const int pirPin = 4;
unsigned long lastTriggerTime = 0;
const unsigned long minInterval = 10000;
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) delay(500);
delay(60000);
}
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();
http.end();
}
}
void loop() {
bool motion = digitalRead(pirPin) == HIGH;
if (motion && (millis() - lastTriggerTime > minInterval)) {
lastTriggerTime = millis();
triggerIFTTT();
}
delay(100);
}
Enhanced Version with Data
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()) + "\"}";
http.POST(payload);
http.end();
}
}
Low-Power Battery Version with p
#include <esp_sleep.h>
void setup() {
esp_sleep_enable_ext0_wakeup((gpio_num_t)pirPin, 1);
}
void loop() {
triggerIFTTT();
delay(5000);
esp_deep_sleep_start();
}
Installation Steps
- Set up IFTTT account and create Webhooks trigger
- Create desired actions (lights, notifications, etc.)
- Get IFTTT API key from Webhooks documentation
- Update code with Wi-Fi credentials and IFTTT key
- Upload to ESP32 and test with hand motion
- Verify IFTTT triggers in activity log
- Place sensor in desired location
Popular IFTTT Actions for PIR Sensors
- Send SMS via Twilio
- Send push notification via IFTTT app
- Send email to yourself or family
- Turn on Philips Hue lights
- Activate TP-Link Kasa smart plug
- Control Sonos speakers
- Add row to Google Sheets
- Log to Google Drive
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.