Sensor

๐ŸŽฒ Tilt Switch (Ball Switch)

๐Ÿ“Œ Description

A tilt switch is a small sensor that detects whether it is tilted or upright. Inside, it has a tiny metal ball that rolls and makes or breaks contact depending on the angle.

๐ŸŽฏ Use

In our projects, tilt switches are used to:

  • Detect orientation (upright vs tilted).
  • Act as a safety sensor (turn off if the robot falls).
  • Trigger actions when the robot changes position.

๐Ÿ‘‰ Think of the tilt switch as the robotโ€™s balance sensor โ€” it knows if the robot is standing straight or leaning.

Module parameters

Pin name Description
G GND (Power Input Negative)
V VCC (Power Input Cathode)
S Digital signal pins
  • Supply voltage: 3.3V/5V
  • Connection: PH2.0 terminal wire
  • Installation method: double screw fixing

๐Ÿ–ฅ๏ธ Code Explanation

import machine
import time

pin25 = machine.Pin(25, machine.Pin.IN)    # Tilt switch input
pin26 = machine.Pin(26, machine.Pin.OUT)   # LED output

while True:
    if pin25.value() == 0:                 # If tilted
        pin26.value(1)                     # Turn LED ON
    else:                                  # If upright
        pin26.value(0)                     # Turn LED OFF
        time.sleep_ms(500)                 # Small delay

Step by Step

  1. import machine, time โ†’ Tools to control pins and add pauses.
  2. pin25 = machine.Pin(25, machine.Pin.IN) โ†’ Pin 25 reads the tilt switch (input).
  3. pin26 = machine.Pin(26, machine.Pin.OUT) โ†’ Pin 26 controls the LED (output).
  4. while True: โ†’ Loop that repeats forever.
  5. if pin25.value() == 0: โ†’ Checks if the tilt switch is tilted.
  6. pin26.value(1) โ†’ Turns the LED on when tilted.
  7. else: โ†’ If not tiltedโ€ฆ
  8. pin26.value(0) โ†’ Turns the LED off.
  9. time.sleep_ms(500) โ†’ Adds a short delay for stability.

๐Ÿ‘‰ Together, this program makes the LED light up when the sensor is tilted.


โœ… Conclusion of the Test

  • If tilting the sensor turns the LED on, and keeping it upright turns the LED off, the program is working correctly.
  • This test shows how the robot can sense orientation and react with an output (LED).
  • Later, instead of just controlling a light, the tilt switch could be used to stop motors, trigger alarms, or reset the robot if it falls.
On this page