Tilt Sensor (tilt Switch) Module-Arduino Tutorial

by Lisleapex Blog in Circuits > Arduino

395 Views, 1 Favorites, 0 Comments

Tilt Sensor (tilt Switch) Module-Arduino Tutorial

1.png

This post shows how to use the tilt sensor module with Arduino. Tilt sensors are many times referred to as inclinometers, tilt switches or rolling ball sensors. Using a tilt sensor is an easy way to detect orientation or tilt.

Supplies

  • Arduino UNO – Read the best Arduino starter kits
  •  1x breadboard
  •  1x tilt sensor
  •  1 LED
  •  1x 220 ohm resistor
  •  Jumper

How Does It Work

2.png

Here's how it works:


When the sensor is fully upright, the ball drops to the bottom of the sensor and connects the poles, allowing current to flow.

When the sensor is tilted, the ball does not touch the magnetic pole, the circuit is broken, and current does not flow.

In this way, the tilt sensor acts like a switch, turning it on or off depending on its tilt. Therefore, it will provide digital information to the Arduino, whether it is a high signal or a low signal.

Pin Wiring

3.png

Connecting the tilt sensor to the Arduino is very simple. You just need to connect one pin to the Arduino digital pin and GND to GND. In this case, you just add an LED to the schematic in the "Pin Wiring" section.

Programming

To complete this example, upload the following code to the Arduino board.

  1. int ledPin = 12;
  2. int sensorPin = 4;
  3. int sensorValue;
  4. int lastTiltState = HIGH; // the previous reading from the tilt sensor
  5. // the following variables are long's because the time, measured in miliseconds,
  6. // will quickly become a bigger number than can be stored in an int.
  7. long lastDebounceTime = 0; // the last time the output pin was toggled
  8. long debounceDelay = 50; // the debounce time; increase if the output flickers
  9. void setup(){
  10. pinMode(sensorPin, INPUT);
  11. digitalWrite(sensorPin, HIGH);
  12. pinMode(ledPin, OUTPUT);
  13. Serial.begin(9600);
  14. }
  15. void loop(){
  16. sensorValue = digitalRead(sensorPin);
  17. // If the switch changed, due to noise or pressing:
  18. if (sensorValue == lastTiltState) {
  19. // reset the debouncing timer
  20. lastDebounceTime = millis();
  21. }
  22. if ((millis() - lastDebounceTime) > debounceDelay) {
  23. // whatever the reading is at, it's been there for longer
  24. // than the debounce delay, so take it as the actual current state:
  25. lastTiltState = sensorValue;
  26. }
  27. digitalWrite(ledPin, lastTiltState);

  28. Serial.println(sensorValue);
  29. delay(500);
  30. }


Finally

4.png

In the end, this is what you'll have.