How does work Tilt sensor – Tilt sensor with Arduino nano [Code and circuit diagram]
Hello guys, welcome back to my SriTu Hobby blog. Today we will learn about Tilt sensors. A tilt sensor can be identified as a device that detects the tilting of a flat plane along different axes. These sensors can easily detect orientation and inclination. Also, these sensors are called Roll ball sensors and Mercury sensors.
Ok, let’s see now how does work these sensors. The cylindrical part of these sensors contains a mercury or metal ball that is designed to move in both directions. Also, there are two conductive elements at one end of this cylindrical section. When the metal ball moves in that direction, the two ends become shorter. The current then flows through these two terminals. For this reason, it acts as a switch. We can also get a signal by connecting this to a circuit.
The main advantages of using these sensors
- Low cost.
- Low power consumption.
- Simple to use.
Okay, now let’s see where these sensors should be used.
- Identify the location of game system controllers.
- For mobile phones and tablets.
- To prevent the capsizing of boats, planes, and vehicles.
- To measure the height of trees and buildings.
- To warn when carrying material that is not suitable for tilt.
Let’s test how a tilt switch works using this. For this, we need the following accessories. These can be easily purchased from the links below.
- Arduino nano board x 1 — Our Store / Amazon
- Tilt sensor x 1 — Our Store / Amazon
- LED bulb x 1 — Our Store / Amazon
- 180 resistor x 1 — Our Store / Amazon
- Breadboard x 1 — Our Store / Amazon
- Jumper wires — Our Store / Amazon
Disclosure: These Amazon links are Affiliate links. As an Amazon Associate, I earn from qualifying purchases.
Step 1
Firstly, identify these components.
Step 2
OK, now connect these components using the circuit diagram below.
Step 3
Now let’s look at the code below.
- The complete program of this project – Download
/*Angle[Tilt] sensor with Arduino.
created by the SriTu Tech team.
Read the code below and use it for any of your creations.
*/
#define sensor 2
#define LED 3
void setup() {
Serial.begin(9600);
pinMode(sensor , INPUT);
pinMode(LED , OUTPUT);
}
void loop() {
bool value = digitalRead(sensor);
Serial.println(value);
if (value == 0) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
}
First, the sensor connector pin and the LED bulb pin are defined.
#define sensor 2
#define LED 3
This code sets the sensor PIN as an input pin and the LED PIN as an output pin.
void setup() {
Serial.begin(9600);
pinMode(sensor , INPUT);
pinMode(LED , OUTPUT);
}
The following code reads the sensor value and inserts it into the “value” variable. Then, it is tested using the if condition, the LED bulb turns on if the value variable is equal to 0, and the LED bulb turns off if the value variable is equal to 1.
void loop() {
bool value = digitalRead(sensor);
Serial.println(value);
if (value == 0) {
digitalWrite(LED, HIGH);
} else {
digitalWrite(LED, LOW);
}
Step 4
Finally, select the correct board and port. After, click the upload button.
OK, enjoy this tutorial. The full video guide is below. So, we hope to see you in the next tutorial or project. Have a good day.
How does work Tilt sensor – Tilt sensor with Arduino nano [Code and circuit diagram]