💡 Level 1 – Fundamentals

Project 1.11: "Painting Robot"

 

🚀 Project 1.11 – Painting Robot


🎯 What You’ll Learn

  • ✅ Goal 1: Move motors to draw straight lines.
  • ✅ Goal 2: Combine turns to draw shapes (square, circle, triangle).
  • ✅ Goal 3: Create free creative patterns.

Key Ideas

  • Digital output: Control motor direction.
  • PWM: Control motor speed.
  • Loops: Repeat movements to form shapes.
  • Timing: Control size of drawings.

🧱 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).
  • time.sleep() → Delay for movement duration.
  • for i in range(...) → Loop for repeated shapes.

🧰 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
Motors2Connected to L298N outputs
Structure1Holds pen/marker

✅ Before You Start

  • USB cable connected
  • Motors wired correctly
  • Pen attached to robot
  • Test print shows:
print("Ready!")  # Confirm serial is working

🎮 Microprojects (5 Mini Missions)


🎮 Microproject 1.11.1 – Draw a straight line

Blocks used: Digital output, PWM, Delay
Block sequence:

  1. Setup motors forward
  2. Run for 3s

MicroPython Code:

import machine, time                          # Import modules

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

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

pwm5.freq(2000)                               # Motor A frequency
pwm14.freq(2000)                              # Motor B frequency
pwm5.duty(512)                                # Motor A speed 50%
pwm14.duty(512)                               # Motor B speed 50%

pin2.value(1)                                 # Motor A forward
pin4.value(0)                                 # Motor A forward
pin12.value(1)                                # Motor B forward
pin13.value(0)                                # Motor B forward
print("Drawing straight line")                # Serial log

time.sleep(3)                                 # Run forward for 3 seconds

Reflection: Robot draws a straight line.
Challenge: Try longer time for longer line.


🎮 Microproject 1.11.2 – Square drawing

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

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

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

pwm5.freq(2000); pwm14.freq(2000)             # Set frequencies
pwm5.duty(512); pwm14.duty(512)               # Set speeds

for i in range(0, 4, 1):                      # Repeat 4 sides
    pin2.value(1); pin4.value(0)              # Motor A forward
    pin12.value(1); pin13.value(0)            # Motor B forward
    print("Forward side", i+1)                # Serial log
    time.sleep(2)                             # Forward 2s

    pwm5.duty(0)                              # Stop motor A
    pwm14.duty(512)                           # Motor B forward
    print("Right turn", i+1)                  # Serial log
    time.sleep(1)                             # Turn 1s

    pwm5.duty(512)                            # Restore motor A speed

Reflection: Robot draws a square.
Challenge: Try bigger square with longer forward time.


🎮 Microproject 1.11.3 – Draw a circle

Blocks used: Digital output, PWM, Delay
Block sequence:

  1. Motor A faster
  2. Motor B slower
  3. Run for 5s

MicroPython Code:

import machine, time                          # Import modules

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

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

pwm5.freq(2000); pwm14.freq(2000)             # Frequencies
pwm5.duty(768)                                # Motor A faster
pwm14.duty(256)                               # Motor B slower

pin2.value(1); pin4.value(0)                  # Motor A forward
pin12.value(1); pin13.value(0)                # Motor B forward
print("Drawing circle")                       # Serial log

time.sleep(5)                                 # Run for 5 seconds

Reflection: Robot draws a circle.
Challenge: Try different speed ratios for bigger/smaller circles.


🎮 Microproject 1.11.4 – Triangle drawing

Blocks used: Digital output, PWM, Loop, Delay
Block sequence:

  1. Forward 2s
  2. Turn 120° (approx 2s)
  3. Repeat 3 times

MicroPython Code:

import machine, time                          # Import modules

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

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

pwm5.freq(2000); pwm14.freq(2000)             # Frequencies
pwm5.duty(512); pwm14.duty(512)               # Speeds

for i in range(0, 3, 1):                      # Repeat 3 sides
    pin2.value(1); pin4.value(0)              # Motor A forward
    pin12.value(1); pin13.value(0)            # Motor B forward
    print("Forward side", i+1)                # Serial log
    time.sleep(2)                             # Forward 2s

    pwm5.duty(0)                              # Stop motor A
    pwm14.duty(512)                           # Motor B forward
    print("Turn 120 degrees", i+1)            # Serial log
    time.sleep(2)                             # Turn approx 120°
    pwm5.duty(512)                            # Restore motor A speed

Reflection: Robot draws a triangle.
Challenge: Try adjusting turn time for sharper angles.


 

🎮 Microproject 1.11.5 – Creative free pattern

Blocks used: Digital output, PWM, Delay
Block sequence:

  1. Mix forward, backward, turns
  2. Create unique path

MicroPython Code:

import machine, time                          # 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

# Configure PWM frequency
pwm5.freq(2000)                               # Motor A frequency
pwm14.freq(2000)                              # Motor B frequency

# Start with medium speed
pwm5.duty(512)                                # Motor A speed 50%
pwm14.duty(512)                               # Motor B speed 50%

# Forward movement
pin2.value(1); pin4.value(0)                  # Motor A forward
pin12.value(1); pin13.value(0)                # Motor B forward
print("Forward 2s")                           # Serial log
time.sleep(2)                                 # Forward 2s

# Backward movement
pin2.value(0); pin4.value(1)                  # Motor A backward
pin12.value(0); pin13.value(1)                # Motor B backward
print("Backward 2s")                          # Serial log
time.sleep(2)                                 # Backward 2s

# Left turn
pwm5.duty(0)                                  # Stop motor A
pwm14.duty(512)                               # Motor B forward
print("Left turn 1s")                         # Serial log
time.sleep(1)                                 # Turn left

# Right turn
pwm5.duty(512)                                # Motor A forward
pwm14.duty(0)                                 # Stop motor B
print("Right turn 1s")                        # Serial log
time.sleep(1)                                 # Turn right

# Stop motors
pwm5.duty(0)                                  # Stop motor A
pwm14.duty(0)                                 # Stop motor B
print("Pattern complete")                     # Serial log

Reflection: Robot creates a free pattern with mixed moves.
Challenge: Add more turns and speeds for creative paths.


✨ Main Project – Painting Robot

🔧 Blocks Steps (with glossary)

  • Digital output: Motor direction.
  • PWM: Motor speed.
  • Loops: Repeat shapes.
  • Timing: Control size of drawings.

Block sequence:

  1. Setup motors.
  2. Draw straight line.
  3. Draw square.
  4. Draw circle.
  5. Draw triangle.
  6. Create free pattern.

📖 External Explanation

This project shows how to choreograph robot movements to draw shapes.

  • Straight line teaches basic forward motion.
  • Square and triangle use loops and turns.
  • Circle uses speed differences.
  • Free pattern encourages creativity.

✨ Story Time

Imagine your robot as an artist 🎨. With each movement, it sketches lines, shapes, and curves. By combining speed and direction, your robot becomes a painter, creating unique patterns on the floor or paper.


🕵️ Debugging (2 Common Problems)

🐞 Debugging 1.11.A – Shapes distorted

Problem: Square sides uneven.
Clues: Forward time too short/long.
Broken code:

time.sleep(1)   # Forward too short

Fixed code:

time.sleep(2)   # Correct forward time

Why it works: Adjusting time ensures equal sides.


🐞 Debugging 1.11.B – Circle not smooth

Problem: Robot moves in jagged path.
Clues: Speed ratio not balanced.
Broken code:

pwm5.duty(1023)   # Too fast
pwm14.duty(100)   # Too slow

Fixed code:

pwm5.duty(768)    # Balanced speed
pwm14.duty(256)   # Balanced speed

Why it works: Balanced duty cycles create smooth circle.


✅ Final Checklist

  • Straight line works.
  • Square drawing works.
  • Circle drawing works.
  • Triangle drawing works.
  • Free pattern created.

📚 Extras

  • 🧠 Student tip: Try combining shapes into a “robot signature.”
  • 🧑‍🏫 Instructor tip: Show how timing changes shape size.
  • 📖 Glossary: Duty cycle, forward, backward, turn.
  • 💡 Mini tip: Always test one shape before combining patterns.
On this page