ย 

๐Ÿš€ 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

PartHow many?Pin connection
D1 R321USB cable
L298N Driver1IN1=Pin 2, IN2=Pin 4, ENA=Pin 5; IN3=Pin 12, IN4=Pin 13, ENB=Pin 14
TT Motors2Connected to L298N outputs
Wheels2Attached 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:

  1. Motor A forward
  2. Motor B stop
  3. 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:

  1. Forward 2s
  2. Right turn 1s
  3. 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:

  1. Setup pins for both motors.
  2. Drive forward.
  3. Drive backward.
  4. Turn left and right.
  5. 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.
On this page