Skip to content
Parallax Learn

Parallax Learn

  • Welcome
  • Tutorials
        • Tutorial Series head tag

          Tutorial Series
        • Tutorial Series

          The special, classroom-ready series pages are organized collections of tutorials for our most popular hardware and/or languages. The tutorials for each topic are conveniently accessible from a single page, shown in the order it is recommended that they be completed.
        • Robotics Series Head tag

          Robotics Series
        • Robotics Series

          • Artificial Intelligence
          • Cybersecurity: Radio Data tutorialCybersecurity
          • cyber:bot + Python
          • cyber:bot + MakeCode
          • Boe-Bot Tutorial SeriesBoe-Bot
          • Arduino Shield-Bot
          • ActivityBot with C TutorialsActivityBot + C
          • ActivityBot with BlocklyProp Tutorial SeriesActivityBot + BlocklyProp
          • Scribbler 3 Tutorial SeriesScribbler 3
        • Electronics & Programming Series Head tag

          Electronics & Programming Series
          • BS2 Board of Education Tutorial SeriesBS2 Board of Education
          • Propeller C-Language BasicsPropeller C Basics
          • FLiP Try-It Kit C Tutorial SeriesFLiP Try-It Kit + C
          • FLiP Try-It Kit BlocklyProp TutorialsFLiP Try-It Kit + BlocklyProp
          • Badge WX Tutorial SeriesBadge WX
          • Propeller BlocklyProp Basics and ProjectsPropeller BlocklyProp Basics
          • View All Tutorial Series »
        • Browse Tutorials
        • Browse Tutorials

          Individual tutorials sorted by robot or kit, and language.
        • By Robot or Kit
          • ActivityBot
          • SumoBot WX
          • Boe-Bot
          • Shield-Bot
          • cyber:bot
          • Badge WX
          • ELEV-8
          • ARLO
        • By Language
        • By Language

          • Propeller C
          • Arduino
          • BlocklyProp
          • PBASIC
          • Python
          • MakeCode
          • View All Tutorials »
  • Educators
  • Reference
  • Downloads
  • Home
  • All Courses
  • AI for Simple Python Scripts

AI for Simple Python Scripts

Test the Existing Tilt Compass

Example script: display_tilt_direction_with_leds

This script is from Your Turn: Display Tilt Direction.  It’s part of a chapter about using the micro:bit as a tilt radio controller for the cyber:bot robot.  Here we will just focus on measuring and displaying tilt.

  • Enter the script display_tilt_direction_with_leds into the micro:bit Python Editor.
  • Use the Send to micro:bit button to flash the script into the micro:bit.
# display_tilt_direction_with_leds

from microbit import *
import math

sleep(1000)

while True:
    x = accelerometer.get_x()
    y = accelerometer.get_y()

    angle = round( math.degrees( math.atan2(y, x) ) )
    needle = ( angle + 90 + 15 ) // 30

    print("angle =", angle, ", needle =", needle)
    display.show(Image.ALL_CLOCKS[needle])
    
    sleep(200)

 

Test the Existing Tilt Compass

  • Try holding the micro:bit vertically, and rotating it like a car’s steering wheel.  Verify that it points downward as you do that, like in the “This is down” picture.
  • Also try holding the micro:bit flat and then tip it in various directions (like the micro:bit tilt controller in the cyber:bot video).
  • In both cases, holding vertically and rotating, or holding flat and tilting the micro:bit, verify that it points down.

How It Works

The micro:bit uses its built-in accelerometer to measure tilt.  To learn more about how the micro:bit measures tilt, try Test Tilts.

The micro:bit displays the tilts using one of twelve elements in the Image.ALL_CLOCKS[] list.  Image.ALL_CLOCKS[0] points to 12 o’clock, Image.ALL_CLOCKS[1] points to 1 o’clock, Image.ALL_CLOCKS[2] points to 2 o’clock, and so on…  Learn more at Display Tilt Direction.

The micro:bit can also measure tilt angles, as shown in Measuring Rotation Angles.

The micro:bit uses equations from trigonometry to change X and Y axis accelerometer measurements into tilt angles.  Don’t worry, you don’t have to understand trigonometry to use this app.  But, if you’re curious there’s more about it here:  Did You Know? Trigonometry and Rotation Angles.

 


Printer-friendly version
Try an Existing Tilt Compass
Prev
Tilt Compass App made with AI
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

About | Terms of Use | Feedback: learn@parallax.com | Copyright©Parallax Inc. 2024

© 2025 Parallax Learn • Built with GeneratePress