Tilt Detect Sample

This sample demonstrates detecting tilt without using I2C bus.

To run the sample:

  1. Copy the sample code to a folder on the Raspberry Pi.

  2. Open terminal, and then go to the folder you copied the sample.

  3. Run the sample with the following command:

python3 tilt_detect.py

Sample Code

tilt_detect.py
#!/usr/bin/env python3

#This sample demonstrates detecting tilt without using I2C bus.
#Install Sensor uHAT library with "pip3 install turta-sensoruhat"

from time import sleep
from turta_sensoruhat import Turta_Accel

#Initialize
accel = Turta_Accel.AccelTiltSensor()

try:
    while True:
        #Read tilt states in one shot
        tilt_xyz = accel.read_tilt_xyz()

        #Print the readings
        print("X-Tilt..........: " + ("Tilt detected." if tilt_xyz[0] else "No tilt."))
        print("Y-Tilt..........: " + ("Tilt detected." if tilt_xyz[1] else "No tilt."))
        print("Z-Tilt..........: " + ("Tilt detected." if tilt_xyz[2] else "No tilt."))

        #Wait
        print("-----")
        sleep(0.5)

#Exit on CTRL+C
except KeyboardInterrupt:
    print('Bye.')

Result

When you run the sample, it prints 3D tilt state. The example output should be like this:

X-Tilt..........: No tilt.
Y-Tilt..........: No tilt.
Z-Tilt..........: Tilt detected.

To stop the Python script, just press CTRL + C.

Last updated