Angle Sensor Ball Switch Tilt

🎮 The Earthquake Detector
Module Goal: feel orientation. Inside this sensor a metal ball rolls when you tilt it your robot can know if it's standing straight or falling over. Every balancing robot starts here.
Goal:
discover what the rolling ball does to the signal.
Concepts:
Digital Input · Mechanical Sensing · Prediction
Before running: hold the sensor flat and still. Prediction: what number do you expect? Now predict: what happens when you tilt it sideways and the ball rolls? Test both guesses.
🔍 Detective rule: a metal ball rolling inside = a mechanical switch, so expect the inverted family.
import machine # Library that controls the board's pins
import time # Library for pauses
# Sets up pin26 as an input for the tilt sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
while True: # Repeat forever
state = pin26.value() # Read the raw tilt signal
print("Tilt signal:", state) # Send the raw value to the PC
time.sleep_ms(300) # Pause so the screen stays readableSerial Monitor (tilt the board slowly, then set it flat again):
Tilt signal: 1
Tilt signal: 1
Tilt signal: 0 ← the ball rolled! (sensor tilted)
Tilt signal: 0
Tilt signal: 1 ← back flat, ball rolled home
💡 Note: inside that little module is a metal ball and two contacts. Flat: the ball rests at the bottom, connecting the circuit one way. Tilt: the ball rolls away, breaking the connection. You just read a moving piece of metal through a number. Shake it gently and watch the signal flicker that's the ball bouncing. Physics, live on your screen.
🎮 INTERPRET
Goal:
translate the signal into orientation words.
Concepts:
Conditional Translation · Meaning Making
import machine # Library that controls the board's pins
import time # Library for pauses
# Sets up pin26 as an input for the tilt sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
while True: # Repeat forever
state = pin26.value() # Read the raw tilt signal
if state == 0: # Inverted: 0 = ball rolled away
print("TILTED I am falling over!") # Alarm message
else: # 1 = ball resting = standing straight
print("LEVEL standing proud") # Calm message
time.sleep_ms(300) # Pause between readingsSerial Monitor:
LEVEL standing proud
LEVEL standing proud
TILTED I am falling over!
LEVEL standing proud
💡 Note: think ahead a robot vacuum uses this exact information to know when it's climbing a ramp or about to tip over. Same signal, same two numbers... but soon, in Level 2, TILTED will not print a message: it will stop the motors. The data never changes; what you DO with it grows with your skills.
🎮 Report Only Changes
Goal:
build a quiet reporter it speaks ONLY when the state changes, not every 300 ms.
Concepts:
Change Detection · Quiet Monitoring · Both Edges
import machine # Library that controls the board's pins
import time # Library for pauses
# Sets up pin26 as an input for the tilt sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
prev = 1 # Remembers the previous reading (starts LEVEL)
while True: # Repeat forever
now = pin26.value() # Fresh reading
if now != prev: # The state CHANGED (either direction!)
if now == 0: # It just became TILTED
print(">>> Event: sensor TILTED") # Report the fall
else: # It just became LEVEL again
print(">>> Event: back to LEVEL") # Report the recovery
prev = now # Save this reading for comparison
time.sleep_ms(50) # Fast checking so quick tilts are caughtSerial Monitor (tilt it, hold, set flat, tilt again):
>>> Event: sensor TILTED
(you hold it tilted silence)
>>> Event: back to LEVEL
>>> Event: sensor TILTED
💡 Note: new trick inside a familiar pattern the comparison now != prev catches BOTH edges: falling AND recovering. Previous programs only cared about one direction; this one narrates the whole story. A real earthquake station works the same way: it stays silent during calm and speaks at every change. A good monitor talks less, not more.
🎮 How Long Was the Tilt?
Goal:
measure the duration of each tilt episode from the moment it starts to the moment the ball comes home.
Concepts:
Event Timing · Waiting Loops · Duration Analysis
import machine # Library that controls the board's pins
import time # Library for pauses and time
# Sets up pin26 as an input for the tilt sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
print("Tilt watch started tilt the sensor any time") # Instructions
while True: # Watch forever
if pin26.value() == 0: # A tilt just STARTED
start = time.ticks_ms() # Freeze the ms when the tilt began
print("Tilt began...") # Announce it
while pin26.value() == 0: # Wait WHILE it stays tilted...
time.sleep_ms(10) # ...checking every 10 ms
duration = time.ticks_diff(time.ticks_ms(), start) # End minus start
print("The tilt lasted:", duration, "ms") # Report the duration
if duration < 500: # Under half a second?
print("That was a quick wobble") # Short event
else: # Half a second or more
print("That was a serious lean!") # Long event
time.sleep_ms(20) # Calm checking between eventsSerial Monitor:
Tilt watch started tilt the sensor any time
Tilt began...
The tilt lasted: 340 ms
That was a quick wobble
Tilt began...
The tilt lasted: 2100 ms
That was a serious lean!
💡 Note: compare this with the button's 5.4 the skeleton is the same, but notice the difference in flow: the button program ran in rounds ("press now!"), while this one watches quietly and reacts whenever an event happens. This second style event-driven is how real monitoring systems work: security cameras, smoke detectors, seismographs. They don't ask you to perform; they just wait and measure.
🎮 Balance Master
Goal:
the balance challenge keep the sensor perfectly LEVEL for 10 straight seconds. Every tilt counts as a failure. The PC delivers your verdict at the end.
Concepts:
Timed Challenge · Failure Counting · Verdict Logic
import machine # Library that controls the board's pins
import time # Library for pauses and time
# Sets up pin26 as an input for the tilt sensor
# pin26 corresponds to D2 on the Sensor Shield V5.0
pin26 = machine.Pin(26, machine.Pin.IN)
print("=== BALANCE MASTER ===") # Mission title
print("Keep the sensor LEVEL for 10 seconds!") # The rules
print("Ready...") # Build suspense
time.sleep(1) # One second
print("3...") # Countdown
time.sleep(1) # One second
print("2...") # Countdown
time.sleep(1) # One second
print("1...") # Countdown
time.sleep(1) # One second
print("START! Steady hands!") # Go!
start = time.ticks_ms() # Begin the 10-second clock
fails = 0 # Failure counter
while time.ticks_diff(time.ticks_ms(), start) < 10000: # While under 10 seconds
if pin26.value() == 0: # Tilt detected FAILURE!
fails += 1 # One more failure
print("TILT! Failure number", fails) # Report it
while pin26.value() == 0: # Wait until the sensor is level again
time.sleep_ms(10) # ...so ONE tilt episode = ONE failure
print("Recovered... stay steady!") # Encouragement
time.sleep_ms(20) # Fast checking
print("TIME UP!") # End of challenge
if fails == 0: # Zero failures?
print("PERFECT BALANCE! You are a true master!") # Best verdict
elif fails <= 2: # One or two failures?
print("Nice! Only", fails, "failures almost perfect") # Good verdict
else: # Three or more
print("Wobbly hands!", fails, "failures train again!") # Try againSerial Monitor:
=== BALANCE MASTER ===
Keep the sensor LEVEL for 10 seconds!
Ready...
3...
2...
1...
START! Steady hands!
TILT! Failure number 1
Recovered... stay steady!
TILT! Failure number 2
Recovered... stay steady!
TIME UP! Nice! Only 2 failures almost perfect
💡 Note: this mission reuses your entire toolkit without teaching anything new countdown (from the bumper storm), timed loop, waiting cleanup, and now a graded verdict: three possible endings instead of one. Notice how if / elif / else turned a number into a judgment. Challenge a classmate: who achieves PERPECT BALANCE first? Then ask yourselves a Segway, a rocket, a walking robot: they all do exactly this, hundreds of times per second, just with a much faster sensor and motors instead of verdicts.
