X
Home > Blog > STEM for ESP32 > HOW to Detect Tilt with ESP32

HOW to Detect Tilt with ESP32

By r December 24th, 2024 161 views

1. Overview

KD2090

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

Voltage: DC 3.3 ~ 5V
Current: 4.2 mA
Maximum power: 0.03 W
Operating temperature: -10°C ~ +50°C
Dimensions: 32 x 24 x 18 mm (without housing)
Positioning hole: Diameter of 4.8 mm
Interface: Telephone socket

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:

a18

Schematic diagram of working principle

a19

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.

IOT-blue

Here we connect the module to socket 2,From the port view of the kidsIOT board, socket 2 is digital io port io2.

b4

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!”.

a20

HOW to Use Button Input Data with ESP32
Previous
HOW to Use Button Input Data with ESP32
Read More
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Next
Comprehension! HOW to Monitor Smart Home Environment with ESP32
Read More
Message Us