fbpixel
Tags:
0
(0)

A tilt switch is used to detect the orientation or inclination of a system. It is often used to indicate when a system (such as an agricultural vehicle) exceeds its operating tilt range, or to detect the orientation of a screen and thus modify its layout. It doesn’t give as much information as an accelerometer, but it’s more robust and doesn’t require a special program to process.

Hardware

  • Computer
  • Arduino UNO
  • USB A Male cable
  • Tilt Switch

Operating principle

The tilt switch consists of a ball (or mass of mercury) and a contactor. When its orientation with respect to the horizon changes, the ball, subjected to gravity, moves and comes into contact with two poles. This short-circuits the poles and closes the switch.

Diagram

The tilt switch is connected, like a pushbutton, preferably to a digital pin on the microcontroller, as it returns a high or low, closed or open state.

  • By connecting ground and a digital pin (here pin 2)
  • Using an external pull-up resistor (if the microcontroller or pin used has no internal pull-up)

Code

The tilt switch works like a switch. The sensor’s control code is therefore very similar to that of a pushbutton. It’s worth noting that, generally speaking, a ball makes contact and closes the switch. Occasionally, this ball will bounce when the switch changes state. To avoid this, we use anti-bounce logic.

//Parameters
const int tiltPin = 2;

//Variables
bool tiltStatus = false;
bool oldTiltStatus = false;
unsigned long lastDebounceTime = 0;
unsigned long debounceDelay = 50;

void setup() {
  //Init Serial USB
  Serial.begin(9600);
  Serial.println(F("Initialize System"));
  //Init digital input
  pinMode(tiltPin, INPUT);
}

void loop() {
  debounceTilt();
}

void debounceTilt( ) { /* function debounceTilt */
  ////debounce TiltSwitch
  int reading = digitalRead(tiltPin);
  if (reading != oldTiltStatus) {
    lastDebounceTime = millis();
  }
  if ((millis() - lastDebounceTime) > debounceDelay) {
    if (reading != tiltStatus) {
      tiltStatus = reading;
      Serial.print(F("Sensor state : ")); Serial.println(tiltStatus);
    }
  }
  oldTiltStatus = reading;
}

Results

By tilting the ball sensor, you should see its status change on the serial monitor. After several tests, you should be able to adjust the debounceDelay parameter to ensure that the status read is reliable.

Applications

  • Detect the orientation of an object

Sources

How useful was this post?

Click on a star to rate it!

Average rating 0 / 5. Vote count: 0

No votes so far! Be the first to rate this post.

As you found this post useful...

Follow us on social media!

We are sorry that this post was not useful for you!

Let us improve this post!

Tell us how we can improve this post?