Tutorial 04 thumbnail

Tutorial 04 — Autonomous Mapping NEW

Ultrasonic scanning + basic grid mapping

Make Your Robot Choose Where To Go

This lesson upgrades your robot from reactive obstacle avoidance to basic autonomous navigation. Your bot sweeps an HC-SR04 ultrasonic sensor on a servo, converts distance readings into a simple map, then chooses the clearest direction.

Safety: HC-SR04 ECHO is 5V. Use a voltage divider / level shifter before Raspberry Pi GPIO.
Outcome: a working scan → decide loop you can plug into your motor drive code from Tutorial 01.
Left / Forward / Right decisions based on the maximum open distance.
Tip: Start with the sensor mounted solidly. A loose ultrasonic/servo mount will give noisy readings.

Parts List

Wiring

Suggested GPIO (BCM)

Voltage divider quick note: ECHO (5V) → 1k (top) → junction → 2k (bottom) → GND. The junction goes to GPIO24.

How The Mapping Loop Works

  1. Servo sweeps angles (example: 30°, 60°, 90°, 120°, 150°).
  2. Ultrasonic returns distances at each angle.
  3. Distances are saved to a simple array (starter model).
  4. Robot picks the direction with the largest open distance.

Next, we’ll expand this into a real occupancy grid and add position tracking.

Python Starter Code

Download mapping.py below and run it on your Pi. It prints sweep distances and the best direction. Then integrate the best direction into your motor control code (Tutorial 01).

mapping.py

Servo sweep + distance read + best direction pick.

Download mapping.py

Printable PDF

Quick reference for parts, wiring, and steps.

Download tutorial04.pdf

All-in-one ZIP

Code + PDF in one file.

Download ZIP

Tutorials in Video Format

These help you visualize servo scanning, mapping basics, and what SLAM is building toward.

1) Servo + Ultrasonic scanning basics

2) Occupancy grid mapping basics

3) SLAM explained (beginner)

Downloads