Angle Sensor Ball Switch Tilt

🎮 Display the Raw Digital State
Goal
Read the tilt sensor and display its raw 0 or 1 value on the PC.
Concepts
Digital Input · HIGH/LOW · Loop · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
pin26 = machine.Pin(26, machine.Pin.IN) # D2 reads the tilt sensor
while True: # Repeats forever
state = pin26.value() # Reads the sensor state
print(state) # Sends 0 or 1 to the PC
time.sleep(0.2) # Waits before reading again 🎮 Level or Tilted
Goal
Interpret the digital value and display Level or Tilted on the PC.
Concepts
Digital Input · If/Else · Loop · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
pin26 = machine.Pin(26, machine.Pin.IN) # D2 reads the tilt sensor
while True: # Repeats forever
state = pin26.value() # Reads the sensor state
if state == 1: # Checks if the sensor is level
print('Level') # Reports the level position
else: # Runs when the sensor is tilted
print('Tilted') # Reports the tilted position
time.sleep(0.2) # Waits before reading again 🎮 Report Only State Changes
Goal
Report the sensor state only when it changes between Level and Tilted.
Concepts
Digital Input · Variables · Comparison · State Change · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
pin26 = machine.Pin(26, machine.Pin.IN) # D2 reads the tilt sensor
last_state = pin26.value() # Saves the starting state
while True: # Repeats forever
state = pin26.value() # Reads the current state
if state != last_state: # Checks if the state changed
if state == 1: # Checks if it changed to level
print('Level') # Reports the new state
else: # Runs if it changed to tilted
print('Tilted') # Reports the new state
last_state = state # Saves the new state
time.sleep(0.05) # Waits before reading again 🎮 Measure Tilt Duration
Goal
Measure how long each tilted period lasts and display the duration on the PC.
Concepts
Digital Input · Timing · Variables · State Change · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
pin26 = machine.Pin(26, machine.Pin.IN) # D2 reads the tilt sensor
while True: # Repeats forever
if pin26.value() == 0: # Checks if the sensor is tilted
start = time.ticks_ms() # Saves the tilt start time
print('Tilt started') # Reports the new tilt
while pin26.value() == 0: # Waits while it stays tilted
time.sleep(0.01) # Adds a short pause
end = time.ticks_ms() # Saves the end time
duration = time.ticks_diff(end, start) # Calculates tilt time
print('Tilt time:', duration, 'ms') # Sends the duration to the PC
time.sleep(0.05) # Waits before checking again 🎮 Tilt Activity Monitor
Goal
Count tilt events and report both the duration of each tilt and the time between tilt events to the PC.
Concepts
Digital Input · Counter · Timing · Variables · Serial Output
import machine # Controls the board hardware
import time # Controls pauses and timing
pin26 = machine.Pin(26, machine.Pin.IN) # D2 reads the tilt sensor
count = 0 # Stores the number of tilt events
last_tilt = time.ticks_ms() # Stores the previous tilt time
while True: # Repeats forever
if pin26.value() == 0: # Checks if a tilt begins
start = time.ticks_ms() # Saves the tilt start time
between = time.ticks_diff(start, last_tilt) # Finds time between tilts
count += 1 # Adds one to the tilt counter
print('Tilt:', count) # Shows the tilt number
print('Time between:', between, 'ms') # Shows time between tilts
while pin26.value() == 0: # Waits while it stays tilted
time.sleep(0.01) # Adds a short pause
end = time.ticks_ms() # Saves the end time
duration = time.ticks_diff(end, start) # Calculates tilt duration
print('Duration:', duration, 'ms') # Shows the tilt duration
last_tilt = start # Saves this tilt time
time.sleep(0.05) # Waits before checking again 