.. note::
Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.
**Why Join?**
- **Expert Support**: Solve post-sale issues and technical challenges with help from our community and team.
- **Learn & Share**: Exchange tips and tutorials to enhance your skills.
- **Exclusive Previews**: Get early access to new product announcements and sneak peeks.
- **Special Discounts**: Enjoy exclusive discounts on our newest products.
- **Festive Promotions and Giveaways**: Take part in giveaways and holiday promotions.
👉 Ready to explore and create with us? Click [|link_sf_facebook|] and join today!
2.1.3 Tilt Switch
=================
Introduction
------------
This is a ball tilt-switch with a metal ball inside. It is used to
detect inclinations of a small angle.
Components
----------
.. image:: img/list_2.1.3_tilt_switch.png
Principle
---------
**Tilt**
The principle is very simple. When the switch is tilted in a certain
angle, the ball inside rolls down and touches the two contacts connected
to the pins outside, thus triggering circuits. Otherwise the ball will
stay away from the contacts, thus breaking the circuits.
.. image:: img/image167.png
Schematic Diagram
-----------------
.. image:: img/image307.png
.. image:: img/image308.png
Experimental Procedures
-----------------------
**Step 1:** Build the circuit.
.. image:: img/image169.png
:width: 800
**Step 2:** Change directory.
.. raw:: html
.. code-block::
cd ~/davinci-kit-for-raspberry-pi/c/2.1.3/
**Step 3:** Compile.
.. raw:: html
.. code-block::
gcc 2.1.3_Tilt.c -lwiringPi
**Step 4:** Run.
.. raw:: html
.. code-block::
sudo ./a.out
Place the tilt horizontally, and the green LED will turns on. If you
tilt it, \"Tilt!\" will be printed on the screen and the red LED will
lights on. Place it horizontally again, and the green LED will turns on
again.
.. note::
If it does not work after running, or there is an error prompt: \"wiringPi.h: No such file or directory\", please refer to :ref:`install_wiringpi_pi5`.
**Code**
.. code-block:: c
#include
#include
#define TiltPin 0
#define Gpin 2
#define Rpin 3
void LED(char* color)
{
pinMode(Gpin, OUTPUT);
pinMode(Rpin, OUTPUT);
if (color == "RED")
{
digitalWrite(Rpin, HIGH);
digitalWrite(Gpin, LOW);
}
else if (color == "GREEN")
{
digitalWrite(Rpin, LOW);
digitalWrite(Gpin, HIGH);
}
else
printf("LED Error");
}
int main(void)
{
if(wiringPiSetup() == -1){ //when initialize wiring failed,print message to screen
printf("setup wiringPi failed !");
return 1;
}
pinMode(TiltPin, INPUT);
LED("GREEN");
while(1){
if(0 == digitalRead(TiltPin)){
delay(10);
if(0 == digitalRead(TiltPin)){
LED("RED");
printf("Tilt!\n");
delay(100);
}
}
else if(1 == digitalRead(TiltPin)){
delay(10);
if(1 == digitalRead(TiltPin)){
LED("GREEN");
}
}
}
return 0;
}
**Code Explanation**
.. code-block:: c
void LED(char* color)
{
pinMode(Gpin, OUTPUT);
pinMode(Rpin, OUTPUT);
if (color == "RED")
{
digitalWrite(Rpin, HIGH);
digitalWrite(Gpin, LOW);
}
else if (color == "GREEN")
{
digitalWrite(Rpin, LOW);
digitalWrite(Gpin, HIGH);
}
else
printf("LED Error");
}
Define a function LED() to turn the two LEDs on or off. If the parameter
color is RED, the red LED lights up; similarly, if the parameter color
is GREEN, the green LED will turns on.
.. code-block:: c
while(1){
if(0 == digitalRead(TiltPin)){
delay(10);
if(0 == digitalRead(TiltPin)){
LED("RED");
printf("Tilt!\n");
}
}
else if(1 == digitalRead(TiltPin)){
delay(10);
if(1 == digitalRead(TiltPin)){
LED("GREEN");
}
}
}
If the read value of tilt switch is 0, it means that the tilt switch is
tilted then you write the parameter \"RED\" into function LED to get the
red LED lighten up; otherwise, the green LED will lit.