# Digistump Wiki

### Site Tools

oak:tutorials:rollingball

# Oak: Tilt Switching

This lesson will show you how to connect and use a Tilt switch to trigger the onboard LED. It will also demonstrate how to publish the fact that the switch has been tilted.

## Components Used:

Part Quantity Identification
SW-520D Rolling Ball Switch 1
10k Resistor 1 Brown, Black, Orange, Gold
M to M 30cm Jumper Wire 3 Red, Black, Blue

## Concepts:

Tilt Switch: In this lesson we will be using a tilt switch to detect if the switch has been turned upside down. This will be achieved by reading the pin the tilt switch is connected to, much like a button.

Pull-Down Resistor: In this lesson we will use a Pull-Down resistor to make sure we can reliably detect the tilt switch tilting. If an I/O pin is 'left floating' reading it's value can result in either HIGH or LOW. By attaching a resistor between the I/O pin of the Oak and GND we can ensure that if the tilt switch is tilted, the state is always LOW.

## Circuit:

You will want to insert the 10k resistor first. Make sure that the Red and Black wires go to the separate pins of the tilt switch and the blue goes to the same pin as the resistor, but on the tilt switch side of the resistor as in the diagram.

We will be using the onboard LED to display the state of the tilt switch.

## Code:

```// define which pins to use
const int tilt = 2;     // the number of the tilt switch pin
const int ledPin = 1;      // the number of the LED pin

// set a variable to record the switch's state
int tiltState = 0;         // variable for reading the tilt switch status

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the tilt switch pin as an input:
pinMode(tilt, INPUT);
}

void loop() {
// read the state of the tilt switch:

// check if the tilt switch is tilted.
// if it is, the tiltState is HIGH:
if (tiltState == LOW) {
// turn LED on:
digitalWrite(ledPin, HIGH);
}
else {
// turn LED off:
digitalWrite(ledPin, LOW);
}
}```

In this example code we create a variable called tiltState that allows us to store the current status of the tilt switch. In the loop() we constantly check its state and then respond based on this. This is very similar to what you'd do with a Oak: Breadboard Button Lesson.

How about we kick it up a notch?

##### Letterbox Detector

This example code, along with the same circuit from above will notify us every time the ball is tilted and returned. This is perfect for detecting a letterbox being opened and closed by the postie.

```// define which pins to use
const int tilt = 2;     // the number of the tilt switch pin
const int ledPin = 1;      // the number of the LED pin

// set a variable to record the switch's state
int tiltState = 0;         // variable for reading the tilt switch status
int tilted;

void setup() {
// initialize the LED pin as an output:
pinMode(ledPin, OUTPUT);
// initialize the tilt switch pin as an input:
pinMode(tilt, INPUT_PULLUP);
}

void loop() {
// read the state of the tilt switch:

// check if the tilt switch is tilted.
// if it is, the tiltState is HIGH:
if (tiltState == HIGH) {
// set tilted to 1
tilted = 1;
}
else {
// turn LED off:
if (tilted = 1) {
// Tell the Particle Cloud we've done a full tilt
Particle.publish("Tilted","Totally");
// Turn the LED on and off to let us know we're publishing this
digitalWrite(ledPin, HIGH);
delay(1000);
digitalWrite(ledPin, LOW);
// Set tilted state back to 0
tilted = 0;
}
}
// Let's wait a second to give the postie time to close it before we notify the cloud
delay(1000);
}```

This code uses a second variable tilted in tandem with tiltState to determine when we've completed a full on-off of the tilt. Note that tilt sensors are very sensitive so this may trigger unexpectedly at times.

Once you've got this code running on your Oak, check out https://dashboard.particle.io/user/logs and tilt your oak over and back. You should get something like this:

## Conclusion:

Tilt switches are a very simple analog way of checking if something has tilted. They can be used to detect basic movement but are very sensitive and don't like vibrations. Always make sure to debounce them with pullup resistors and if you find them too sensitive, debouncing code. The arduino debouncing tutorial is a good primer on this.