ESP8266 Tilt Sensor Project
The ESP8266 Tilt Sensor project demonstrates how to interface a simple tilt sensor (often a metal‑ball‑type switch or basic inclinometer module) with an ESP8266‑based board such as NodeMCU or Wemos‑D1. The sensor changes its state when the device is tilted or inclined, and the ESP8266 reads this change to trigger LEDs, buzzers, or Wi‑Fi alerts. This makes it ideal for motion‑based alarm systems, vibration‑like detections, or orientation‑aware toys and devices.
How the Tilt Sensor Works
A basic tilt‑sensor module often contains a small conductive ball inside a sealed tube with two or more contacts. When the sensor is upright, the ball touches the contacts, closing the circuit and making the output LOW or HIGH depending on wiring. When the device tilts, the ball rolls away, opening the circuit and reversing the output. The ESP8266 reads this digital state, so any tilt beyond a threshold angle is treated as an event.
Some fancier modules send an analog value proportional to the angle, but many common ESP8266‑style setups use a digital‑output tilt switch connected to a GPIO pin.
Components Needed
- ESP8266 development board (e.g. NodeMCU‑v2 or Wemos‑D1)
- Tilt sensor module (digital ball‑type switch or inclinometer)
- LED (optional, for visual feedback)
- 220 Ω resistor (for LED)
- Passive buzzer (optional, for alarm)
- Jumper Wires
- Breadboard
Circuit Diagram
Circuit Setup
1. Connect Tilt Sensor to ESP8266
Many tilt‑sensor modules present 3 pins:
- VCC → 3.3 V (or 5 V, depending on module).
- GND → GND on ESP8266.
- OUT (digital signal) → GPIO 2 (D4 on NodeMCU) on ESP8266.
If the sensor is just a 2‑pin ball‑switch, you can power it as a pull‑up input: connect one pin to a GPIO with an internal or external 10 kΩ pull‑up to 3.3 V, and the other pin to GND. The GPIO will read HIGH when the sensor is stable and LOW when tilted (or vice versa, depending on orientation).
2. Optional Indicator Circuits
- LED: Connect an LED anode to GPIO 5 (D1 on NodeMCU) through a 220 Ω resistor; connect the cathode to GND for visual indication of tilt events.
- Buzzer: Connect the buzzer positive to GPIO 4 (D2 on NodeMCU); connect the negative to GND for an audible alarm when the device is tilted.
Instructions
1. Software Setup
Configure the Arduino IDE for ESP8266 (e.g. board: NodeMCU‑v2). Initialize serial communication (9600 or 115200 baud) for debugging. Define the tilt‑sensor pin and indicator pins, then set the tilt‑pin as `INPUT` and the LED/buzzer pins as `OUTPUT` in `setup()`.
2. Tilt Detection in loop()
In the `loop()` function, read the tilt‑sensor pin with `digitalRead(tiltPin)`. Treat a HIGH or LOW state (depending on your wiring) as a tilt event. When tilt is detected, light the LED, sound the buzzer, and optionally send a Wi‑Fi alert over HTTP or MQTT.
3. De‑bouncing and Filtering
Tilt sensors can produce noisy or bouncy transitions when the device wobbles slightly. Add a simple software debounce by reading the pin multiple times or checking the state over a short time window (e.g. 50–100 ms) to avoid repeated false triggers.
4. Power and Mounting
Use a stable 3.3–5 V power supply. Mount the sensor securely on the device so that normal upright positioning produces a consistent baseline state, while intentional tilting or motion reliably triggers the sensor. Avoid placing the sensor on surfaces that vibrate constantly.
C++ Example Code (ESP8266 Tilt Sensor Alarm)
#include <ESP8266WiFi.h>
// Replace with your Wi‑Fi credentials (if using Wi‑Fi alerts)
const char* ssid = "YOUR_WIFI_SSID";
const char* password = "YOUR_WIFI_PASSWORD";
// Tilt sensor and indicator pins
const int tiltPin = 2; // D4 on NodeMCU
const int ledPin = 5; // D1 on NodeMCU
const int buzzerPin = 4; // D2 on NodeMCU
// Debounce variables
int tiltState = 0;
int lastTiltState = 0;
unsigned long lastDebounceTime = 0;
const unsigned long debounceDelay = 50;
void setup() {
Serial.begin(9600);
// Optional: connect to Wi‑Fi
// WiFi.begin(ssid, password);
// while (WiFi.status() != WL_CONNECTED) delay(1000);
pinMode(tiltPin, INPUT);
pinMode(ledPin, OUTPUT);
pinMode(buzzerPin, OUTPUT);
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
Serial.println("=== ESP8266 TILT SENSOR READY ===");
}
void loop() {
int reading = digitalRead(tiltPin);
// Simple debouncing
if (reading != lastTiltState) {
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
if (reading != tiltState) {
tiltState = reading;
if (tiltState == HIGH) {
// Tilt detected
digitalWrite(ledPin, HIGH);
digitalWrite(buzzerPin, HIGH);
Serial.println("TILT DETECTED!");
// Optional: send Wi‑Fi alert or MQTT message here
} else {
// Device back to stable position
digitalWrite(ledPin, LOW);
digitalWrite(buzzerPin, LOW);
}
}
}
lastTiltState = reading;
delay(10);
}
Applications
Motion‑Based Alarm Systems: Use the sensor in security enclosures, toolboxes, or camera housings to detect unauthorized movement and trigger an alert via Wi‑Fi, SMS, or email.
Toy and Game Devices: Add tilt‑sensitivity to handheld toys or game controllers so that motions or tilts control game events or LED effects.
Orientation Monitoring: Mount the sensor on vehicles, robots, or industrial equipment to detect abnormal tilt or imbalance and log or alert when the angle exceeds safe limits.
Notes
Digital Output vs Analog
Most cheap tilt‑sensor modules provide a simple digital output (HIGH/LOW) when the tilt angle crosses a threshold, not a continuous angle value. For fine‑grained tilt‑angle measurement, an accelerometer or MPU‑6050‑style sensor is used instead of this basic switch.
Serial and Debugging
Use the Serial Monitor to watch when "TILT DETECTED!" appears and to verify that the LED and buzzer respond correctly. This helps you tune the debounce delay and check how sensitive your sensor is to different tilt angles.
Remote Alert Extension
You can extend this project by connecting the ESP8266 to Wi‑Fi and sending tilt events to a cloud service (ThingSpeak, MQTT broker, or IFTTT) so that you receive notifications on your phone or dashboard whenever the installed device is moved or tilted.