Advertisement
microrobotics

Tilt Sensor Module

Mar 31st, 2023
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. A tilt sensor module detects the orientation of a device by using a small ball that moves inside a metal or plastic tube, completing or breaking an electrical circuit. Here's an example of how to use a tilt sensor module with an Arduino to detect a tilt:
  3.  
  4. To wire the tilt sensor module, connect its VCC pin to the 5V pin on the Arduino and the GND pin to the ground. Connect the sensor's signal (S) pin to the TILT_SENSOR_PIN (pin 2) on the Arduino.
  5.  
  6. This code will continuously read the tilt state from the sensor and light up the built-in LED on the Arduino when a tilt is detected. It will also print "Tilt detected!" on the Serial Monitor.
  7. */
  8.  
  9. const int TILT_SENSOR_PIN = 2; // Tilt sensor connected to Arduino pin 2
  10. const int LED_PIN = 13; // Built-in LED on Arduino pin 13
  11.  
  12. void setup() {
  13.   Serial.begin(9600);
  14.   pinMode(TILT_SENSOR_PIN, INPUT);
  15.   pinMode(LED_PIN, OUTPUT);
  16. }
  17.  
  18. void loop() {
  19.   int tiltState = digitalRead(TILT_SENSOR_PIN);
  20.  
  21.   if (tiltState == LOW) { // Tilt detected (sensor output LOW when tilted)
  22.     digitalWrite(LED_PIN, HIGH);
  23.     Serial.println("Tilt detected!");
  24.   } else {
  25.     digitalWrite(LED_PIN, LOW);
  26.   }
  27.  
  28.   delay(100); // Wait 100ms between readings
  29. }
  30.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement