Test the Infrared Object Sensors

Let’s test the IR object sensors before using them to navigate.  That way, you can be sure they are wired correctly before trying to use them for navigation.  (If they are not wired correctly, they probably won’t detect any objects, and won’t work for navigation.)  The test code will use the Parallax Serial Terminal to show the status of each sensor: 1 if it sees an object, 0 if it does not.

  • Load this test code into the Propeller.
'' 1 Test IR Object Detectors with PST.spin

'' Use IR LED and IR receiver to detect object presence.
'' Display results in Parallax Serial Terminal.                                                

OBJ

  system : "Propeller Board of Education"             ' System configuration
  freq   : "PropBOE Square Wave"                      ' Square wave signal generator
  ir     : "PropBOE IR Detect"                        ' IR object detection
  pst    : "Parallax Serial Terminal Plus"            ' Serial communication object
  time   : "Timing"                                   ' Delay and wait convenience methods
 
VAR

  byte objectL, objectR                               ' Variables to store results

PUB Go                                                ' Startup method

  system.Clock(80_000_000)                            ' System clock -> 80 MHz
  freq.Out(4, 1000, 3000)                             ' P4 sends 1 s, 3 kHz tone to speaker
 
  repeat                                              ' Main loop repeats indefinitely
    objectL := ir.Detect(13, 12)                      ' Check for left object
    objectR := ir.Detect(0, 1)                        ' Check for right object
    
    Display                                           ' Call display method (below)
    time.Pause(20)                                    ' Wait 20 ms before repeating   

PUB Display                                           ' Display method for IR detectors

  pst.Home                                            ' Send cursor home (top-left)         
  pst.Str(string("objectL = "))                       ' Display "objectL = "
  pst.Bin(objectL, 1)                                 ' Display objectL value
  pst.Str(string("  objectR = "))                     ' Display "objectL = "
  pst.Bin(objectR, 1)                                 ' Display objectL value
 
  • Open your Parallax Serial Terminal software, make sure it’s connected to the same COM Port as the Propeller Board of Education, and click the Enable button.  (Click here for a reminder how to do this.) 
  • Put your hand in front of both sensors (just a few cm in front) and verify that the Serial Terminal displays 11.
  • Point the IR sensors away from any objects and verify that the terminal displays 00.
  • Try putting your hand a few cm in in front of the left sensor and verify it displays 10.
  • Try putting your hand a few cm in front of the right sensor and verify that it displays 01.
  • If it passed all these tests, you are ready to move on.  If not, it’s time to take a closer look at your wiring and find the mistake.

Both sides detect objects

 

Neitehr side detects objects

 

Left side detects objects

 

Right side detects objects

How It Works

The Propeller BOE IR Detect object has a method named DetectDetect has parameters for pins connected to the IR LED and IR Receiver, and it returns 1 if it detects an object, or 0 if it does not.  So, objectL := ir.Detect(13, 12) turns on the IR LED flashlight connected to P13 and then check’s the IR receiver’s output connected to P12.  If the IR receiver sees an object, the Detect method returns 1; otherwise, it returns 0.  The expression objectL := ir.Detect(13, 12) copies that value to the objectL variable.  The code repeats that for the right IR sensor using objectR := ir.Detect(0, 1)

It then displays both values by calling the Display method.  The Display method sends the cursor to the top-left “home” position with pst.Home.  Then, it displays the “IR Detectors” and “Left     Right” headings.  Next, it positions the cursor 2 characters over and 2 lines down.  That way, pst.Bin(objectL, 1) displays the 1-digit binary result at that position, right below the “f” in “Left”.  pst.Position(8, 2) does the same thing for displaying the binary value for the right IR detector.

Try This

The Input Output Pins object has a method named Out that can turn an LED light on with a 1 or off with a 0.  For example, pin.Out(10, 1) will turn on the P10 IR LED, and pin.Out(10, 0) will turn it off.  Instead of 1 or 0, you could substitute the objectL variable in the Out method.  That way, when objectL stores a 1 (object detected), the light turns on.  Likewise, when objectL stores 0 (no object detected), the light turns off.

  • Modify your repeat loop so that it looks like this.
  repeat                                              ' Main loop repeats indefinitely

    objectL := ir.Detect(13, 12)                      ' Check for left object
    objectR := ir.Detect(0, 1)                        ' Check for right object

    pin.Out(10, objectL)                              ' Left object -> left LED
    pin.Out(8, objectR)                               ' right object -> right LED

    time.Pause(20)                                    ' Wait 20 ms & try again
  • Load the modified code into your board.
  • Test to make sure the red R1 light turns on when the left sensor detects an object, and the green G0 light turns on when the right sensor detects an object.
  • Make sure to study the pin.Out method calls.  See how the values the variables store control the lights? What does this tell us about storing variables?

NOTE: You can also try 2 Test IR Object Detectors with LEDs.spin.  It’ll display the LEDs without any messages to the Parallax Serial Terminal.

Your Turn

You can modify display for custom messages.  Here is an example of a modified Display method that only displays a message if it detects an object on the right.

  • Save a copy of your example program.  Use Save as and get it a new name.
  • Replace the display method in”1 Test IR Object Detectors with PST” with the one below.
  • Load the modified program into the Propeller and test
PUB Display                                           ' Display method for IR detectors

  if objectR == 1                                     ' If right detector "sees" object
    pst.Str(string("Object detected on right!"))      ' Display message
    pst.NewLine                                       ' Move cursor to start of next line
    time.Pause(100)                                   ' Wait 0.1 seconds.

You’ve already got code in your program that makes the speaker beep at the when the program starts.  How about making the speaker tell you which obstacle it sees?  For example, you could change the freq.Out call’s duration parameter from 1000 to 100 to make it beep briefly, and change the frequency parameter from 3000 to 3500 to make it higher pitched.  You could also choose different pitches for the left and right IR sensors.  Hint: The “Try This” section already has an example that executes some method calls, but only when the right sensor reports an object.  So, you could start by replacing pst.Str calls with freq.Out calls.

  • Try it!

Did You Know?

The IR receiver itself is an active-low sensor sends a high signal (1) to the I/O pin if it does not see an object, or a low (0) signal if it does.  The PropBOE IR object’s Detect method inverts that value, returning a 1 if it does see an object, or a 0 if it does not.  That makes the code more intuitive to understand.

The PropBOE IR Detect object makes the IR LED flash on/off at 38,000 times per second (that’s 38 kHz).  It has to do that because the IR receiver is only designed to send a low signal when it sees IR light blinking at that frequency.  It uses that frequency to distinguish between messages from your TV remote and infrared that comes from normal indoor lighting and the sun.

Infrared is not visible to us, but some cameras can see it.  For example, here is a picture of a TV remote pointed at the camera in a cell phone.  Normal digital cameras can see it too.  Not all cameras will see it though, for example a lot of iPhones do not.

A TV remote flashes its IR light at 38 kHz for different amounts of time to transmit binary numbers.  For example, a remote sending a message to a SONY TV might keep its IR LED flashing for 2.4 ms to signal the start of a message, then 0.6 ms to send a binary 0, then 1.2 ms to send a binary 1, then 0.6 ms again to send another binary 0.  The binary value 010, which is 0x22 + 1x21 + 0x20, which works ou t to 0x4 + 1x2 + 0x1, which equals the number 2.  (With three binary digits, you can have numbers from 0 to 7.)