Note

Hello, welcome to the SunFounder Raspberry Pi & Arduino & ESP32 Enthusiasts Community on Facebook! Dive deeper into Raspberry Pi, Arduino, and ESP32 with fellow enthusiasts.

Why Join?

  • Expert Support: Solve post-sale issues and technical challenges with help from our community and team.

  • Learn & Share: Exchange tips and tutorials to enhance your skills.

  • Exclusive Previews: Get early access to new product announcements and sneak peeks.

  • Special Discounts: Enjoy exclusive discounts on our newest products.

  • Festive Promotions and Giveaways: Take part in giveaways and holiday promotions.

👉 Ready to explore and create with us? Click [here] and join today!

Lesson 6 IR Obstacle

Meet your rover’s side “eyes” - the Infrared Obstacle Avoidance sensors!

These clever sensors help your GalaxyRVR detect and dodge obstacles on its sides, just like having peripheral vision. Learn how they work and program your rover to navigate around objects automatically.

Get ready to make your Mars Rover a smart obstacle-avoider!

Learning Objectives

  • Understand the working principles of the Infrared Obstacle Avoidance Module and its application in the Mars rover.

  • Learn how to read and apply data from the Infrared Obstacle Avoidance Module in Scratch.

  • Create a Mars exploration-themed obstacle avoidance game using the IR module and the Scratch stage.

Meet the Obstacle Avoidance Module

Say hello to your GalaxyRVR’s new sidekick - the Infrared Obstacle Avoidance Module! This clever little device helps your rover detect and avoid obstacles. Let’s see what makes it tick:

../_images/ir_avoid2.png

Meet the Four Important Pins:

  • GND - The ground connection (completes the circuit)

  • + - Power input (needs 3.3V to 5V electricity)

  • Out - Signal output (sends “obstacle detected” messages)

  • EN - Enable pin (controls when the module is active)

How It Works - The Invisible Flashlight:

Think of this module as having an invisible flashlight and special glasses:

../_images/ir_receive2.png
  1. The transmitter sends out infrared light (invisible to our eyes)

  2. When the light hits an obstacle, it bounces back

  3. The receiver “sees” the reflected light

  4. The module sends a signal: “Obstacle ahead!”

Fun Facts About Your Sensor:

  • Detection Range: 2-40cm (about the length of your pencil case!)

  • Color Matters: Works best with light-colored objects

  • Dark Objects: Harder to detect from far away

Advanced Controls (For Curious Minds):

  • EN Pin: The jumper cap keeps the module always active. Remove it if you want to control the module with code.

    ../_images/ir_cap2.png
  • Two Adjustments:

    • One dial controls how far the infrared light travels

    • One dial adjusts the light frequency

    ../_images/ir_avoid_pot2.png

Now that you’ve met your new sidekick, let’s connect it to your rover and start programming!

Testing Your Infrared Sensors

  1. First, Connecting the APP to GalaxyRVR.

  1. Let’s tune your sensors for perfect performance!

    1. Make sure the infrared components are straight. Gently adjust if needed.

    2. Place an object (like your rover’s box) 20cm away. Turn the dial until the indicator light turns on. Test by moving the object closer and farther.

    3. Do the same for the other infrared module.

  2. Then, Connecting the APP to GalaxyRVR.

  3. Find the “left IR status” and “right IR status” blocks in the GalaxyRVR category and check their boxes.

    ../_images/4_ir_statusblock.png
  4. The sensor values will now show on your stage.

    ../_images/4_ir_statusvalue.png
  5. Wave your hand near each infrared sensor and watch the values change!

What the Values Mean:

  • True = Obstacle detected

  • False = Clear path

Now you’re ready to see what your rover can “see”!

Programming Your Rover to Avoid Obstacles

Let’s teach your GalaxyRVR to automatically dodge obstacles using its infrared sensors!

  1. First, Connecting the APP to GalaxyRVR.

  2. Start the program with a green flag block.

    ../_images/4_ir_start.png
  3. Set a safe speed of 30% for easy testing and debugging.

    ../_images/4_ir_speed.png
  4. Find the when left IR is blocked block for left sensor detection.

    ../_images/4_ir_when_blocked.png
  5. When the left sensor detects an obstacle, make the rover turn right.

    ../_images/4_ir_turn_right.png
  6. Keep turning right until the left side no longer detects the obstacle.

    ../_images/4_ir_wait_until.png
  7. Stop moving once the path is clear.

    ../_images/4_ir_stop.png
  8. Test by triggering the left infrared sensor with your hand. The GalaxyRVR should smartly turn right to avoid it.

  9. Duplicate the code by long-pressing the blocks and selecting duplicate from the menu.

    ../_images/4_ir_duplicate.png
  10. In the duplicated code, swap the left and right sides so it handles right-side obstacles.

    ../_images/4_ir_left_right.png
  11. To prevent erratic behavior when both sensors are triggered at once, add a “stop other scripts in sprite” block. This ensures only one sensor event is handled at a time.

    ../_images/4_ir_stop_script.png

    Note

    The stop scripts block can conflict with timed movement blocks, so avoid using them together when possible.

  12. Now your GalaxyRVR will turn left or right when obstacles are detected on either side. Test by triggering both sensors with your hands.

  13. Add a forward block under each code section so the rover continues moving after avoiding obstacles.

    ../_images/4_ir_avoid_move.png

Now click the green flag! Your GalaxyRVR will move forward continuously, smartly dodging obstacles and resuming its path after avoiding them.