← Back to Missions
MEDIUM • 40 MINUTES

Mission 5: Obstacle Avoider 🚧

Navigate around objects like a self-driving car!

🚧

🎯 What You'll Learn

📏 Ultrasonic Sensors

How sound waves measure distance (like bat echolocation!)

🧠 Decision Making

If obstacle detected, then turn away logic

🤖 Autonomous Navigation

Robot explores on its own without crashing!

⚡ Real-Time Response

Processing sensor data and reacting instantly

📦 What You Need

📍 Note: The Maqueen Plus V2 has an ultrasonic sensor mounted on the front (looks like two silver "eyes"). We'll use this to detect walls and objects!

🛠️ Let's Build an Explorer!

1

Understand Ultrasonic Sensors

Look at the front of your Maqueen—see those two silver cylinders? One is a transmitter (sends sound waves), the other is a receiver (listens for echoes). Just like a bat!

How It Works:

  1. 1. Sensor sends ultrasonic "ping" (40,000 Hz—too high for humans to hear)
  2. 2. Sound wave bounces off an object and returns
  3. 3. Sensor measures how long the echo took
  4. 4. Time × speed of sound = distance!
🦇 Real-World Connection: Bats navigate in darkness using echolocation. Submarines use sonar. Self-driving cars use similar sensors (LIDAR). Your robot is using the same physics!
2

Test the Distance Sensor

Let's see what distances the sensor reports! In MakeCode:

Forever:
  Set [distance] to [Ultrasonic sensor (cm)]
  Show number [distance]
  Pause 500ms

Upload the code. The LED matrix will show the distance in centimeters to the nearest object!

🧪 Experiment: Hold your hand in front of the sensor at different distances. Watch the number change! This is real-time sensor data!
3

Simple Obstacle Detection

Now let's make the robot stop when it gets too close to something:

Forever:
  Set [distance] to [Ultrasonic sensor (cm)]

  If [distance] > 20:
    // Clear path - drive forward
    Set Motors: Left=100, Right=100

  Else:
    // Too close - STOP!
    Set Motors: Left=0, Right=0
    Play tone 200Hz for 200ms
🎯 The Logic: If more than 20cm away, drive. If closer than 20cm, stop and beep!
4

Add Turn-Away Behavior

Stopping is good, but let's make it AVOID the obstacle by turning!

Forever:
  Set [distance] to [Ultrasonic sensor (cm)]

  If [distance] > 20:
    // Drive forward
    Set Motors: Left=100, Right=100

  Else:
    // Obstacle detected!
    Set Motors: Left=0, Right=0
    Pause 200ms
    // Back up a bit
    Set Motors: Left=-80, Right=-80
    Pause 500ms
    // Turn right
    Set Motors: Left=100, Right=-100
    Pause 600ms
🔄 The Strategy: Stop → Back up → Turn → Continue exploring!
5

Add Random Turns (Smarter Navigation)

Always turning the same direction can get the robot stuck in corners. Let's make it randomly choose left or right!

When obstacle detected:
  Set Motors: Left=0, Right=0
  Set [direction] to [pick random 0 to 1]
  Set Motors: Left=-80, Right=-80 (back up)
  Pause 500ms

  If [direction] = 0:
    Set Motors: Left=100, Right=-100 (turn right)
  Else:
    Set Motors: Left=-100, Right=100 (turn left)
  Pause 600ms
🎲 Randomness = Intelligence! The robot now explores unpredictably, just like a Roomba vacuum!
6

Create an Obstacle Course!

Set up books, boxes, and toys in a room. Put the robot down and watch it explore autonomously!

Challenge Ideas:

  • Maze Navigator: Build walls with books—can it find the exit?
  • Room Mapper: Let it explore for 2 minutes—does it cover the whole space?
  • Speed Tuning: Make it go faster but still avoid obstacles
  • Sound Effects: Different beep for close vs far obstacles
  • LED Indicators: Green = safe, Red = obstacle detected

🔬 What's Happening Behind the Scenes?

The ultrasonic sensor sends 40kHz pulses (ultrasound—above human hearing). When the pulse hits an object, it reflects back. The sensor measures the round-trip time and calculates:

Distance = (Time × Speed of Sound) ÷ 2

Speed of sound is ~343 meters/second. The "÷2" is because the sound travels TO the object and BACK.

Real-World Applications: This technology is everywhere! Parking sensors in cars, warehouse robots, drones, medical ultrasound imaging, and even measuring liquid levels in tanks! 🚗🏥

🔧 Troubleshooting Tips

Problem: Sensor reads "0" or crazy numbers

Check that nothing is blocking the sensors. Soft materials (fabric, foam) can absorb sound waves. Try detecting hard, flat surfaces like walls or cardboard boxes.

Problem: Robot still crashes sometimes

Increase the detection distance from 20cm to 30cm. Or slow down the forward speed so it has more time to react.

Problem: Gets stuck in corners

Increase turn time (try 800ms instead of 600ms) or add a second random turn if it still detects an obstacle after the first turn.

Mission Success Checklist

🚀 Ready for AI Vision?

Incredible work! The robot now has spatial awareness and can navigate autonomously. The boys learned about echolocation, real-time decision making, and autonomous systems—the foundation of self-driving cars and warehouse robots!

Next up: We're adding EYES to the robot! The HuskyLens AI camera will let it see and recognize objects. This is where things get REALLY exciting! 👁️

Next Mission: First Look (HuskyLens Setup) 👁️ →
← Back to All Missions