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.

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.

  • Position your cyber:bot so that there is nothing close by directly in front of it.
  • Set the cyber:bot board's 3-position power switch to position 1.
  • Enter, save, and flash the script test_left_IR to the micro:bot module.
#test_left_IR

from cyberbot import *

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

Once the script is fully flashed and running, the cyber:bot board, the LED labeled P20 should come 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.

  • Rename the script 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

  • Combine the right and left IR detector tests into a single script that tests both object detectors.
  • Above the while True loop, add a line of code that plays a 3000 Hz tone for one second, to signal that the script is running and ready for object detection.
  • Put the cyber:bot board's power switch in position 1, and flash the script.
  • 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 that 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)