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

How it Works

How the servo_plot Tests Work

The tests you just completed helped verify two things:

  1. Servo angle is controlled by pulse width.
  2. The servo signal’s period is 20 ms for a frequency of 50 Hz.

The 5 ms/div setting is fine for measuring the servo signal’s period, but a smaller Time/Div setting would be better for measuring the actual pulse width times.  Look forward to that in the Try This section.

Here is an excerpt from the servo_plot script:  

    pin16.write_analog(51)      # 51.2 -> 1000 us -> 45 degrees
    plot_servo(ch2="servo", delay=40)
    sleep(4000)

The pin16.write_analog(51) and sleep(4000) statements are from the original servo_test_angles script you used to test the servo in Connect and Test the Servo.  The plot_servo(ch2=”servo”, delay=40) function call tells the multimeter module to measure a servo signal with the Ch2 probe connected to P2 but wait 40 ms before starting the measurement.  This delay in taking the measurement ensures that the micro:bit has successfully switched from whatever servo signal it was sending previously to the new one.

Unlike a real oscilloscope, which would record thousands of voltage measurements and plot them, the multimetermodule measures the servo signal’s high and low times, in microseconds.  It uses those measurements to generate a list of voltages and times that it sends to the CYBERscope to emulate what a real oscilloscope does.

Try This: Adust the Settings

A smaller Time/Div setting will make it easier to measure the servo pulses.

  • Change the Time/Div dropdown to 5 ms to 0.5 ms.
  • Verify that the oscilloscope displays this when the servo points its horn in the 90° direction.

One approach to measuring pulses with an oscilloscope is to subtract the time the pulse’s rising edge occurs from the time the falling edge occurs.  In the case of the pulse for 90°, subtract 0.5 ms from 2 ms for a result of 1.5 ms.

 

  • Record the rising and falling edge times for each servo horn angle.
  • Calculate the pulse width for each of these servo angles:

Angle            Pulse width
0°
45°
90°              2 ms – 0.5 ms = 1.5 ms (Example)
180°    

 


Printer-friendly version
Script and Tests
Prev
One More Angle
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress