In earlier animations, you have seen how the write_analog(value) function calls make the micro:bit send repeating on/off (or high/low) signals to the servo. The servo positions its horn at an angle in response to how long the ‘on’ part of the signal stays on. These signals can be measured with the CYBERscope’s oscilloscope and compared to how this document said it’s supposed to work.
This kind of activity is the sort of thing students find themselves doing to make sure their motor will run properly before it’s connected. Engineers also do this to verify that their prototypes are running properly, and technicians often take similar measurements when a device with motors isn’t running properly and needs to be diagnosed and fixed.
In this activity, you will:
Same as previous activity + red alligator clip and jumper wire.
We’ll be using the P2 red alligator clip lead to monitor signals the micro:bit sends to the servo.
servo_plot.hex [1]
Your display should now resemble this one:
The time that the signal stays high (at 3.3 V in this case) is called the pulse width. Here, we are not going to measure the pulse width, just observe how it contains information that tells the servo what angle to hold.
One thing we can measure at 5 ms/div is the signal’s period (T). This was first introduced in the Connect and Blink a Light activity’s On-Off Signals pag [4]e. It introduced the period of an on/off signal like a blinking light or this servo signal as tHIGH + tLOW, but take a look at what happens between the rising edges in the CYBERscope. See how the time between rising edges contains both the high (pulse width) and low times in the signal?
The tests you just completed helped verify two things:
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 [6]. 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.
A smaller Time/Div setting will make it easier to measure the servo pulses.
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.
Angle Pulse width
0°
45°
90° 2 ms - 0.5 ms = 1.5 ms (Example)
180°
Desktop oscilloscopes have built-in tools to measure period, pulse width, frequency, and much more. You will often see these kinds of measurements along one of the outside edges of the plot.
This particular oscilloscope can be configured to display the measurements along the bottom of the plot.
The tests so far have not included 135°, let’s add that to the script and verify the result.
Predict the pulse width for 15°. Calculate the duty cycle for positioning the servo horn at 15° then write the script to verify your prediction that moves back and forth 0° and 15°.
Solution:
y = 0.5689x + 25.6
y = 0.5689(15) + 25.6
y = 34.1335 ≈ 34
# servo_plot from microbit import * from multimeter import * display.off() pin16.set_analog_period(20) while True: pin16.write_analog(26) plot_servo(ch2="servo", delay=40) sleep(4000) pin16.write_analog(34) plot_servo(ch2="servo", delay=40) sleep(4000)
The pulse width should be approx. 0.2 ms
Links
[1] https://learn.parallax.com/sites/default/files/content/Python/servo/servo_plot.hex
[2] https://python.microbit.org/v/2
[3] https://cyberscope.parallax.com
[4] https://learn.parallax.com/tutorials/language/python/led-lights/connect-and-blink-light/signals
[5] https://learn.parallax.com/tutorials/language/python/electrical-measurements/measurement-units/convert-prefixes-values
[6] https://docs.google.com/document/d/15auxYUR0YnEvQtKc6jFfm2a4BNcgdhoQRcPrR9eYj_Y/edit#heading=h.ov44fqdkdi74
[7] https://docs.google.com/document/d/15auxYUR0YnEvQtKc6jFfm2a4BNcgdhoQRcPrR9eYj_Y/edit#heading=h.6oak8nsfcryy