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
  • Infrared Light Navigation for the cyber:bot

Infrared Light Navigation for the cyber:bot

Test the IR Object Detectors

Detecting Objects with Infrared

Object detection with the IR LED and receiver circuits you built on your cyber:bot takes three steps:

  1. Flash the IR LED on/off at 37500 Hz.
  2. Delay for a millisecond or more. This gives the IR receiver time to send a low signal if it detects 37500 Hz, that is 37.5 kHz, IR light reflecting off an object.
  3. Check the state of the IR receiver for either a high signal (no IR detected), or a low signal (IR detected).

The function ir_detect handles all of these steps for us.  Here is an example from our next script:


The ir_detect function generates a square wave on the IR LED pin; here the arguments specify a 37500 Hz signal on P14.  This square wave last about 2 milliseconds. Then, the function checks the state of the IR receiver pin; P13 in this example, and stores the result in the variable irL.  The state of the IR receiver pin.  IR receiver detects a reflection of the 37500 Hz infrared light signal, irL will store a zero, and if not, irL will store a 1— remember that this receiver is an “active low” sensor.

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.
  • Make sure the cyberbot.py module is added to the Project Files.
    (See Add modules to your micro:bit).

Example Script: test_left_ir

This script only tests the cyber:bot’s left IR detector.  Focusing on only one of the two object detector circuits at a time helps simplify trouble-shooting.  This is yet another example of subsystem testing.  After the subsystems check out, we can move to system integration.  But first, let’s make sure to test and correct any wiring or code entry errors that might have crept in.

  • Set the project’s name to test_left_IR, 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.)
#test_left_IR

from cyberbot import *

while True:
    irL = bot(14, 13).ir_detect(37500)
    bot(20).write_digital(irL)
  • Position your cyber:bot so that there is nothing close by directly in front of it.
  • Set the cyber:bot board’s PWR switch to position 1.
  • Check the cyber:bot board.  The LED labeled P20 should turn on.
  • Place an object, such as your hand or a sheet of paper, a couple of inches (5 cm) from the left object detector. The P20 LED should turn off.
  • Move the object away again. The P20 LED should turn on.
  • Once the LED is working properly for object not detected and object detected, move on to the Try This and Your Turn sections.

Try This – Test the Right IR Detector

Now it is time to modify the script to test the right-side object detector.

  • Change the project name from test_left_IR to test_right_IR.
  • Change bot(14, 13) to bot(1, 2).
  • Change irL to irR.
  • Change bot(20).write_digital(irL) to bot(21).write_digital(irR).
  • Repeat the testing steps in this activity for the cyber:bot’s right IR object detector.  
  • If necessary, troubleshoot any mis-wired circuits or code entry errors. 

Your Turn – Test Both Sides Together

The script below combines the right and left IR detector tests into a single script that tests both object detectors.

  • Change the project name from test_right_IR to test_both_IR_indicators.
  • Update the script so that it matches the one below, and then click Save.
  • Click Send to micro:bit.  
  • Make sure the cyber:bot board’s PWR switch is in position 1.
  • Verify that the speaker makes a clear, audible tone.
  • Verify that when you place an object in front of the left IR detector, the left (pin 20) LED turns off.
  • Repeat the test for the right IR detector and pin 21 LED.
#test_both_IR_indicators

from cyberbot import *

bot(22).tone(3000, 1000)

while True:
    irL = bot(14, 13).ir_detect(37500)
    irR = bot(1, 2).ir_detect(37500)
    bot(20).write_digital(irL)
    bot(21).write_digital(irR)

Setting PWR switch to 0 vs. Unplugging Battery Holder   

Taking a break, building a circuit, or writing a script?    

  • Always set the cyber:bot board’s PWR switch to 0 until it’s time to test the next script.

Whenever you are done for the day, always follow these steps.

  • Set PWR to 0.
  • Unplug the battery holder’s barrel plug from the cyber:bot board’s barrel jack.

Printer-friendly version
Build the IR Circuits
Prev
Check for IR Interference
Next

DISCUSSION FORUMS | PARALLAX INC. STORE

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

© 2025 Parallax Learn • Built with GeneratePress