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
- Create an account at ifttt.com (free).
- Click your profile → 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 (e.g., 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>
// 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
- 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 the activity log.
- 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:
- Create a new Google Sheet.
- Set up IFTTT action “Google Sheets” → “Add row to spreadsheet”.
- Configure columns: Timestamp, Sensor ID, Event Type.
- 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.