LEARN.PARALLAX.COM
Published on LEARN.PARALLAX.COM (https://learn.parallax.com)
Home > Infrared Light Navigation for the cyber:bot

Infrared Light Navigation for the cyber:bot

What it’s about

In a previous tutorial, the cyber:bot used whiskers to get around, but they only detect obstacles when the robot bumps into something. Wouldn’t it be better if the cyber:bot could just “see” objects and then decide what to do about them? 

Well, that’s what it can do with infrared headlights and eyes like the ones shown below. Each headlight is an infrared LED inside a tube that directs the light forward, just like a flashlight. Each eye is an infrared receiver that sends the micro:bit module high/low signals to indicate whether it detects the infrared LED’s light reflected off an object.

Before you start

You will need:

  • The cyber:bot extension ready to use with your micro:bit software
  • A fully assembled cyber:bot robot
  • Components from your cyber:bot kit’s electronics pack
  • White poster board or paper – not included
  • Black electrical tape or flat black tempera or poster paint – not included
  • Infrared remote, or another cyber:bot, to generate infrared interference – not included
  • ruler – not included

After you finish

You will then be ready to combine infrared navigation with other cyber:bot capabilities. You might also be interested in the project Control your cyber:bot with an Infrared TV Remote.

Infrared Light Signals

Infrared is abbreviated IR, and it is light the human eye cannot detect. The IR LEDs introduced in this chapter emit infrared light, just like the red LEDs we’ve been using emit visible light, but at a different wavelength.

The infrared receivers in this chapter detect infrared light, similar to the phototransistors in the tutorial Visible Light Navigation for the cyber:bot. But, there’s a difference—these infrared receivers are not just detecting ambient light, they are designed to detect infrared light flashing on and off very quickly. 

The infrared LED that the cyber:bot will use as a tiny headlight is actually the same one you can find in just about any TV remote. The TV remote flashes the IR LED to send messages to your TV. The microcontroller in your TV picks up those messages with an infrared receiver like the one your cyber:bot will use.

The TV remote sends messages by flashing the IR LED very fast, at a rate of about 38 kHz (about 38,000 times per second). The IR receiver only responds to infrared if it’s flashing at this rate. This prevents infrared from sources like the sun and incandescent lights from being misinterpreted as messages from the remote. So, to send signals that the IR receiver can detect, your Propeller chip will have to flash the IR LED on/off at 38 kHz.

Some fluorescent lights do generate signals that can be detected by the IR receivers.
These lights can cause problems for your cyber:bot’s infrared headlights. One of the things you will do in this chapter is develop an infrared interference “sniffer” that you can use to test the fluorescent lights near your cyber:bot courses.

The light sensors inside most digital cameras, including cell phones and webcams, can all detect infrared light. By looking through a digital camera, we can “see” if an infrared LED is on or off. These photos show an example with a digital camera and a TV remote. When you press and hold a button on the remote and point the IR LED into the digital camera’s lens, it displays the infrared LED as a flashing, bright white light.

The pixel sensors inside the digital camera detect red, green, and blue light levels, and the processor adds up those levels to determine each pixel’s color and brightness. Regardless of whether a pixel sensor detects red, green, or blue, it detects infrared. Since all three-pixel color sensors also detect infrared, the digital camera display mixes all the colors together, which results in white.

Infra means below, so infrared means below red.
The name refers to the fact that the frequency of infrared light waves is less than the frequency of red light waves. The wavelength our IR LED transmits is 980 nanometers (abbreviated nm), and that’s the same wavelength our IR receiver detects. This wavelength is in the near-infrared range. The far-infrared range is 2000 to 10,000 nm, and certain wavelengths in this range are used for night-vision goggles and IR temperature sensing.

Build the IR Circuits

Build the IR Circuits

In this activity, you will build infrared object detector circuits on the cyber:bot robot’s breadboard.

Parts List
(2) IR receivers
(2) IR LEDs (clear case)
(2) IR LED shield assemblies
(2) Resistors, 220 Ω (red-red-brown)
(2) Resistors, 2 kΩ (red-black-red)
(misc) Jumper wires

Gather the parts in the Parts List, using the drawings below to help identify the infrared receivers, LEDs, and shield assembly parts.

Make sure you are using infrared LEDs and not phototransistors. The infrared LED has a taller and more rounded plastic dome:

Assemble the IR Headlights

The infrared LEDs are designed to sit in the standoff tubes a certain way. The flat spot on the LED dome matches a flat ledge inside the tube. Holes on the bottom of the tube are labeled for the anode (+) and cathode (-) leads.

  • Insert the infrared LED into the LED standoff tube, threading the anode and cathode leads through their respective holes in the bottom. If properly aligned, the LED should drop in place without pushing.
  • Slip the short tube over the IR LED’s clear plastic case, tab end first. The tabs should snap into the standoff, holding the LED in place.

IR Object Detection Circuit

Each IR object detector requires its own IR LED and IR receiver, mounted together on a corner of the breadboard closest to the very front of the cyber:bot.

Note that the anode lead of each IR LED connects to a 2 kΩ resistor (red-black-red). Also note that each IR LED and each IR receiver has its own ground connection wire. In the diagram, the IR LED ground wires are shown in blue to help tell them apart.

  • Put the cyber:bot board’s power switch in position 0 — standard practice before building or modifying circuits.
  • Build the circuit in the schematic below, using the wiring diagram as a reference for parts placement.

Watch your IR LED anodes and cathodes! 
The anode lead is the longer lead on an IR LED by convention. The cathode lead is shorter and mounted in the plastic case closer to its flat spot. These are the same conventions as the red LEDs in the kit.

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 block ir detect handles all of these steps for us. Here is an example from our next project:

The ir detect block generates a square wave on the IR LED pin; here the arguments specify a 37500 Hz signal on P14. This square wave lasts about 2 milliseconds. Then, the block 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. If the 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 browser, go to makecode.microbit.org to open the micro:bit Makcode Editor.
  • Make sure the cyberbot.py module is added to the Project Files.
    (See Add extensions to your micro:bit).

Example project: test_left_ir

This project only tests the cyber:bot’s left IR detector. Focusing on only one of the two object detector circuits at a time helps simplify troubleshooting. 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 name to test_left_IR and enter the project below.
    (See Save & Edit Projects and Flash Projects with MakeCode Editor.)
  • Click Download.
    (See Flash Projects with MakeCode Editor.)

Once the project 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 project to test the right-side object detector.

  • Rename the project test_left_IR to test_right_IR, or make a new one.
  • Change in (Pin14) out (Pin13) to in (Pin1) out (Pin2).
  • Change irL to irR.
  • Change (Pin20) write digital (irL) to (Pin21) 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 project that tests both object detectors.
  • In the on start loop, add a block that plays a 3000 Hz tone for one second, to signal that the project is running and ready for object detection.
  • Put the cyber:bot board’s power switch in position 1, and flash the project.
  • 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.

Check for IR Interference

You might have found that your cyber:bot said it detected something even though nothing was in range. That may mean a nearby device is generating some IR light at a frequency close to 37.5 kHz. It might also mean that direct sunlight streaming through a window is causing false detections. 

If you try to have a cyber:bot contest or demonstration near one of these light sources, your infrared systems could end up performing very poorly! So, before any public demo, make sure to check the prospective navigation area with this IR interference “sniffer” project ahead of time.

The concept behind this project is simple: don’t transmit any IR through the IR LEDs, just monitor to see if any IR is detected. If IR is detected, sound the alarm using the piezospeaker.

Many handheld remotes generate IR interference.
Many TVs, VCRs, CD/DVD players, and projectors use the same type of IR detectors you have on your cyber:bot right now. Look at the end of the device’s remote for an LED. If it is the kind of remote that must point directly at the device with a clear line of sight to work, it is likely using infrared. Try pointing it at your cyber:bot and repeatedly press/release one of the remote’s buttons to generate IR interference.

Example Project: IR_interference_sniffer

With this project, your cyber:bot will play a tone and turn on its indicator LEDs if it detects IR 37.5 kHz infrared signals. Since the project does not turn on the IR LEDs on the cyber:bot breadboard, a response means the IR interference has to be coming from an outside source.

If you are in a classroom, you can test with a separate cyber:bot that’s running test_both_IR_indicators. Just point its IR LEDs into the IR_interference_sniffer bot’s IR receivers. 

If you don’t have a second cyber:bot, just use a handheld IR remote for a TV, VCR, CD/DVD player, or projector. Simply point the remote at the cyber:bot and repeatedly press and release one of its buttons. If the cyber:bot responds by sounding the alarm, you know your IR interference sniffer is working.

  • Put the cyber:bot board’s power switch in position 1.
  • Enter and flash the project IR_interference_sniffer to your micro:bit module.
  • Test to make sure the cyber:bot sounds the alarm when it detects IR interference. 

Your Turn – Test for IR Interference

  • Disconnect your cyber:bot from its programming cable, and walk around the room. Point your cyber:bot at light fixtures, uncovered windows, and electronic devices. Pay extra attention to fluorescent overhead lights where you plan to use your cyber:bot.

  • If you get an alarm under a fluorescent light or near a device, try turning off that light or device and retesting to see if it is the source of the interference.
  • If the IR interference source turns out to be sunlight streaming in through a window, close the blinds and retest, or move your obstacle course to a location that’s well away from the window.

Always use this IR_interference_sniffer project to make sure that any area where you are using the cyber:bot is free of infrared interference.

Adjust the Detection Range

When it is dark outside, you may have noticed that a brighter flashlight lets you see objects that are further away. By making the cyber:bot’s infrared LED headlights brighter, you can also increase its detection range. A smaller resistor allows more current to flow through an LED. More current through an LED is what causes it to glow more brightly. In this activity, you will examine the effect of different resistance values with both the red and infrared LEDs.

Parts List
(2) Resistors, 470 Ω (yellow-violet-brown)
(2) Resistors, 220 Ω (red-red-brown)
(2) Resistors, 1 kΩ (brown-black-red)
(2) Resistors, 4.7 kΩ (yellow-violet-red)
(1) Red LED
(1) Misc. Jumper Wire
Piece of white paper
Ruler

LED Brightness

First, let’s use a red LED to actually see the difference that a resistor makes in how brightly an LED glows. All we need to test the LED circuit is a project that sends a high signal to an LED.

  • Leave all the current IR object detector circuits in place.
  • Put the cyber:bot board’s power switch in position 0.
  • Add a red LED circuit connected to P7.

Example project: P7_led_on

  • Put the cyber:bot board’s power switch in position 1.
  • Enter and flash the project P7_led_on.
  • Run the project and verify that the LED in the circuit connected to P7 emits light.

Try this: Test LED Brightness with Different Resistors

As you work, put the cyber:bot board’s power switch in position 0 for changing resistors, and position 1 for running the test. Also, the same project will run when you reconnect power, so you can pick up right where you left off with each test.

  • Replace the 220 Ω resistor that goes from pin 7 to the LED’s cathode with a 470 Ω resistor. Note now how brightly the LED glows.
  • Repeat with a 1 kΩ resistor. Is the red LED brighter or dimmer?
  • Repeat once more with a 4.7 kΩ resistor. Now how bright, or dim, is the red LED?
  • You can remove the 4.7 kΩ resistor, red LED, and jumper wire before moving on to the next portion of this activity.
  • Explain in your own words the relationship between LED brightness and series resistance.

Series Resistance and Range

We now know that less series resistance will make an LED glow more brightly. A reasonable hypothesis would be that brighter IR LEDs can make it possible to detect objects that are further away.

  • Re-open and flash the project test_both_IR_indicators.

  • Verify that both LED indicator circuits are working properly before continuing.

Resistance vs Distance

You will need two each of the resistor values you used with the red LED test, along with a piece of paper and a ruler. The goal is to measure the object detection distance for different resistor values in series with the IR LEDs. As you work, remember to put the cyber:bot board’s power switch in position 0 for changing resistors, and position 1 for running the test. Remember also that the same project will run when you reconnect power, so you can pick up right where you left off with each test.

  • Place your cyber:bot in an open area, and put the ruler in front of it extending outwards.
  • With the IR LEDs’ 2 kΩ resistors in place on P1 and P14, and the project test_both_IR_indicators running, hold a sheet of white paper in front of the cyber:bot about a foot away.
  • Move the paper slowly towards the robot, and keep an eye on the P20 and P21 amber LEDs. When you see the LEDs go off, the cyber:bot detects the paper. Note that distance on the ruler, and record your data.
  • Replace the IR LEDs’ 2 kΩ resistors on P1 and P14 with 4.7 kΩ resistors.
  • Again, determine the furthest distance at which the same sheet of paper is detected, and record your data.
  • Repeat with 1 kΩ resistors, 470 Ω resistors, and 220 Ω resistors. (For the smaller resistor values, they may end up making the detectors so sensitive that they see the table surface in front of your robot. If that happens, set the robot on the edge of the table with the IR detectors pointing off the end and try the distance measurements again.)
Series Resistance on IR LED Maximum Detection Distance
4700 (yellow-violet-red)  
2000 (red-black-red)  
1000 (brown black-red)  
470 (yellow-violet-brown)  
220 (red-red-brown)  
  • Before moving on to the next activity, restore your IR object detectors to their original circuit with 2 kΩ resistors in series with each IR LED; or whichever resistor value lets your robot “see” objects several inches away, but not see the floor.
  • Also, before moving on, make sure to test this last change with the project test_both_IR_indicators to verify that both IR object detectors are working properly.

Navigate and Avoid Obstacles

An interesting thing about these IR detectors is that their outputs are just like the whiskers. When no object is detected, the output is high; when an object is detected, the output is low. You could modify the project roaming_with_whiskers so that it works with the IR detectors with just a few changes. Here are the steps it takes:

  • Go to the project roaming_with_whiskers and duplicate it or just rename it as roaming_with_IR.
  • Replace the read digital blocks with ir detect blocks:

  • Change all the variables in the if statement from whisker_left and whisker_right to irL and irR.

Example project: roaming_with_ir

  • After modifying your project as described above, double-check that it matches roaming_with_ir below.
  • Flash the project to your micro:bit module.
  • Disconnect the cyber:bot from its programming cable and place it where it can roam and avoid obstacles.
  • Set the power switch to position 2.
  • Watch your cyber:bot roam. It should behave like roaming_with_whiskers, aside from the fact that it doesn’t actually run into objects before changing direction.

Faster IR Navigation

The style of pre-programmed maneuvers from the last activity were fine for whiskers, but are unnecessarily slow when using the IR detectors. With whiskers, the cyber:bot had to make contact and then back up to navigate around obstacles. With infrared, your cyber:bot will detect most obstacles before it runs into them, and can just find a clear path around the obstacle.

Increase the Sampling Rate to Avoid Collisions

As your cyber:bot navigates, it will execute a series of small turns to avoid an obstacle before it ever runs into it. With that approach, the cyber:bot never turns further than it has to, and it can neatly find its way around obstacles and successfully navigate more complex courses. After experimenting with this next project, you’ll likely agree that it’s a much better way for your cyber:bot to roam.

  • Enter and flash the project fast_IR_roaming, then test it with the same obstacles you used with the project roaming_with_IR. Is your cyber:bot roaming more efficiently?

How fast_IR_roaming works better

Though this project has a similar structure to roaming_with_IR, there are three key changes that improved performance:

  1. The pause block arguments have been drastically reduced from 250 ms to just 20 ms. This lets the cyber:bot roam much faster.
  2. The project removes the backwards function when only one IR LED detects something. Since the cyber:bot is no longer physically touching objects to detect them, it does not need to back up before turning.
  3. The project also performs a one-servo-forward pivot turn instead of rotating left and right using both servos.

Drop-off Detector

Up to this point in the tutorial, your cyber:bot has been programmed to look slightly upwards and take evasive maneuvers when an object is detected. The component position and code can also be modified to look downward and take evasive maneuvers when an object is not detected, such as the surface it is driving on. 

This capability would allow your cyber:bot to roam on a table without driving over the edge. However, driving off a table accidentally while experimenting could certainly be harmful to your cyber:bot. So, we will take advantage of a property of infrared light. Infrared light is absorbed by most black surfaces, and reflects off of most light-colored surfaces. By making a wide black border around a white surface, we can simulate a drop-off without putting the cyber:bot in any danger of falling.

Recommended Materials for a Simulated Drop-off
(1) Roll of black vinyl electrical tape, ¾″ (19 mm) wide, or black tempera paint and brush.
(1) Sheet of white poster board, 22 x 28 in (56 x 71 cm).

Re-orient the Object Detectors

Ther are no new components or circuit changes needed for this application, just an adjustment to the position of the existing parts.

  • Put the cyber:bot board’s power switch in position 0.
  • Point your IR object detectors downward and outward as shown below.

Simulate a Drop-off with Poster Board

Simulate a Drop-Off with Poster Board

A sheet of white poster board with a border made of electrical tape or black tempera paint makes for a handy way to simulate the drop-off presented by a table edge, with much less risk to your cyber:bot.

  • Put the cyber:bot board’s power switch in position 1.
  • Run the project IR_interference_sniffer to make sure that nearby fluorescent lighting or other devices will not interfere with your cyber:bot’s IR object detectors.
  • Make a small test patch of your paint or electrical tape on a piece of paper, and make sure that the infrared object detectors cannot see a reflection from it using test_both_IR_indicators, below.
  • Build a course similar to the one shown above. For electrical tape, use at least three strips, edge to edge on the poster board with no paper visible between the strips, and make diagonals over the corners. For paint, make a stripe at least 3 inches wide.
  • Use the project test_both_IR_indicators again to make sure that the cyber:bot detects the poster board but does not detect the electrical tape or paint.

If the cyber:bot still “sees” the electrical tape too clearly, here are a few remedies:

  • Try adjusting the IR detectors and LEDs downward at shallower angles.
  • Try a different brand of vinyl electrical tape.
  • Try replacing the 2 kΩ resistors with 4.7 kΩ (yellow-violet-red) resistors to make the cyber:bot more nearsighted.
  • Adjust the ir detect block with different frequency arguments. These will make the cyber:bot more nearsighted: 39000, 40000, 41000.

If you are using older IR LEDs, the cyber:bot might actually be having problems with being too nearsighted. Here are some remedies that will make it more far sighted:

  • Try replacing the 2 kΩ (red-black-red) resistors with 1 kΩ (red-black-brown) or even 470 Ω (yellow-violet-brown) resistors in series with the IR LEDs instead of 2 kΩ.
  • Try pointing the IR LEDs and detectors downward at a steeper angle so that the cyber:bot is looking at the surface right in front of it.

Avoid Drop-offs

For the most part, programming your cyber:bot to navigate around the virtual drop-off without going over the simulated edge is a matter of adjusting the if statements from the project fast_IR_roaming.

First of all, instead of backing up, it will need to go forward 20 ms at a time when it sees objects with both detectors. 

It will also need to turn toward objects instead of away from them, and it will need to turn for more than 20 ms when it sees the drop-off. 375 ms turns seem to work well, but it will be up to you to adjust that value for best performance.

Example Project: avoid_table_edge

  • Open the project fast_IR_navigation and duplicate it as avoid_table_edge.
  • Disconnect the programming cable, and place the cyber:bot the project on your electrical tape or paint delimited course.
  • Put the power switch in position 2. Can your cyber:bot drive around on the poster board without going over the black “edge?”

How avoid_table_edge Works

The project avoid_table_edge is just the project fast_IR_roaming with a modified if statement in its forever loop.

The condition that used to go forward for 20 ms now backs up for 250 ms. Likewise, the condition that used to back up now goes forward for 20 ms. Also, the condition that used to call for a 20 ms right turn now calls for a 375 ms left turn, and the condition that used to call for a 20 ms left turn now calls for a 375 ms right turn.

Let’s look at the two statements side by side:

In response to if (irL = 0 and irR = 0), the project fast_IR_roaming backs up because both IR detectors see an obstacle. In contrast, the project avoid_table_edge goes forward because both IR detectors see the table, which means it’s safe to move forward for another 20 ms.

In response to else if( irL = 0 and and irR = 1), the project fast_IR_roaming turns right for 20 ms, taking a step toward avoiding an obstacle on the left, while avoid_table_edge turns away from a drop-off that must be on its right. 

Also, irR = 0 and irL = 1, fast_IR_roaming turns left for 20 ms, taking an incremental step toward avoiding an obstacle on the right while avoid_table_edge has the cyber:bot turning right.

Lastly, any other condition has the fast_IR_roaming driving forward while the avoid_table_edge goes backwards away from the table’s edge.

Try This: Adjusting for the Application

The turns to avoid the table edge can be adjusted for different applications. For example, if the cyber:bot is supposed to hug the edge of the table, smaller turns might be useful. In a contest where the cyber:bot is supposed to push objects out of an area, a larger turn (but not too large) would be better so that it zigzags back and forth across the table.

You can modify the code to make shallower turns by using a smaller pause block argument. For example, if you change the 375 in the left function to 300, it will make shallower left turns. If you change it to 450, it will make sharper left turns.

  • Modify the project avoid_table_edge so that it closely follows the edge of the simulated drop-off course. Adjust the pause block arguments in the movement functions to make the cyber:bot execute smaller turns when it sees the drop-off. How small can you make the turn before it tries to fall off?
  • Experiment with pivoting away from the table edge to make the cyber:bot roam inside the perimeter instead of following the edge.

Your Turn, If you Dare

Drive your cyber:bot on an actual tabletop at your own risk!

If you try a tabletop after success with the electrical tape or paint course:

  • Remember to follow the same steps you followed before running the cyber:bot in the electrical tape or paint delimited course!
  • Your cyber:bot may detect you if you are standing in its line of sight. Its current project has no way to differentiate you from the table below it, so it might try to continue forward and off the edge of the table. So, stay out of its detector’s line of sight as you spot.
  •  Make sure to be the spotter for your cyber:bot. It’s best to keep your hand poised so that you are ready to pick it up from above. Be ready as your cyber:bot roams the tabletop:
  • Always be ready to pick your cyber:bot up from above as it approaches the edge of the table it’s navigating. If the cyber:bot tries to drive off the edge, pick it up before it takes the plunge. Otherwise, your cyber:bot might become a Not-Bot!

DISCUSSION FORUMS | PARALLAX INC. STORE

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


Source URL:https://learn.parallax.com/courses/infrared-light-navigation-for-the-cyberbot_makecode/
Links