Tilt Sensor with Arduino Easy Tutorial

Tilt Sensor with Arduino Easy Tutorial

 

Tilt sensors are inexpensive and easy to use. They are also sometimes called tilt or ball switches.  They can detect if an object is upright or tilted at a certain angle. To connect it to an Arduino board, you just need to add one 10Kohm resistor. The tilt sensor has no fixed positive or negative side. You simply choose which one will be your positive and which one will be your negative side. Here are three Arduino projects to get you started with the tilt sensor.

 

 

 

PROJECT 1: Tilt Sensor Value

 

This project prints a 1 or 0 on the Serial Monitor to check if the tilt sensor is working.

 

Materials:

1 Arduino Microcontroller

1 Tilt Switch

1 10Kohm resistor

 

Breadboard Diagram

 

 

Arduino Sketch:


  //Read the value of a tilt sensor

  int tiltsensor = 2;
  
  void setup()
  {
    pinMode(tiltsensor, INPUT);
    //initialize communication at 9600 bits per second
    Serial.begin(9600);
  }

  void loop()
  {
    //print the value on the serial monitor
    //Go to Tools->Serial Monitor to see the values
    Serial.println(digitalRead(tiltsensor));
    delay(100);
  }

 

 

PROJECT 2: Tilt Sensor with 1 LED

 

This project turns on an LED if the sensor is tilted and turns it off when it is upright.

 

Materials:

1 Arduino Microcontroller

1 Tilt Switch

1 10Kohm resistor

1 LED

1 220-ohm resistor

 

Breadboard Diagram 

Arduino Sketch

  int tiltsensor = 2;
  int led=9;
  
  void setup()
  {
    pinMode(tiltsensor, INPUT);
    pinMode(led,OUTPUT);
  }

  void loop()
  {
    if (digitalRead(tiltsensor) == 1){
      digitalWrite(led,HIGH);
    }
    else{
      digitalWrite(led,LOW);
    }

    //add delay for stability
    delay(100);
  }

 

 

PROJECT 3: 2 Tilt Sensor 2 LEDs

 

This project performs the following behavior:

CONDITION

OUTPUT

Tilt Sensor 1

Tilt Sensor 2

LED 1

LED 2

TILTED

TILTED

ON

ON

TILTED

NOT TILTED

ON

OFF

NOT TILTED

TILTED

OFF

ON

NOT TILTED

NOT TILTED

OFF

OFF

 

This project uses the AND operator. To know more about Relational and Conditional Operators, click this link.

 

Materials

 

1 Arduino Microcontroller

2 Tilt Switches

2 10Kohm resistors

2 LED

2 220-ohm resistor

 

Breadboard

 

Arduino Sketch

  int tiltsensor_1 = 2;
  int tiltsensor_2 = 4;
  int led_1=9;
  int led_2=10;
  
  void setup()
  {
    pinMode(tiltsensor_1, INPUT);
    pinMode(tiltsensor_2, INPUT);
    pinMode(led_1, OUTPUT);
    pinMode(led_2, OUTPUT);
  }

  void loop()
  {
    if (digitalRead(tiltsensor_1)==1 && digitalRead(tiltsensor_2)== 1){
      digitalWrite(led_1,HIGH);
      digitalWrite(led_2,HIGH);
    }

    else if (digitalRead(tiltsensor_1)==1 && digitalRead(tiltsensor_2)== 0){
      digitalWrite(led_1,HIGH);
      digitalWrite(led_2,LOW);
    }

    else if (digitalRead(tiltsensor_1)==0 && digitalRead(tiltsensor_2)== 1){
      digitalWrite(led_1,LOW);
      digitalWrite(led_2,HIGH);
    }

    else if (digitalRead(tiltsensor_1)==0 && digitalRead(tiltsensor_2)== 0){
      digitalWrite(led_1,LOW);
      digitalWrite(led_2,LOW);
    }

    //add delay for stability
    delay(100);
    
  }

 

No comments

Leave your comment

In reply to Some User

Arduino Intro

Arduino Intro is dedicated to providing high-quality, beginner-friendly resources for learning Arduino and electronics. Our mission is to make technology accessible to everyone through comprehensive tutorials, hands-on projects, and a supportive community.