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
  • cyber:bot Roaming with the Ping)))

cyber:bot Roaming with the Ping)))

Sub-system Ping))) and Servo Testing

Program the micro:bit to Use the Ping))) Ultrasonic Sensor

This project has several subsystems that we’ll test independently before we program to work together. First, we need to check the Ping))) Ultrasonic Sensor to see that it is wired properly and produces accurate measurements.

Hardware Setup

  • Set the cyber:bot board’s power (PWR) switch to Position 0.
  • Make sure the battery holder is loaded with 5 AA batteries.
  • Make sure the battery holder’s barrel plug is firmly plugged into the cyber:bot board’s barrel jack. 
  • Connect your micro:bit module to your computer with a USB cable.

Software Setup

  • In a Google Chrome or Microsoft Edge browser, go to python.microbit.org to open the micro:bit Python Editor.
  • Add two modules to the project: cyberbot.py, qti.py, and intbits.py.
    • Go to Add modules to your micro:bit.
    • Skip the Quick Start section, and instead start at the section titled Adding a Module to micro:bit Filesystem. 
    • Watch the video and then add the cyberbot.py module to the project by following the instructions from there to the end of the page. 
    • Next, follow the same steps to add the ping.py module to the project.  It will be in the same folder with cyberbot.py. 

Script: ping_test_with_serial_monitor

  • Make sure that you have completed the Hardware Setup and Software Setup tasks.
  • Set the project’s name to ping_test_with_serial_monitor, enter the script below, and then click Save.
    (See Save & Edit Scripts and Flash Scripts with Python Editor.)
  • Click Send to micro:bit.  
    (See Flash Scripts with Python Editor.)
# ping_test_with_serial_monitor

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    print('Distance:', distance, 'cm')
    sleep(750)

The Ping))) sensor has a range of approximately 3 cm to 3 m in a controlled environment with both the sensor and a single target elevated well above the floor.  For these tests, we’ll work on a range of 10 cm to 1 m, which is more realistic for a cyber:bot roaming close to the floor with multiple objects nearby.  

  • Set the cyber:bot board’s PWR switch to position 2.
  • Click Show serial to open the serial monitor.
    (See Use the Serial Monitor.)
  • Place an object in front of the Ping))) sensor as shown.  Start at 10 cm, and try a few distances like 10, 20, 30, 40, and 50 cm.  Then try 1 m.
  • Verify that the serial monitor displays distances that match the actual object distances within a few cm.
  • Set the cyber:bot board’s PWR switch to position 0.

Theremin

Once the Ping))) is working you could easily add code for the piezospeaker and create a theremin. A theremin is an electronic musical instrument where the tone is changed by the movement of your hand between two antennas. You will substitute the Ping))) Ultrasonic Sensor for the antennas.

  • Change the project’s name from ping_test_with_serial_monitor to ping_theremin. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
  • Set the cyber:bot board’s PWR switch to 2.
  • Vary the distance of an object (or your hand) from the Ping))) to create theremin-like sound effects.
  • Set the cyber:bot board’s PWR switch to 0.
# ping_theremin

from cyberbot import *
from ping import *

bot(22).tone(2000, 300)

while True:
    distance = ping(16).distance('cm')
    # print('Distance:', distance, 'cm')
    bot(22).tone((distance*50),50)

Program the micro:bit to Control the Ping))) Bracket’s Servo

The next subsystem to test is the Ping))) bracket’s servo. The Parallax Standard Servo is able to sweep 180 degrees, positioning the Ping))) at near-exact angles along the way so the micro:bit can take distance measurements on the sides and front of the cyber:bot.

  • Change the project’s name from ping_theremin to ping_servo_test. 
  • Enter the script below, and then click Save.
  • Click Send to micro:bit.  
# ping_servo_test

from cyberbot import *
from ping import *

# angles for L/R sweep scan
theta = [0, 30, 60, 90, 120, 150, 180, 165, 135, 105, 75, 45, 15]
# Offset from CW limit to actual 0 degree theta is 12 degrees
offset = 12   

index = 0
bot(22).tone(2000, 300)

while True:
    for index in range(0, 13):
        bot(17).servo_angle (theta[index] + offset)
        angle = theta[index] - 90
        display.scroll (angle)

Tests and Adjustments

The ping_servo_Test script programs the micro:bit to position the bracket’s servo through the entire range of motion, testing only the servo motor. If you find that your servo turns beyond the range shown in the picture below you will follow some instructions in this section to adjust it.

  • Set the cyber:bot board’s PWR switch to 2.
  • Compare the range of motion your servo points the ping against the diagram below.
  • If you find that your servo turns beyond the range shown in the picture:
    • Set the cyber:bot board’s PWR switch to 0 when the micro:bit starts to display 0.
    • This is the point where the script thinks the Ping))) should be pointing straight ahead.  
    • Remove the single screw that holds servo horn to the servo’s output spline.
    • Pull the Ping))) upwards and off the Ping))) Mounting Bracket.
    • Position the Ping))) so that it is pointing straight ahead, and press it back onto the servo spline.
    • Replace the screw that holds the servo horn to the servo.

 

The Ping_Servo_Test code example creates a list of values named theta, which represent the position the micro:bit will use to control the servo. The value of 0 degrees is far right and 180 degrees is far left. However, to make the next program easier to read and useful for navigation, we will create a variable named angle which calculates the straight-ahead value as 0 degrees, far right as -90 degrees and far left as 90 degrees from this line of code:

angle = theta[index] - 90

The angle value is simply 90 degrees less than the servo position controlled by the micro:bit.  The image shows how this adjustment makes it so that an angle of 0 degrees is straight ahead.  Anything on the left is a positive angle, and anything on the right is negative.  This comes in handy for navigation programming because your code can check if the object’s angle is greater or less than zero to decide which direction to turn.  


Printer-friendly version
Build the Ping))) Bracket and Circuit
Prev
cyber:bot Ping))) Roaming Program
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress