28 – Tilt switch



    • Introduce the tilt sensors.
    • Assemble a prorotype.
    • This is a resting chapter, for us to relax.
    • Show a debouncing without delay.


Bill of materials

Arduino UNO  Arduino Uno or equivalent.
Breadboard A solderless Breadboard.
Jumper wires  Some jumper wires.
Red LED diode  A LED diode.
Tilt sensor  A tilt switch.
330 Ohm resistor
A 330 Ohm resistor.


Tilt Sensors


Those who a few years ago played with Pinball machines know very well that when you were carried away by emotion and thrusted the machine, the tilt signal lighted up quickly and prevented you continuing playing.

To tilt refers to modify the inclination of the reference plane on which our system of coordinates is settled (inclination that if were not prevented, wouldn’t let the ball go down to the Pinball out hole, since it would suffice to lift the legs that stand closer to the player).

As far as I can remember in those old machines it was made by using a pendulum, but that could have been done by using one of the sensors that concern us in this chapter: the tilt sensors.

Tilt sensor

Tilt sensors are cylindrical sensors of a few millimeters in length, which carry within one or two small conductive balls able to close the circuit making contact the lower metal pins of the cylinder.

When the pins make contact allow the passage of current intensity and close contact exactly as if they were a switch (and indeed they are handled in the same way) but from a certain angle of inclination they no longer make contact and open the circuit.


It is not a new technology, as it has been in use for a long time, but previously it was done by using a droplet of liquid mercury, which is conductive, and when it scrolled through the cylinder closed the circuit between a pair of contacts in the bottom.

But since mercury is highly toxic for the environment and the people who handle it, for quite some time it has been replaced by the kind that we are going to use, although nowadays they are still called mercury switches.

Clearly they behave like simple switches and, in fact, we will have to do the debouncing likewise, because if the contacts bounced using switches you can imagine how much could bounce these little balls.

Let’s see the circuit we are going to assemble.




Let’s use the same wiring diagram that we use with a normal switch. We therefore need to use a pull-up resistor, and depending on the read value we will turn on or turn off a LED diode connected to pin 13.

  • For once, but without this becoming a regular occurrence, we will connect the LED to pin 13 without a resistor, because this pin carries one in series already. In any other pin we should put a resistor to limit the current intensity or we will see the LED diode burning out.
  • We will use negative logic. This is a complicated way of saying that since under normal conditions, the switch is closed, the trigger signal will be activated when we read a LOW value in pin 2.

This is the circuit schematic diagram:

Chapter 28, schematic diagram

And here we have the circuit wiring diagram:

Chapter 28, wiring diagram




The program will be very similar to that used in the previous chapter about conditional statements and buttons. But to vary it a bit, we will use debouncing with variable time (not to get bored).

This is the sketch:

Sketch 28.1
int inPin = 2;
int outPin = 13;
int reading;
int previous = LOW;

long time = 0;            // To save the time when the value inverts 
long debounce = 50;       // Bouncing time

void setup()
   {  pinMode(inPin, INPUT_PULLUP);
      pinMode(outPin, OUTPUT);

void loop()
   {  reading = digitalRead(inPin);
      if (reading!= previous)     // If there is some change in the reading
          time = millis();
      if ((millis() - time) > debounce)
           digitalWrite(outPin, reading);

      previous = reading;

There is nothing new in the program. In the “reading” variable we store the value of the pin 2 until it is different from the last read value. From this we see whether the debouncing time has elapsed to filter the rebounds and write in the output pin what we have read.

In practice they are fairly easy to handle and allow us to detect a sudden movement of the sensor and also to detect an inclination higher than a certain angle.

It is not difficult to imagine in a circuit that detects whether a garage door is raised or not, for example, or an shock sensor. For some reason that I can`t imagine these sensors appear to be very popular within the Arduino world.



    • We have seen that these little tilt switches are simple and reasonably effective.
    • They behave exactly like a contact switch but with more bounce.
    • We have seen that they can be used as a sensor of sudden movement or inclination.





No Comments

Give a Reply