Introduction to the Inclination Sensor Module
The tilt sensor module is shown in the figure below.
Inclination sensors can detect direction or inclination. It detects if the sensor is fully upright or tilted.
This makes it very useful, for example in toys, robots and other utensils where the working method depends on the tendency.
How it works
The tilt sensor is cylindrical and contains a freely conductive rolling ball inside with two conductive elements (rods) underneath.
It works as follows:
- When the sensor is fully upright, the ball falls to the bottom of the sensor and connects the poles, allowing current to flow.
- When the sensor is tilted, the ball does not touch the poles, the circuit is open, and the current does not flow.
In this way, the tilt sensor is like a switch that turns on or off depending on its inclination. Thus, it will provide digital information to the Arduino, that is, a high or low signal.
Pin wiring
Connecting the tilt sensor to the Arduino is very simple. Simply connect one pin to the Arduino digital pin and connect GND to GND.
Note: A pull-up resistor of 10kOhm is required on the pin.
example
In this example, if the sensor is upright, the LED turns off; If the sensor is tilted, the LED turns on.
。
int ledPin = 12;
int sensorPin = 4;
int sensorValue;
int lastTiltState = HIGH; // sh上传从倾斜传感器读取的数据
long lastDebounceTime = 0; // 最后一次切换输出引脚的时间
long debounceDelay = 50; //延时
void setup(){
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, HIGH);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop(){
sensorValue = digitalRead(sensorPin);
// 如果开关因噪音或按压而改变
if (sensorValue == lastTiltState) {
// 时间复位
lastDebounceTime = millis();
}
if ((millis() - lastDebounceTime) > debounceDelay) {
lastTiltState = sensorValue;
}
digitalWrite(ledPin, lastTiltState);
Serial.println(sensorValue);
delay(500);
}