Project 1.4: "DC Motor Control"
🚀 Project 1.4 – DC Motor Control
🎯 What You’ll Learn
- ✅ Goal 1: Rotate a DC motor in both directions.
- ✅ Goal 2: Control motor speed with PWM.
- ✅ Goal 3: Drive two motors together for coordinated movement.
Key Ideas
- Digital output: Control motor direction pins.
- PWM: Adjust motor speed.
- Loop: Repeat coordinated movement.
🧱 Blocks Glossary (used in this project)
- Digital output: Set motor direction pins HIGH/LOW.
- PWM: Control motor speed by duty cycle.
- Loop (while True / for): Repeat actions.
🧰 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 |
| TT Motor | 2 | Connected to L298N outputs |
| Wheels | 2 | Attached to motors |
🔌 Wiring tip: Connect motor driver inputs to pins 2, 4, 5. Motors to L298N outputs.
📍 Pin map snapshot: Pin 2 = IN1, Pin 4 = IN2, Pin 5 = ENA (PWM).
✅ Before You Start
- USB cable plugged in
- Motors wired correctly to L298N
- Test print shows:
print("Ready!") # Confirm serial is working
🎮 Microprojects (5 Mini Missions)
🎮 Microproject 1.4.1 – The motor rotates in one direction
Goal: Spin motor clockwise.
Blocks used: Digital output
Block sequence:
- IN1 HIGH, IN2 LOW → Motor forward
MicroPython Code:
import machine, time # Importa librerías
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 salida
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 salida
pin2.value(1) # IN1 HIGH
print("IN1 HIGH") # Serial: IN1 encendido
pin4.value(0) # IN2 LOW
print("IN2 LOW") # Serial: IN2 apagado
time.sleep(3) # Motor gira 3 segundos
🎮 Microproject 1.4.2 – The motor rotates in the opposite direction
Goal: Spin motor counterclockwise.
Blocks used: Digital output
Block sequence:
- IN1 LOW, IN2 HIGH → Motor reverse
MicroPython Code:
import machine, time
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 salida
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 salida
pin2.value(0) # IN1 LOW
print("IN1 LOW") # Serial: IN1 apagado
pin4.value(1) # IN2 HIGH
print("IN2 HIGH") # Serial: IN2 encendido
time.sleep(3) # Motor gira 3 segundos en reversa
🎮 Microproject 1.4.3 – Speed control with PWM
Goal: Control motor speed.
Blocks used: PWM, Digital output
Block sequence:
- Setup ENA pin as PWM
- Adjust duty cycle for speed
MicroPython Code:
import machine, time
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 salida
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 salida
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM en pin 5
pin2.value(1); pin4.value(0) # Dirección: adelante
print("Direction: Forward") # Serial: dirección adelante
pwm5.freq(2000) # Frecuencia PWM
print("PWM frequency set to 2000Hz") # Serial: frecuencia configurada
pwm5.duty(512) # Velocidad media (duty 50%)
print("Motor speed: 50% duty") # Serial: velocidad media
time.sleep(3) # Motor corre 3 segundos
🎮 Microproject 1.4.4 – Two simultaneous engines
Goal: Run two motors at once.
Blocks used: Digital output, PWM
Block sequence:
- Setup IN1/IN2 for motor A, IN3/IN4 for motor B
- Both forward
MicroPython Code:
import machine, time
# Motor A
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 salida
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 salida
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM
# Motor B
pin12 = machine.Pin(12, machine.Pin.OUT) # IN3 salida
pin13 = machine.Pin(13, machine.Pin.OUT) # IN4 salida
pwm14 = machine.PWM(machine.Pin(14)) # ENB PWM
pin2.value(1); pin4.value(0) # Motor A adelante
print("Motor A forward")
pin12.value(1); pin13.value(0) # Motor B adelante
print("Motor B forward")
pwm5.freq(2000); pwm5.duty(512) # Motor A velocidad media
print("Motor A speed 50%")
pwm14.freq(2000); pwm14.duty(512) # Motor B velocidad media
print("Motor B speed 50%")
time.sleep(3) # Ambos motores giran 3 segundos
🎮 Microproject 1.4.5 – Coordinated forward/backward movement
Goal: Drive both motors forward then backward.
Blocks used: Digital output, PWM, Loop
Block sequence:
- Both forward → Delay 3s
- Both backward → Delay 3s
- Repeat
MicroPython Code:
import machine, time
# Motor A
pin2 = machine.Pin(2, machine.Pin.OUT)
pin4 = machine.Pin(4, machine.Pin.OUT)
pwm5 = machine.PWM(machine.Pin(5))
# Motor B
pin12 = machine.Pin(12, machine.Pin.OUT)
pin13 = machine.Pin(13, machine.Pin.OUT)
pwm14 = machine.PWM(machine.Pin(14))
pwm5.freq(2000); pwm14.freq(2000) # Configura frecuencia PWM
pwm5.duty(512); pwm14.duty(512) # Velocidad media
while True: # Bucle infinito
pin2.value(1); pin4.value(0) # Motor A adelante
pin12.value(1); pin13.value(0) # Motor B adelante
print("Both motors forward") # Serial: ambos adelante
time.sleep(3)
pin2.value(0); pin4.value(1) # Motor A atrás
pin12.value(0); pin13.value(1) # Motor B atrás
print("Both motors backward") # Serial: ambos atrás
time.sleep(3)
✨ Main Project – DC Motor Control
🔧 Blocks Steps (with glossary)
- Digital output: Control motor direction pins (IN1, IN2, IN3, IN4).
- PWM: Control motor speed (ENA, ENB).
- Loop: Repeat coordinated forward/backward movement.
Block sequence:
- Setup pins for motor driver (IN1, IN2, ENA, IN3, IN4, ENB).
- Run forward sequence.
- Run backward sequence.
- Repeat forever.
🐍 MicroPython Code (mirroring blocks)
# Project 1.4 – DC Motor Control
import machine, time # Importa librerías para pines y tiempo
# Motor A
pin2 = machine.Pin(2, machine.Pin.OUT) # IN1 salida
pin4 = machine.Pin(4, machine.Pin.OUT) # IN2 salida
pwm5 = machine.PWM(machine.Pin(5)) # ENA PWM en pin 5
# Motor B
pin12 = machine.Pin(12, machine.Pin.OUT) # IN3 salida
pin13 = machine.Pin(13, machine.Pin.OUT) # IN4 salida
pwm14 = machine.PWM(machine.Pin(14)) # ENB PWM en pin 14
# Configuración inicial de PWM
pwm5.freq(2000) # Frecuencia PWM motor A
print("Motor A PWM frequency set to 2000Hz") # Serial: frecuencia motor A
pwm14.freq(2000) # Frecuencia PWM motor B
print("Motor B PWM frequency set to 2000Hz") # Serial: frecuencia motor B
pwm5.duty(512) # Duty 50% motor A
print("Motor A speed set to 50% duty") # Serial: velocidad motor A
pwm14.duty(512) # Duty 50% motor B
print("Motor B speed set to 50% duty") # Serial: velocidad motor B
while True: # Bucle infinito
# Ambos motores adelante
pin2.value(1); pin4.value(0) # Motor A adelante
pin12.value(1); pin13.value(0) # Motor B adelante
print("Both motors forward") # Serial: ambos adelante
time.sleep(3) # Espera 3 segundos
# Ambos motores atrás
pin2.value(0); pin4.value(1) # Motor A atrás
pin12.value(0); pin13.value(1) # Motor B atrás
print("Both motors backward") # Serial: ambos atrás
time.sleep(3) # Espera 3 segundos
📖 External Explanation
- What it teaches: How to control DC motors with direction and speed.
- Why it works: Digital outputs set motor direction, PWM adjusts speed, and loops repeat actions.
- Key concept: Direction + speed + repetition = motor control system.
✨ Story Time
Imagine your robot is a small car. With these blocks, you can make it drive forward, reverse, and even adjust its speed like a real vehicle.
🕵️ Debugging (2 Common Problems)
🐞 Debugging 1.4.A – Motor does not rotate (reverse polarity)
Problem: Motor stays still.
Clues: Wrong wiring polarity.
Broken code/wiring:
pin2.value(0); pin4.value(0) # Ambos LOW, motor no recibe dirección
Fixed code:
pin2.value(1); pin4.value(0) # Dirección correcta: motor adelante
Why it works: One pin HIGH and the other LOW sets rotation.
Avoid next time: Always check polarity and wiring.
🐞 Debugging 1.4.B – Inconsistent speed
Problem: Motor speed changes unexpectedly.
Clues: PWM duty not set or too low.
Broken code:
pwm5.duty(10) # Duty demasiado bajo, motor apenas gira
Fixed code:
pwm5.duty(512) # Duty medio, motor estable
Why it works: Proper duty cycle ensures consistent speed.
Avoid next time: Use values between 256–768 for stable speeds.
✅ Final Checklist
- Motor rotated forward and backward.
- Speed controlled with PWM.
- Two motors ran simultaneously.
- Coordinated forward/backward movement worked.
📚 Extras
- 🧠 Student tip: Try different duty cycles to simulate acceleration.
- 🧑🏫 Instructor tip: Verify motor driver wiring before running.
- 📖 Glossary: Digital output, PWM, Duty cycle.
- 💡 Mini tips: Always test one motor before connecting two.