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

AI-generated Tilt Compass Script

ChatGPT’s Example Script

User

Please write a Python script for the micro:bit that will allow me to rotate it like a steering wheel and:

  1. Have a Python script use the micro:bit module’s accelerometer to measure its rotation angle.
  2. Always point down in its 5×5 LED display.  
  3. Use the clock hands found in micro:bit compass examples to display the downward direction as one of twelve possible hour positions.

ChatGPT

Certainly! Below is a simple Python script for the micro:bit that uses the accelerometer to measure the rotation angle and displays the downward direction as one of twelve possible hour positions on the LED display. The script uses the compass module for displaying the clock hands.

from microbit import *

def get_rotation_angle():
    # Get the accelerometer readings
    x, y, z = accelerometer.get_values()

    # Calculate the rotation angle
    angle = atan2(y, x) * (180 / 3.14159)

    # Ensure the angle is positive
    angle = (angle + 360) % 360

    return angle

def display_clock_hand(angle):
    # Calculate the hour position
    hour_position = int(angle / 30)

    # Display the clock hand
    display.show(Image.ALL_CLOCKS[hour_position])

while True:
    # Get the rotation angle
    rotation_angle = get_rotation_angle()

    # Display the downward direction as a clock hand
    display_clock_hand(rotation_angle)

    # Optional: You can add a sleep time to avoid rapid updates
    sleep(100)

Explanation

  • The get_rotation_angle function calculates the rotation angle based on accelerometer readings.
  • The display_clock_hand function determines the hour position based on the rotation angle and displays the corresponding clock hand on the LED display.
  • The script runs in a loop, continuously updating the display based on the current rotation angle.
  • This script assumes that the micro:bit is lying flat, and you are rotating it like a steering wheel around the z-axis. Adjustments may be needed based on the orientation of your micro:bit during usage.

Tests

Although the script looks very different from the example you already tried, it still might serve as a decent starting point.  

  • Examine the script.
    • Is it using the accelerometer instead of the magnetometer?
    • Do you think it will work?
  • Try the script, and name it AI_tilt_compass_first_try.
    • Does it display the tilt, or does it display an error message?

 


Printer-friendly version
Tilt Compass App made with AI
Prev
How it Doesn’t Work
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress