Introduction
This experiment shows how to use a tilt sensor with an Arduino to detect changes in orientation.
Components Needed
- Tilt Sensor
- Arduino (e.g., Uno, Nano)
- LED
- 220-ohm Resistor
- Jumper wires
Circuit Setup
- Connect the tilt sensor's output to a digital input pin (e.g., D2) on the Arduino.
- Connect the LED to a digital output pin using a 220-ohm resistor in series.
The LED will turn on when the sensor detects a tilt.
Code for Tilt Sensor
Upload the following code to your Arduino to detect a tilt:
const int tiltPin = 2;
const int ledPin = 13;
void setup() {
pinMode(tiltPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
int tiltState = digitalRead(tiltPin);
if (tiltState == HIGH) {
digitalWrite(ledPin, HIGH); // Turn on LED when tilted
Serial.println("Tilt Detected!");
} else {
digitalWrite(ledPin, LOW); // Turn off LED when upright
Serial.println("No Tilt");
}
delay(500);
}
Explanation
The tilt sensor detects when the orientation changes and sends a signal to the Arduino, which turns on the LED and prints a message on the Serial Monitor.
Troubleshooting
- If the sensor doesn't detect tilt, check the wiring and ensure the sensor is positioned correctly.
- If the LED doesn't respond, check the tilt sensor's connections and pin settings in the code.