Project 1.6: "Basic Car"
ย
๐ Project 1.6 โ Basic Car
๐ฏ What Youโll Learn
- โ Goal 1: Drive motors forward and backward.
- โ Goal 2: Perform left and right turns.
- โ Goal 3: Combine movements into a square pattern.
Key Ideas
- Digital output: Controls motor direction pins.
- PWM: Controls motor speed.
- Loop: Repeats movement patterns.
๐งฑ Blocks Glossary (used in this project)
- machine.Pin: Sets motor driver pins HIGH/LOW.
- machine.PWM: Controls motor speed.
- while True: Infinite loop for continuous movement.
- time.sleep(): Sets duration of each movement.
๐งฐ 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 |
| TT Motors | 2 | Connected to L298N outputs |
| Wheels | 2 | Attached to motors |
๐ Wiring tip: Connect motor driver inputs to pins 2, 4, 5 (Motor A) and 12, 13, 14 (Motor B).
๐ Pin map snapshot: Pin 2/4/5 = Motor A, Pin 12/13/14 = Motor B.
โ Before You Start
- USB cable connected
- Motors wired correctly to L298N
- Test print shows:
print("Ready!") # Confirm serial is working
๐ฎ Microprojects (5 Mini Missions)
๐ฎ Microproject 1.6.4 โ Right Turn
Blocks used: Digital output, PWM, Delay
Block sequence:
- Motor A forward
- Motor B stop
- Run for 2s
MicroPython Code:
import machine, time # Import modules
# Motor A setup
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 output (A)
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 output (A)
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM (A)
# Motor B setup
pin12 = machine.Pin(12, machine.Pin.OUT) # IN3 output (B)
pin13 = machine.Pin(13, machine.Pin.OUT) # IN4 output (B)
pwm14 = machine.PWM(machine.Pin(14)) # ENB PWM (B)
pwm5.freq(2000) # Set PWM frequency A
print("Motor A PWM freq: 2000 Hz") # Log A frequency
pwm5.duty(512) # Run motor A at ~50%
print("Motor A speed: 50% duty") # Log A speed
pwm14.freq(2000) # Set PWM frequency B
print("Motor B PWM freq: 2000 Hz") # Log B frequency
pwm14.duty(0) # Stop motor B (0% duty)
print("Motor B: stop") # Log B stop
pin2.value(1) # Set IN1 HIGH (A forward)
pin4.value(0) # Set IN2 LOW (A forward)
print("Motor A: forward (right turn)") # Log turning action
time.sleep(2) # Turn for 2 seconds
Reflection: The car turns right by stopping motor B.
Challenge: Try increasing motor A speed to 75% duty.
๐ฎ Microproject 1.6.5 โ Square Pattern
Blocks used: Digital output, PWM, Loop, Delay
Block sequence:
- Forward 2s
- Right turn 1s
- Repeat 4 times
MicroPython Code:
import machine, time # Import modules
# Motor A setup
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 output (A)
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 output (A)
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM (A)
# Motor B setup
pin12 = machine.Pin(12, machine.Pin.OUT) # IN3 output (B)
pin13 = machine.Pin(13, machine.Pin.OUT) # IN4 output (B)
pwm14 = machine.PWM(machine.Pin(14)) # ENB PWM (B)
pwm5.freq(2000) # Set PWM frequency A
print("Motor A PWM freq: 2000 Hz") # Log A frequency
pwm14.freq(2000) # Set PWM frequency B
print("Motor B PWM freq: 2000 Hz") # Log B frequency
pwm5.duty(512) # Set A speed to ~50%
print("Motor A speed: 50% duty") # Log A speed
pwm14.duty(512) # Set B speed to ~50%
print("Motor B speed: 50% duty") # Log B speed
for i in range(0, 4, 1): # Repeat 4 sides of the square
print("Square side:", i + 1) # Log current side
pin2.value(1) # IN1 HIGH (A forward)
pin4.value(0) # IN2 LOW (A forward)
pin12.value(1) # IN3 HIGH (B forward)
pin13.value(0) # IN4 LOW (B forward)
print("Move: forward") # Log forward move
time.sleep(2) # Drive forward for 2 seconds
pwm5.duty(0) # Stop motor A (prepare right turn)
print("Motor A: stop for turn") # Log stopping A
pwm14.duty(512) # Keep motor B running at ~50%
print("Motor B: keep running") # Log B running
pin12.value(1) # Ensure IN3 HIGH (B forward)
pin13.value(0) # Ensure IN4 LOW (B forward)
print("Move: right turn") # Log right turn
time.sleep(1) # Turn for 1 second
pwm5.duty(512) # Restore motor A speed to ~50%
print("Motor A: restore speed") # Log restoring A
Reflection: The car drives in a square path.
Challenge: Try making a triangle by repeating 3 times instead of 4.
Perfect ๐ค, letโs finish Project 1.6 โ Basic Car exactly where we left off (Main Project code), and complete the CluโBots Tutorial Template with all required sections: External Explanation, Story Time, Debugging, Final Checklist, and Extras. Every line of code will have a comment.
โจ Main Project โ Basic Car
๐ง Blocks Steps (with glossary)
- Digital output: Control motor direction pins.
- PWM: Control motor speed.
- Loop: Repeat square pattern.
Block sequence:
- Setup pins for both motors.
- Drive forward.
- Drive backward.
- Turn left and right.
- Combine into square pattern.
๐ MicroPython Code (mirroring blocks)
# Project 1.6 โ Basic Car
import machine, time # Import required modules
# Motor A setup
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 output for motor A
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 output for motor A
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM for motor A
# Motor B setup
pin12 = machine.Pin(12, machine.Pin.OUT) # IN3 output for motor B
pin13 = machine.Pin(13, machine.Pin.OUT) # IN4 output for motor B
pwm14 = machine.PWM(machine.Pin(14)) # ENB PWM for motor B
# Configure PWM frequency
pwm5.freq(2000) # Set PWM frequency for motor A to 2 kHz
print("Motor A PWM freq: 2000 Hz") # Serial log for motor A frequency
pwm14.freq(2000) # Set PWM frequency for motor B to 2 kHz
print("Motor B PWM freq: 2000 Hz") # Serial log for motor B frequency
# Set initial speeds
pwm5.duty(512) # Motor A speed at ~50% duty
print("Motor A speed: 50% duty") # Serial log for motor A speed
pwm14.duty(512) # Motor B speed at ~50% duty
print("Motor B speed: 50% duty") # Serial log for motor B speed
# Forward movement
pin2.value(1) # IN1 HIGH (motor A forward)
pin4.value(0) # IN2 LOW (motor A forward)
pin12.value(1) # IN3 HIGH (motor B forward)
pin13.value(0) # IN4 LOW (motor B forward)
print("Main: forward 2s") # Serial log for forward movement
time.sleep(2) # Drive forward for 2 seconds
# Backward movement
pin2.value(0) # IN1 LOW (motor A backward)
pin4.value(1) # IN2 HIGH (motor A backward)
pin12.value(0) # IN3 LOW (motor B backward)
pin13.value(1) # IN4 HIGH (motor B backward)
print("Main: backward 2s") # Serial log for backward movement
time.sleep(2) # Drive backward for 2 seconds
# Left turn
pwm5.duty(0) # Stop motor A
print("Main: stop A (left turn)") # Serial log for stopping A
pwm14.duty(512) # Keep motor B running
print("Main: run B (left turn)") # Serial log for running B
pin12.value(1) # IN3 HIGH (motor B forward)
pin13.value(0) # IN4 LOW (motor B forward)
time.sleep(1) # Turn left for 1 second
# Right turn
pwm5.duty(512) # Restore motor A speed
print("Main: restore A (right turn)") # Serial log for restoring A
pwm14.duty(0) # Stop motor B
print("Main: stop B (right turn)") # Serial log for stopping B
pin2.value(1) # IN1 HIGH (motor A forward)
pin4.value(0) # IN2 LOW (motor A forward)
time.sleep(1) # Turn right for 1 second
๐ External Explanation
This project teaches how to control a twoโmotor car using direction pins and PWM.
- Direction pins decide forward or backward.
- PWM duty cycle sets speed.
- Delays define how long each action lasts.
Together, these create smooth and predictable car movements.
โจ Story Time
Imagine your robot as a tiny driver ๐. It accelerates forward, reverses carefully, and makes turns like a real car. By combining these moves, you can choreograph shapes like squares or triangles, turning your robot into a performer on the floor.
๐ต๏ธ Debugging (2 Common Problems)
๐ Debugging 1.6.A โ Incorrect turns
Problem: The car turns the wrong way.
Clues: Motors wired to opposite sides.
Broken code/wiring:
pin2.value(1); pin4.value(0) # Motor A forward
pin12.value(0); pin13.value(1) # Motor B backward (wrong side)
Fixed code:
pin12.value(1); pin13.value(0) # Motor B forward (correct side)
Why it works: Both motors must coordinate direction.
Avoid next time: Label motor A and B clearly during wiring.
๐ Debugging 1.6.B โ Uncoordinated movement
Problem: Car jerks or moves unevenly.
Clues: PWM duty mismatch.
Broken code:
pwm5.duty(200) # Motor A slow
pwm14.duty(800) # Motor B fast
Fixed code:
pwm5.duty(512) # Motor A medium
pwm14.duty(512) # Motor B medium
Why it works: Matching duty cycles ensures smooth motion.
Avoid next time: Always set equal duty for both motors unless intentionally steering.
โ Final Checklist
- Forward movement works.
- Backward movement works.
- Left turn works.
- Right turn works.
- Square pattern completed.
๐ Extras
- ๐ง Student tip: Try different duty cycles to simulate acceleration.
- ๐งโ๐ซ Instructor tip: Mark motor sides to avoid confusion.
- ๐ Glossary: PWM, duty cycle, forward/backward, turn.
- ๐ก Mini tip: Test each motor separately before combining moves.