Project 1.12: "Total Integration N1"
ย
๐ Project 1.12 โ Obstacle Avoidance
๐ฏ What Youโll Learn
- โ Goal 1: Detect obstacles using sensors.
- โ Goal 2: Stop or turn when obstacle detected.
- โ Goal 3: Combine logic for continuous navigation.
Key Ideas
- Digital input: Sensor readings.
- Logic: If/else for obstacle detection.
- Motor control: Forward, stop, turn.
- Loops: Continuous monitoring.
๐งฑ Blocks Glossary (used in this project)
pinX = machine.Pin(X, machine.Pin.OUT)โ Motor direction.pwmX = machine.PWM(machine.Pin(X))โ Motor speed control.pwmX.freq(2000)โ Set PWM frequency.pwmX.duty(value)โ Set duty cycle (0โ1023).sonar.Sonar(trigger, echo).checkdist()โ Distance measurement.while True:โ Infinite loop.time.sleep()โ Delay.
๐งฐ What You Need
| Part | How many? | Pin connection |
|---|---|---|
| D1 R32 | 1 | USB cable |
| L298N Driver | 1 | IN1=Pin 2, IN2=Pin 4, ENA=Pin 5; IN3=Pin 12, IN4=Pin 13, ENB=Pin 14 |
| Motors | 2 | Connected to L298N outputs |
| Ultrasonic Sensor | 1 | Trigger=Pin 26, Echo=Pin 5 |
โ Before You Start
- USB cable connected
- Motors and sensor wired correctly
- Test print shows:
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 1.12.1 โ Read distance
Blocks used: External sensor (sonar), Serial print
Block sequence:
- Setup sonar sensor
- Read distance
- Print value
MicroPython Code:
import machine, time, sonar # Import modules
sensor = sonar.Sonar(26, 5) # Trigger pin 26, Echo pin 5
while True: # Infinite loop
dist = sensor.checkdist() # Read distance
print("Distance:", dist, "cm") # Serial log
time.sleep(0.5) # Delay
Reflection: Sensor reads distance continuously.
Challenge: Try faster refresh (0.2s).
๐ฎ Microproject 1.12.2 โ Stop at obstacle
Blocks used: External sensor, Motor control, Logic
Block sequence:
- Setup motors forward
- If distance < 20 cm โ stop
MicroPython Code:
import machine, time, sonar # Import modules
# Motor A setup
pin2 = machine.Pin(2, machine.Pin.OUT) # Motor A IN1
pin4 = machine.Pin(4, machine.Pin.OUT) # Motor A IN2
pwm5 = machine.PWM(machine.Pin(5)) # Motor A ENA PWM
# Motor B setup
pin12 = machine.Pin(12, machine.Pin.OUT) # Motor B IN3
pin13 = machine.Pin(13, machine.Pin.OUT) # Motor B IN4
pwm14 = machine.PWM(machine.Pin(14)) # Motor B ENB PWM
# Sensor setup
sensor = sonar.Sonar(26, 5) # Trigger pin 26, Echo pin 5
pwm5.freq(2000); pwm14.freq(2000) # Set frequencies
pwm5.duty(512); pwm14.duty(512) # Set speeds
while True: # Infinite loop
dist = sensor.checkdist() # Read distance
print("Distance:", dist, "cm") # Serial log
if dist < 20: # If obstacle closer than 20 cm
pwm5.duty(0); pwm14.duty(0) # Stop motors
print("Obstacle detected: STOP") # Serial log
else: # If safe distance
pin2.value(1); pin4.value(0) # Motor A forward
pin12.value(1); pin13.value(0) # Motor B forward
pwm5.duty(512); pwm14.duty(512) # Motors ON
print("Path clear: GO") # Serial log
time.sleep(0.2) # Delay
Reflection: Robot stops when obstacle detected.
Challenge: Try different threshold (30 cm).
๐ฎ Microproject 1.12.3 โ Turn at obstacle
Blocks used: External sensor, Motor control, Logic
Block sequence:
- Setup motors forward
- If distance < 20 cm โ turn left
MicroPython Code:
import machine, time, sonar # Import modules
# Motor setup
pin2 = machine.Pin(2, machine.Pin.OUT)
pin4 = machine.Pin(4, machine.Pin.OUT)
pwm5 = machine.PWM(machine.Pin(5))
pin12 = machine.Pin(12, machine.Pin.OUT)
pin13 = machine.Pin(13, machine.Pin.OUT)
pwm14 = machine.PWM(machine.Pin(14))
sensor = sonar.Sonar(26, 5) # Sensor setup
pwm5.freq(2000); pwm14.freq(2000)
pwm5.duty(512); pwm14.duty(512)
while True:
dist = sensor.checkdist() # Read distance
print("Distance:", dist, "cm")
if dist < 20: # Obstacle detected
pwm5.duty(0) # Stop motor A
pwm14.duty(512) # Motor B forward
print("Obstacle: TURN LEFT") # Serial log
time.sleep(1) # Turn left
else: # Path clear
pin2.value(1); pin4.value(0)
pin12.value(1); pin13.value(0)
pwm5.duty(512); pwm14.duty(512)
print("Path clear: GO")
time.sleep(0.2)
Reflection: Robot turns left when obstacle detected.
Challenge: Try right turn instead.
๐ฎ Microproject 1.12.4 โ Continuous navigation
Blocks used: External sensor, Motor control, Loop
Block sequence:
- Forward until obstacle
- Turn left
- Continue
MicroPython Code:
import machine, time, sonar # Import modules
pin2 = machine.Pin(2, machine.Pin.OUT)
pin4 = machine.Pin(4, machine.Pin.OUT)
pwm5 = machine.PWM(machine.Pin(5))
pin12 = machine.Pin(12, machine.Pin.OUT)
pin13 = machine.Pin(13, machine.Pin.OUT)
pwm14 = machine.PWM(machine.Pin(14))
sensor = sonar.Sonar(26, 5)
pwm5.freq(2000); pwm14.freq(2000)
while True:
dist = sensor.checkdist()
print("Distance:", dist, "cm")
if dist < 20:
pwm5.duty(0)
pwm14.duty(512)
print("TURN LEFT")
time.sleep(1)
else:
pin2.value(1); pin4.value(0)
pin12.value(1); pin13.value(0)
pwm5.duty(512); pwm14.duty(512)
print("GO")
time.sleep(0.2)
Reflection: Robot navigates continuously avoiding obstacles.
Challenge: Add random turns for variety.
ย
๐ฎ Microproject 1.12.5 โ Smart avoidance
Blocks used: External sensor, Logic, Motor control
Block sequence:
- If distance < 10 cm โ stop
- If distance < 20 cm โ turn left
- Else โ go forward
MicroPython Code:
import machine, time, sonar # Import modules
# Motor A setup
pin2 = machine.Pin(2, machine.Pin.OUT) # Motor A IN1
pin4 = machine.Pin(4, machine.Pin.OUT) # Motor A IN2
pwm5 = machine.PWM(machine.Pin(5)) # Motor A ENA PWM
# Motor B setup
pin12 = machine.Pin(12, machine.Pin.OUT) # Motor B IN3
pin13 = machine.Pin(13, machine.Pin.OUT) # Motor B IN4
pwm14 = machine.PWM(machine.Pin(14)) # Motor B ENB PWM
# Sensor setup
sensor = sonar.Sonar(26, 5) # Trigger pin 26, Echo pin 5
# Configure PWM frequency
pwm5.freq(2000) # Motor A frequency
pwm14.freq(2000) # Motor B frequency
while True: # Infinite loop
dist = sensor.checkdist() # Read distance
print("Distance:", dist, "cm") # Serial log
if dist < 10: # If obstacle very close
pwm5.duty(0) # Stop motor A
pwm14.duty(0) # Stop motor B
print("STOP: Obstacle too close") # Serial log
elif dist < 20: # If obstacle moderately close
pwm5.duty(0) # Stop motor A
pwm14.duty(512) # Motor B forward
print("TURN LEFT: Obstacle detected") # Serial log
time.sleep(1) # Turn left
else: # If path clear
pin2.value(1); pin4.value(0) # Motor A forward
pin12.value(1); pin13.value(0) # Motor B forward
pwm5.duty(512); pwm14.duty(512) # Motors ON
print("GO: Path clear") # Serial log
time.sleep(0.2) # Delay for stability
Reflection: Robot stops if obstacle is too close, turns if moderately close, and moves forward otherwise.
Challenge: Add right turn option for variety.
โจ Main Project โ Obstacle Avoidance
๐ง Blocks Steps (with glossary)
- External sensor: Detect distance.
- Logic: Decide stop, turn, or go.
- Motor control: Execute movement.
- Loop: Continuous navigation.
Block sequence:
- Setup motors and sensor.
- Read distance.
- Stop if too close.
- Turn if moderately close.
- Move forward if clear.
๐ External Explanation
This project shows how robots avoid obstacles using sensors.
- Ultrasonic sensor measures distance.
- Logic decides whether to stop, turn, or go.
- Motors execute the decision.
This is the foundation of autonomous navigation.
โจ Story Time
Imagine your robot as an explorer ๐ค๏ธ. It moves forward until it sees a wall. If the wall is too close, it stops. If itโs near but not too close, it turns left to find a new path. If the way is clear, it continues its journey.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 1.12.A โ Sensor always reads 0
Problem: Distance always 0 cm.
Clues: Echo pin not connected.
Broken code/wiring:
sensor = sonar.Sonar(26, 0) # Wrong echo pin
Fixed code:
sensor = sonar.Sonar(26, 5) # Correct echo pin
Why it works: Correct wiring ensures sensor reads properly.
๐ Debugging 1.12.B โ Robot spins endlessly
Problem: Motors keep turning even when path is clear.
Clues: Duty cycle not reset.
Broken code:
elif dist < 20:
pwm5.duty(0)
pwm14.duty(512)
# Missing reset after turn
Fixed code:
elif dist < 20:
pwm5.duty(0)
pwm14.duty(512)
time.sleep(1)
pwm5.duty(512); pwm14.duty(512) # Reset speeds
Why it works: Resetting duty cycles restores forward motion.
โ Final Checklist
- Sensor reads distance.
- Robot stops at close obstacles.
- Robot turns at moderate obstacles.
- Robot moves forward when clear.
- Continuous navigation works.
๐ Extras
- ๐ง Student tip: Try different thresholds (10 cm, 30 cm).
- ๐งโ๐ซ Instructor tip: Show how sensor readings change with hand movement.
- ๐ Glossary: Sonar, trigger, echo, threshold.
- ๐ก Mini tip: Always test sensor separately before combining with motors.

