11

Lesson 11: Motors and Movement Control

Making robots move with precision

Learning Objectives

By the end of this lesson, students will:

  • Control DC motors and servos
  • Understand PWM (Pulse Width Modulation)
  • Implement speed and direction control
  • Create basic movement patterns

Skills Developed:

  • Motor control programming
  • PWM signal generation
  • Robotics kinematics basics

Lesson Content

1 Motor Types and Applications (20 minutes)

Motors in miniAuto Robot:

DC Motors:
  • • Drive wheels for movement
  • • Variable speed control
  • • Forward/reverse direction
  • • Need motor driver (H-bridge)
Servo Motors:
  • • Precise position control
  • • Camera pan/tilt
  • • Robotic arm joints
  • • Built-in control circuit
// Basic motor control setup
#include <Servo.h>

// DC Motor pins (using L298N driver)
#define MOTOR_A_PIN1 5
#define MOTOR_A_PIN2 6
#define MOTOR_B_PIN1 9
#define MOTOR_B_PIN2 10

// Servo motor
Servo cameraServo;
#define SERVO_PIN 3

void setup() {
    Serial.begin(9600);
    // Initialize motor pins
    pinMode(MOTOR_A_PIN1, OUTPUT);
    pinMode(MOTOR_A_PIN2, OUTPUT);
    pinMode(MOTOR_B_PIN1, OUTPUT);
    pinMode(MOTOR_B_PIN2, OUTPUT);
    // Initialize servo
    cameraServo.attach(SERVO_PIN);
    cameraServo.write(90); // Center position
    Serial.println("🤖 Motor control system ready!");
}

2 PWM and Speed Control (20 minutes)

PWM (Pulse Width Modulation):

  • • Digital way to create "analog" output
  • • Rapidly switches between HIGH and LOW
  • • Duty cycle determines average voltage
  • • analogWrite() generates PWM (0-255)
// PWM motor speed control
void setMotorSpeed(int motorA_speed, int motorB_speed) {
    // Motor A control
    if (motorA_speed > 0) {
        // Forward direction
        analogWrite(MOTOR_A_PIN1, motorA_speed);
        analogWrite(MOTOR_A_PIN2, 0);
    } else if (motorA_speed < 0) {
        // Reverse direction
        analogWrite(MOTOR_A_PIN1, 0);
        analogWrite(MOTOR_A_PIN2, -motorA_speed);
    } else {
        // Stop
        analogWrite(MOTOR_A_PIN1, 0);
        analogWrite(MOTOR_A_PIN2, 0);
    }
    // Motor B control (similar logic)
    if (motorB_speed > 0) {
        analogWrite(MOTOR_B_PIN1, motorB_speed);
        analogWrite(MOTOR_B_PIN2, 0);
    } else if (motorB_speed < 0) {
        analogWrite(MOTOR_B_PIN1, 0);
        analogWrite(MOTOR_B_PIN2, -motorB_speed);
    } else {
        analogWrite(MOTOR_B_PIN1, 0);
        analogWrite(MOTOR_B_PIN2, 0);
    }
}

// High-level movement functions
void moveForward(int speed) {
    setMotorSpeed(speed, speed);
    Serial.println("🚀 Moving forward");
}

void turnRight(int speed) {
    setMotorSpeed(speed, -speed);  // Differential steering
    Serial.println("↪️ Turning right");
}

void stopRobot() {
    setMotorSpeed(0, 0);
    Serial.println("⏹️ Robot stopped");
}

3 Movement Patterns and Navigation (15 minutes)

Basic Robot Movements:

  • • Forward/backward motion
  • • Turning and rotation
  • • Curved paths and arcs
  • • Complex patterns and sequences
// Movement pattern examples
void squarePattern() {
    Serial.println(" Executing square pattern");
    for (int i = 0; i < 4; i++) {
        // Move forward for 2 seconds
        moveForward(150);
        delay(2000);
        // Turn 90 degrees (approximately)
        turnRight(150);
        delay(500);
        stopRobot();
        delay(500);
    }
    stopRobot();
}

void figureEight() {
    Serial.println(" Executing figure-8 pattern");
    // First loop
    for (int i = 0; i < 8; i++) {
        setMotorSpeed(200, 100);  // Curved left
        delay(500);
    }
    // Second loop (opposite direction)
    for (int i = 0; i < 8; i++) {
        setMotorSpeed(100, 200);  // Curved right
        delay(500);
    }
    stopRobot();
}

void obstacleAvoidance() {
    // Simulated obstacle avoidance
    moveForward(150);
    delay(1000);
    // "Detect" obstacle
    Serial.println(" Obstacle detected!");
    stopRobot();
    delay(500);
    // Turn to avoid
    turnRight(150);
    delay(1000);
    // Continue forward
    moveForward(150);
    delay(1000);
    stopRobot();
}

4 Servo Motor Control (10 minutes)

Servo Applications:

  • • Camera pan/tilt for vision systems
  • • Steering mechanisms
  • • Robotic arm joints
  • • Sensor positioning
// Servo control functions
void sweepCamera() {
    Serial.println("📹 Camera sweep scan");
    // Sweep from 0 to 180 degrees
    for (int angle = 0; angle <= 180; angle += 10) {
        cameraServo.write(angle);
        Serial.print("Camera angle: ");
        Serial.println(angle);
        delay(200);
    }
    // Return to center
    cameraServo.write(90);
}

void lookAround() {
    int positions[] = {45, 90, 135, 90};
    String directions[] = {"Left", "Center", "Right", "Center"};
    for (int i = 0; i < 4; i++) {
        cameraServo.write(positions[i]);
        Serial.print("👀 Looking ");
        Serial.println(directions[i]);
        delay(1000);
    }
}

Hands-On Activity (25 minutes)

Project: Robot Dance Choreographer

Students will program a series of coordinated movements combining DC motors and servo control to create a "robot dance" routine.

Requirements:

  • • Control 2 DC motors for movement
  • • Control 1 servo for "head" movement
  • • Create at least 3 different movement patterns
  • • Combine movements into a choreographed sequence
  • • Add timing and synchronization
  • • Include Serial output describing each move

Creative Challenge:

Add music timing by using specific delay patterns, or create a "robot personality" by varying movement speeds and servo positions to show different "emotions".

Assessment & Homework

Quick Check (In Class):

  • • Can student control motor speed and direction?
  • • Do they understand PWM concepts?
  • • Are they creating coordinated movements?
  • • Can they combine DC motors and servos effectively?

Homework Assignment:

Automated Pet Feeder

Design a pet feeding robot that uses DC motors to move to a feeding location, uses a servo to dispense food, and returns to a "home" position. Include timing controls and multiple feeding schedules.

← Previous: Lesson 10
📝 Take Lesson 11 Quiz Next: Lesson 12 →