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
  • Servo Position Control with Python and micro:bit

Servo Position Control with Python and micro:bit

Pass Statement


Did You Know? Pass Statement

If you don’t rotate the servo with the micro:bit and board, its horn will turn the opposite direction the micro:bit turns.  The statement that makes the servo turn the opposite direction from the micro:bit is angle = 180 – angle in this if…else… statement:

    if angle >= 0:
        angle = 180 - angle
    else:
        continue

To make it turn the same direction, the angle = 180 – angle statement needs to be removed.  That might seem simple enough, but it’s in an if… statement.  So, you can’t just comment it or remove it because MicroPython requires an if… statement to contain at least one statement (below and indented).  Otherwise, the script would stop executing statements and display an exception message when it reached the if… statement.

To make sure theif… statement still contains a statement MicroPython can execute, you can simply add a pass statement.  A pass statement essentially tells MicroPython: “Don’t do anything here, just move on to the next statement.”  Even though it’s a statement that doesn’t actually do anything, it’s still a MicroPython statement.   

    if angle >= 0:
        # angle = 180 - angle
        pass
    else:
        continue

Here’s another way to rewrite theif… statement so it still skips servo positioning in response to angles outside 0…180.  Angles outside the 0…180 range will be negative: -1…-179.  So, another approach would be to rewrite the if…else… statement so it’s just an if statement that only uses continue to skip the servo positioning by jumping back to while True when angle is less than zero.   When angle is greater than zero, this if… statement allows the script to keep going through the last statement in the loop, which updates the servo horn’s position with pin16.write_analog(value).     

    if angle <= 0:
        continue

Your Turn

The Did You Know section above explained how to make the servo horn mirror the board’s rotation if the servo is sitting still and the board is rotated.  

  • Click the Open button and reopen accelerometer_servo_points_up script.
  • Click Send to micro:bit.  
  • Set the servo on a desk or tape it to a wall with the cable to the top, and rotate the board.  The horn should rotate in the opposite direction you rotate the board.
  • Change the project name field to tilt_controlled_servo.
  • Try one of the two script changes explained in the Did You Know section above.
  • Verify that the direction the horn points mirrors how you rotate the board (in the 0° to 180° range.)
  • Click Save to save your work.

 


Printer-friendly version
How it Works
Prev
Review and Practice
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress