Tilt sensor

Specifications

  • Voltage: 4.75–5.25 V.
  • Operating angle: 10°–170°.
  • Service life: 100,000 tilts.

Overview

The tilt sensor contains a metal ball that rolls inside a metal housing. When tilted, the ball rolls to one side and closes the circuit. The sensor detects whether it is tilted in a direction, but does not measure the angle.

For precise tilt-angle measurements across multiple axes, use the position sensor, which is based on an accelerometer and gyroscope.

Using the sensor

Connecting

The sensor uses a standard 3-pin connector and plugs into any IN port on the Trackduino controller.

Programming

The Tilt block is in the Sensors block group. Since the sensor works like a button, it is read with the standard digitalRead() function.

Block and generated functionDescription
Tilt sensor read block
bool digitalRead(int port)
Returns TRUE when the sensor is tilted.
int port: port connected to the sensor.

Example project

Rolling LED

Generated code:

void setup()
{
  pinMode( IN1 , INPUT);
  pinMode( OUT1 , OUTPUT );
  pinMode( OUT2 , OUTPUT );
}

void loop()
{
  if (digitalRead(IN1))  // if the sensor tilts left,
  {
    led ( OUT1 , HIGH ); // turn on the left LED
    led ( OUT2 , LOW );  // and turn off the right one
    delay( 1000 );
  }
  else // otherwise (if tilted right)
  {
    led ( OUT1 , LOW );  // turn off the left LED
    led ( OUT2 , HIGH ); // and turn on the right one
    delay( 1000 );
  }
}