1. Overview
Tilt sensor is also known as one-way ball switch, which boasts a ball inside. In the module, one end is connected and the other is not, which is used to monitor the tilt. The module outputs different level signals depending on whether the sensor is tilted. In application, it can be used for tilt detection and alarm making.
2. Parameters
3. Principle
Tilt the module to make the ball roll to the pin end to conduct two pins, so the module output low; Tilt the module to make the ball roll to the other end, so two pins are not conducted and the module outputs a high level. As shown below:
Schematic diagram of working principle
Tilt module schematic diagram
4. Wiring Diagram
The tilt sensor is a digital module. It needs to be connected to a blue socket of the main board.
Here we connect the module to socket 2,From the port view of the kidsIOT board, socket 2 is digital io port io2.
5. Test Code
/*
Project name: 7_tilt
Function: Read button state
Author: keyestudio
Hardware connection:
- Connect to port 2
Library:
- none (no need to import additional library)
Cautions:
- Connect to port 2
- Before uploading test code, please correctly connect to the development board and port
- Set the baud rate to 9600 before using serial monitor printing
*/
#define tilt_Pin 2 //set pin to IO2
void setup() {
//Set the baud rate
Serial.begin(9600);
//set pin to input
pinMode(tilt_Pin, INPUT);
}
void loop() {
//Read TTL level of pin IO2 (high level '1', low level '0')
int tilt_state = digitalRead(tilt_Pin);
//determine whether it tilt.
if (tilt_state == 1) {
//if yes, print "It's tilted!"
Serial.println("It's tilted!");
} else {
//if not, print "No tilt!"
Serial.println("No tilt!");
}
//Delay 300 to limit the printing speed
delay(300);
}
6. Test Result
After uploading the test code, open the serial monitor and set the baud rate to 9600, and the serial monitor prints results. When the tilt sensor detect no tilt, the on-module LED lights on and the serial monitor shows “No tilt!”. If the LED goes off, the serial monitor displays “It’s tilted!”.