Digital Tilt Sensor

This experiment demonstrates how to detect the tilt state of an object using a Raspberry Pi Pico and a digital tilt sensor. The sensor, which outputs a binary signal on tilt detection, will trigger a console message indicating whether the object is tilted. This setup can be used in various applications, such as orientation detection, alarm systems, or any setup that requires tilt detection without needing precise angular measurements.



  1. Scope:

This experiment covers the basics of digital tilt detection, including:

  • Setting up a digital tilt sensor with the Raspberry Pi Pico

  • Writing code in both Embedded C and MicroPython to read and display tilt status

  • Practical applications where simple tilt detection is sufficient.





  1. Prerequisite:

3.1. Hardware:

  • Pico microcontroller

  • Tilt sensor

  • Breadboard

  • Jumper wires

  • USB cable (to connect the computer)

3 .2. Software:

  • Thonny Software(For micro python)

  • MicroPython firmware installed on Raspberry Pi Pico

  • ubuntu Terminal(For embedded C)

  • CMake,Pico SDK,GNU Arm Embedded Toolchain,WSL.



  1. Connection Details:

Connect the D0 (digital output) pin of the tilt sensor to a GPIO pin on the Pico (e.g., GPIO 16).

Connect the VCC and GND pins of the tilt sensor to the 3.3V and GND pins on the Pico, respectively.

UART Serial Console: Use a micro-USB cable to connect the Pico to the PC, then open PuTTY and select the correct COM port and baud rate (115200).



  1. Working Principle:

The digital tilt sensor contains a switch or ball that activates when tilted past a certain threshold angle. When tilted, it sends a HIGH signal to the GPIO pin; otherwise, it remains LOW. By reading this digital signal from the tilt sensor, the program can determine whether the sensor has tilted.





  1. Application code:

  • MicroPython:

import machine

import time


tilt_pin = machine.Pin(16, machine.Pin.IN) # GPIO pin connected to D0 of the tilt sensor


while True:

tilt_state = tilt_pin.value() # Read the tilt sensor state

if tilt_state:

print("Tilt detected")

else:

print("No tilt detected")

time.sleep(0.5)


  • Embedded C:

#include <stdio.h>

#include "pico/stdlib.h"


#define TILT_PIN 16 // GPIO pin connected to D0 of the tilt sensor


int main() {

stdio_init_all();

gpio_init(TILT_PIN);

gpio_set_dir(TILT_PIN, GPIO_IN);


while (true) {

int tilt_state = gpio_get(TILT_PIN); // Read the tilt sensor state

if (tilt_state) {

printf("Tilt detected\n");

} else {

printf("No tilt detected\n");

}

sleep_ms(500);

}

}



  1. Hardware Image:



  1. Output:

MicroPython Output:

  • The console will display “Tilt detected” if the sensor detects a tilt.

  • It will display “No tilt detected” if there is no tilt.

  • The output updates every 0.5 seconds.

Embedded C Output:

  • The serial console will show “Tilt detected” when the tilt sensor is activated.

  • It will display “No tilt detected” otherwise, updating every 500 ms.



  1. Video Demonstration:





  1. Appendix:

  • Common CMakeLists:

cmake_minimum_required(VERSION 3.12)


# Pull in SDK (must be before project)

include(pico_sdk_import.cmake)


project(pico_experiments C CXX ASM)


set(CMAKE_C_STANDARD 11)

set(CMAKE_CXX_STANDARD 17)


if (PICO_SDK_VERSION_STRING VERSION_LESS "1.3.0")

    message(FATAL_ERROR "Raspberry Pi Pico SDK version 1.3.0 (or later) required. Your version is ${PICO_SDK_VERSION_STRING}")

endif()


# Initialize the SDK

pico_sdk_init()


add_compile_options(-Wall

        -Wno-format          # int != int32_t as far as the compiler is concerned because gcc has int32_t as long int

        -Wno-unused-function # we have some for the docs that arent called

        )

if (CMAKE_C_COMPILER_ID STREQUAL "GNU")

    add_compile_options(-Wno-maybe-uninitialized)

endif()


# Hardware-specific examples in subdirectories:

add_subdirectory(expt_10_LCD)

  • CMakeLists:

cmake_minimum_required(VERSION 3.13)

include(pico_sdk_import.cmake)


project(TiltDetection)

pico_sdk_init()


add_executable(TiltDetection main.c)

target_link_libraries(TiltDetection pico_stdlib)

pico_add_extra_outputs(TiltDetection)

  1. References:

Class Schedules:
# Topic Date & time Action

Curriculum

0% Completed (0/1)