Introduction: Boe-Bot With Infrared Detectors

This instructable will demonstrate how to build and code a Boe-Bot that can navigate a maze using infrared detectors to avoid obstacles. This is an easy to follow guide that allows for easy modifications to suit your needs. This requires a basic understanding of circuitry and programming. You will need to have BASIC Stamp IDE software for this project. Free for download here. As well as the Boe-Bot Robot

Step 1: Resources

Electronic Components

Boe-Bot with connector cable Parallax Store - BoeBot Kit

5 Infrared LED's Parallax Store - IR Transmitter Assembly Kit

5 Infrared Shield Assemblies

5 infrared Detectors Parallax Store - BoeBot IR Receiver

Resistors

Assorted wiring ABRA Electronics - 22 Gauge Wire

3 LED's ABRA Electronics - 5mm Red LED

Support

Computer

BASIC Stamp Editor - (Freeware)

Tools

Wire Cutter ABRA Electronics - Wire Cutter (Optional)

Wire Stripper ABRA Electronics - Wire Stripper

Misc

Walls (to build maze)

Step 2: Understanding How Infrared Detection Works (Optional)

Infrared Headlights

The infrared object detection system we’ll build on the Boe-Bot is like a car’s headlights in several respects. When the light from a car’s headlights reflects off obstacles, your eyes detect the obstacles and your brain processes them and makes your body guide the car accordingly. The Boe-Bot will use infrared LEDs for headlights. They emit infrared, and in some cases, the infrared reflects off objects and bounces back in the direction of the Boe-Bot. The eyes of the Boe-Bot are the infrared detectors. The infrared detectors send signals indicating whether or not they detect infrared reflected off an object. The brain of the Boe-Bot, the BASIC Stamp, makes decisions and operates the servo motors based on this sensor input. Figure 7-1 Object Detection with IR Headlights The IR detectors have built-in optical filters that allow very little light except the 980 nm infrared that we want to detect with its internal photodiode sensor. The infrared detector also has an electronic filter that only allows signals around 38.5 kHz to pass through. In other words, the detector is only looking for infrared that’s flashing on and off 38,500 times per second. This prevents IR interference from common sources such as sunlight and indoor lighting. Sunlight is DC interference (0 Hz), and indoor lighting tends to flash on and off at either 100 or 120 Hz, depending on the main power source in the region. Since 120 Hz is outside the electronic filter’s 38.5 kHz band pass frequency, it is completely ignored by the IR detectors.

-Paralax Student Guide

Step 3: Assembling IR LED's

Insert the IR LED into the larger part of the casing

Enclose the clear part of the LED with the smaller part of the casing

Step 4: Testing Infrared Pairs - Circuit

Before we get too deep into anything, we will test to make sure that the IR pair works (One infrared LED and one infrared detector).

Start by building the above circuit on the breadboard mounted on top of your Boe-Bot

Step 5: Testing Infrared Pairs - Basic Code

Of course, we will need to write code for our IR pairs to work

To do this, will will use the FREQOUT command. This command was designed for audio tones, however it can be used to produce frequencies in the infrared range. For this test we will use the command:

 FREQOUT 8, 1, 38500

this will send a 38.5 kHz frequency that lasts 1 ms to P8. The infrared LED circuit connected to P8 will broadcast this frequency. If the infrared light is reflected back to the Boe-Bot by an object in its path, the infrared detector will send the BASIC Stamp a signal to let it know that the reflected infrared light was detected.

The key to making an IR pair work is to send 1 ms of 38.5 kHz FREQOUT and immediately store the IR detector’s output in a variable.

This example shows storing the IR Detector value in a bit variable named irDectectLeft

FREQOUT 8, 1, 38500 
 irDetectLeft = IN9 

The IR detector’s output state when it sees no IR signal is high. When the IR detector sees the 38500 Hz harmonic reflected by an object, its output is low. The IR detector’s output only stays low for a fraction of a millisecond after the FREQOUT command is done sending the harmonic, so it’s essential to store the IR detector’s output in a variable immediately after sending the FREQOUT command. The value stored by the variable can then be displayed in the Debug Terminal or used for navigation decisions by the Boe-Bot.

Step 6: Testing Infrared Pairs - Hardware + Software

Now that you know the basics, we can put the hardware and Software together to test a pair together and get real-time feedback from what the IR Pair is detecting

You can try and make the code yourself, or use the code below

' {$STAMP BS2} 
' {$PBASIC 2.5}
irDetectLeft VAR Bit
DO
 FREQOUT 8, 1, 38500
 irDetectLeft = IN9
 DEBUG HOME, "irDetectLeft = ", BIN1 irDetectLeft
 PAUSE 100
LOOP
  1. Leave the Boe-Bot connected to the serial cable, because you will be using the DEBUG Terminal to test your IR pair.
  2. Place an object, such as your hand or a sheet of paper, about an inch from the left IR pair
  3. Verify that when you place an object in front of the IR pair the Debug Terminal displays a 0, and when you remove the object from in front of the IR pair, it displays a 1.
  4. If the Debug Terminal does not display the expected values, try the steps in the Trouble-Shooting Step.

Step 7: Trouble-Shooting (For Issues With Last Step)

DEBUG Terminal displaying unexpected values

Check circuit for shorts, misplaced or missing connectors, damaged components, incorrect resistors, or any other visible issue

Check program from logical or syntax errors - If you used your own code for the last step, consider using provided code

Always getting 0, even when there are no objects is placed in front of the Boe-Bot

Check if there are any nearby objects that are reflecting the infrared signal. The table in front of the Boe-Bot could be the cause. Move the Boe-Bot into open space so that the IR LED and detector cannot be reflecting off any nearby object.

Reading is 1 most of the time when there is no object in front of the Boe-Bot, but flickers to 0 occasionally

There may be interference from a nearby fluorescent light; Turn off any nearby fluorescent lights and repeat your tests. If the problem persists, step 9 may reveal the problem

Step 8: Second IR Pair

Now that you have the program for the left IR, it's your turn to make the circuit and program the right IR Pair

  1. Change the DEBUG statement, title and comments to refer to the right IR pair.
  2. Change the variable name from irDetectLeft to irDetectRight. You will need to do this in four places in the program.
  3. Change the FREQOUT command’s Pin argument from 8 to 2.
  4. Change the input register monitored by the irDetectRight variable from IN9 to IN0.
  5. Repeat the testing steps in this activity for the right IR pair; with the IR LED circuit connected to P2 and the detector connected to P0.

Step 9: Detecting Infrared Interference (Optional)

Whether you're experiencing issues with detecting signals that should not be detected or you plan on demonstrating your IR detection in an alternate location, you may want to test for interference.

The concept of this testing program is pretty simple, you detect for infrared signals without sending any out.

You can use the exact same circuit but you will have to alter the code. you may choose to write your own code, but you may use the provided code below:

' {$STAMP BS2} 
' {$PBASIC 2.5}
irDetectLeft VAR Bit
DO
 irDetectLeft = IN9
 irDetectRight = IN0
 IF IN9 = 0 OR IN0 = 0 THEN
   DEBUG "Interference detected" 
 PAUSE 100
LOOP

If you do experience interference, determine the likely source and turn it off/remove it or relocate where you operate your Boe-Bot.

Step 10: Adding More IR Pairs

If you desire more accuracy in the movement of your Boe-Bot, you may want to add more IR Pairs. 3 Greatly improves performance compared to two; you can use a centre pair to scan for a direct obstacle, and use two side IRs to determine how much to turn. However, the downfall of the 3 IR pair design is that you may know when you are sliding against a wall, because the centre IR pair is used to detect obstacles. To solve this problem, you can add an IR pair to each side with a high resistance value—therefore and infrared signal will only be detected if the Boe-Bot is close to the side or a wall on a gentle angle.

Step 11: Five IR Pairs - Circuit

Be careful when directing the two IR LEDs on the side as twisting them may cause the leads to touch and cause a short circuit.

Step 12: Five IR Pairs - Code

You may want to try programming your Boe-Bot before using this code:

' {$STAMP BS2}<br>' {$PBASIC 2.5}
'Five IR Pair Deatection Code
'Matthew Shaw
'May 8 2019 (Version 7)
'Detection for objects and basic logical processing to solve mazes
irDetectLeft VAR Bit       'Variable for left
irDetectCentre VAR Bit     'Variable for centre
irDetectRight VAR Bit      'Variable for right
irDetectLSide VAR Bit      'Variable for left side
irDetectRSide VAR Bit      'Variable for right side
irDetectLSideFar VAR Bit   'Variable for left side low resistance
irDetectRSideFar VAR Bit   'Variable for right side low resistance
mLoop VAR Word
Lmotor PIN 15    'The left motor is connected to pin 14, pulses go through here
Rmotor PIN 14    'right = 15
    'speeds are -> 650-750-850
LFast CON 850    'Conastant for left motor at full speed
RFast CON 650    'Conastant for right motor at full speed
LStop CON 750    'Conastant for left motor at full speed
RStop CON 650    'Conastant for right motor at full speed
LMid CON 830     'Conastant for left motor at medium speed
RMid CON 700     'Conastant for right motor at medium speed
LSlow CON 770    'Conastant for left motor at minimum speed
RSlow CON 730    'Conastant for right motor at minimum speed
LRev CON 650     'Conastant for left motor at full speed in reverse
RRev CON 850     'Conastant for left motor at full speed in reverse
    FREQOUT 7, 1, 38500    'left side
    irDetectLeft = IN8
    FREQOUT 6, 1, 38500    'centre
    irDetectCentre = IN5
    FREQOUT 3, 1, 38500    'right side
    irDetectRight = IN2
    FREQOUT 10, 1, 38500   'Left Close
    irDetectLSide = IN11
    FREQOUT 1, 1, 38500    'right Close
    irDetectRSide = IN0
    FREQOUT 9, 1, 38500
    irDetectLSideFar = IN11
    FREQOUT 4, 1, 38500    'right side
    irDetectRSideFar = IN0
   IF irDetectLSide = 0 AND irDetectRSide = 0 THEN main   'STARTING COMMAND wave your hands past the two side detectors to start program
Main:
PAUSE 1000
DO
    PULSOUT Lmotor, LFast 'left motor runs on full speed
    PULSOUT Rmotor, RFast 'Right motor runs on full speed
    FREQOUT 6, 1, 38500    'centre
    irDetectCentre = IN5
    FREQOUT 10, 1, 38500   'Left Close
    irDetectLSide = IN11
    FREQOUT 1, 1, 38500    'right side
    irDetectRSide = IN0
    IF irDetectLSide = 0 AND irDetectRSide = 1 THEN
       DO
           PULSOUT Lmotor, LFast
           FREQOUT 6, 1, 38500    'centre
           irDetectCentre = IN5
               IF irDetectCentre = 0 THEN cent
           FREQOUT 10, 1, 38500   'Left Close
           irDetectLSide = IN11
           FREQOUT 3, 1, 38500    'right side
           irDetectRight = IN2
        LOOP UNTIL irDetectLSide = 1 OR irDetectRSide = 0
    ELSEIF irDetectLSide = 1 AND irDetectRSide = 0 THEN
        DO
           PULSOUT Rmotor, RFast
           FREQOUT 6, 1, 38500    'centre
           irDetectCentre = IN5
               IF irDetectCentre = 0 THEN cent
           FREQOUT 10, 1, 38500   'Left Close
           irDetectLSide = IN11
           FREQOUT 3, 1, 38500    'right side
           irDetectRight = IN2
        LOOP UNTIL irDetectLSide = 0 OR irDetectRSide = 1
    'ENDIF
    IF irDetectCentre = 0 THEN                      'START
        FREQOUT 7, 1, 38500    'left side
        irDetectLeft = IN8
        FREQOUT 6, 1, 38500    'centre
        irDetectCentre = IN5
        FREQOUT 3, 1, 38500
        irDetectRight = IN2
      PAUSE 1000 'pause to show signal detected
        IF (irDetectLeft = 1 AND irDetectRight = 0) THEN                      ' evaluate duration
          GOSUB turnLeft
        ELSEIF (irDetectLeft = 0 AND irDetectRight = 1) THEN
          GOSUB turnRight
        ELSEIF (irDetectLeft = 1 AND irDetectRight = 1) THEN
          GOSUB turnDecide
        ELSE
          GOSUB turnReverse
        ENDIF
    ENDIF                                           'END
LOOP
END
turnLeft:
    DO
        PULSOUT Lmotor, LRev
        FREQOUT 8, 1, 38500
        irDetectLeft = IN9
        FREQOUT 5, 1, 38500
        irDetectCentre = IN4
        FREQOUT 2, 1, 38500
        irDetectRight = IN0
    LOOP UNTIL IN0 = 1
RETURN
turnRight:
    DO
        PULSOUT Rmotor, RRev
        FREQOUT 8, 1, 38500
        irDetectLeft = IN9
        FREQOUT 5, 1, 38500
        irDetectCentre = IN4
        FREQOUT 2, 1, 38500
        irDetectRight = IN0
    LOOP UNTIL IN9 = 1
RETURN
turnReverse:
    FOR mLoop = 0 TO 50
        PULSOUT Rmotor, RRev
        PULSOUT Lmotor, LRev
        PAUSE 20
        PULSOUT Lmotor, LRev
        PAUSE 20
    NEXT
    DO
        PULSOUT Rmotor, RRev
        FREQOUT 8, 1, 38500
        irDetectLeft = IN9
        FREQOUT 5, 1, 38500
        irDetectCentre = IN4
        FREQOUT 2, 1, 38500
        irDetectRight = IN0
    LOOP UNTIL IN9 = 1
RETURN
turnDecide: 'uses lower resistance to see further
    FREQOUT 9, 1, 38500
    irDetectLSideFar = IN11
    FREQOUT 4, 1, 38500    'right side
    irDetectRSideFar = IN0
    IF (irDetectLSideFar = 1 AND irDetectRSideFar = 0) THEN                      ' evaluate duration
          GOSUB turnLeft
        ELSEIF (irDetectLSideFar = 0 AND irDetectRSideFar = 1) THEN
          GOSUB turnRight
        ELSEIF (irDetectLSideFar = 1 AND irDetectRSideFar = 1) THEN
          GOSUB turnLeft
        ELSE
          GOSUB turnReverse
    ENDIF
RETURN