If you are familiar with electronics, you must have asked yourself why you need a tilt sensor while accelerometers are more precise and flexible. Well, the answer lies in their simplicity. Tilt sensors detect orientation, inclination, or motion simply. And the large ones can switch power by themselves. So we will look at tilt sensors and their applications, plus explain how to read the switch state using three microcontrollers. Let's get right into it!
Contents
What is a Tilt Sensor?
A tilt sensor (also known as a tilt switch, rolling ball sensor, or mercury switch) is an instrument used to measure tilting changes in a reference plane's multiple axes. In other words, it monitors the orientation and inclination in vertical structures from a preset zero state. Once it detects tilting changes, it produces an electric signal proportional to the tilting degree in multiple axes.
A tilt sensor
Tilt Sensor Pinout
A tilt switch has three pins.
Pin | Function |
VCC | Power Supply (3.0V to 5.5V) |
GND | Ground |
DOUT | Digital Output (MCU.IO) |
Factors Affecting Tilt Sensor Functionality
- Gravity
- Temperature
- Vibrations
- Zero offset
- Cross-axis sensitivity
- Calibration
A graphic image showing the KY020 tilt switch module
- Linearity
- Acceleration & deceleration
- Shock
- Line of sight from the user to the measured point
Special Offer: Get $100 off your order!
Email [email protected] to get started!
Tilt Sensor Specifications
Vital tilt switch specifications include the following.
- The number of axes: The application determines the required number of axes. For instance, dual axes are ideal for robotics. But joysticks and video game controllers need three axes sensors. Smartphones might have four axes to increase functionality.
- Sensitivity: Sensitivity refers to the switch's ability to detect tiny movements.
- Resolution: The minimum detectable inclination by the sensor.
- Noise tolerance: Noise creates harmonic distortions that cause variations in output and reduce system efficiency. Therefore, you should adhere to the manufacturer's guidelines regarding the allowable noise levels.
- Measuring range: Tilt sensors detect inclinations in a specific scope. Some can only handle measurements as low as 10° while others can sense from 0-60°.
- Vibration: Like noise, vibrations interfere with a sensor's functionality. So the sensor requires vibration resistance measures, especially in harsh environments, such as construction sites and off-road vehicles.
Reading a Tilt Sensor's Switch State
We will look at how to get sensor readings using three microcontrollers so that you use the one you are familiar with for your project. Let's start with the Raspberry Pi Pico.
Using Raspberry Pi Pico
The Raspberry Pi Pico supports Micropython and C but requires different firmware (such as UF2) when using the former.
Pin Connection
Tilt Sensor | Pico Microcontroller | Function |
VCC | 3.3V | Power Supply |
GND | GND | Ground |
DOUT | GP22 | Digital Output |
With the Raspberry Pi being the host device, open the terminal, then execute these commands to get the example codes.
C Code
If using the codes written in C language, go to the C directory, then add the SDK path using the following commands.
Modify the last SDK path command to match the correct command to export.
A mercury tilt-switch sensor module
Next, generate the makefile, then build using these commands.
Once you generate the uf2 file, press & hold the Pico button, connect the Pico and release the button. Lastly, drag & drop or copy the generated uf2 file to the recognized portable disk in the computer (RPI-RP2) using this command.
Micropython Code
If using the Micropython codes, flash the Pico with the Micropython firmware, then open Thonny IDE. Upgrade the IDE if it is an old version using this command.
After that, configure the interpreter by picking Micropython (Raspberry Pi Pico) to run your code. At the bottom, select ttyACM0 as the port. Lastly, browse for the Micropython code file labeled "Tilt Sensor.py" under the file menu, then run the code.
A graphic image showing the internal structure of a KY027 mercury tilt switch
Expected Outcome
The module's signal indicator will be on when you shake or tilt it. But when placed parallel to the reference plane, it will turn off.
Using STM32
With the STM32, we will base the examples on the STM32H743 and the STM32F103RBT6. The connection below is for the STM32F103RB, so you might have to change it, then port the code if you use other STM32 boards.
An STM32 microcontroller used in a project
The STM32 examples are HAL-library based. Download and unzip the example codes into your PC, then use the project in the location:
Tilt-Sensor-code>STM32>STM32F103RB>MDK-ARM
Open the project file "Tilt Sensor. uvprojx"
Build the file, then program it into the STM32 board
Connect the board's UART1 to your computer, then check the incoming serial data via SSCOM software.
Using Arduino
This Arduino project is for an Arduino UNO. You will have to change the connections if using other Arduino microcontrollers.
The Arduino UNO microcontroller
Install Arduino IDE on your computer, then download & unzip the demo code. Next go to Tilt-Sensor-code>Arduino>Tilt_Sensor, then execute the "Tilt_Sensor.in" file.
Select the board and port correctly, then build the file and program it into the UNO. Use SSCOM or Arduino IDE's serial monitor to read the incoming serial data.
Tilt Sensor Applications
- Monitoring the performance of bridges under load
- Auto-rotate functions in phones
- Indicating rolls in airplanes, boats, and vehicles
- Monitoring piers, retaining walls, piles, and structures when tunneling or excavating
- Measuring angular distortions in tunnels and along railway tracks
- Detecting the position of in-game controllers and hand-held gaming systems
Tilt Sensor Benefits
- High accuracy and resolution
- Compact
- Easy to use
- Consumes little power
- Readable by most industry-standard data loggers
A vibration ball tilt sensor
Wrap Up
In conclusion, tilt sensors are critical components in several applications, especially in the construction industry. Compared to accelerometers, they have a simple operating mechanism. And you can read their switch state in different microcontrollers using the steps described above. Contact us if you have any questions.
Special Offer: Get $100 off your order!
Email [email protected] to get started!