⚙️ Controlling Motors
Controlling Motors
Learn how to control motors with your Raspberry Pi - this lets you build moving projects like fans, robots, and more!
Types of Motors
There are different types of motors you can control. Here are the most common ones:
Servo Motor
Moves to specific angles (0-180 degrees)
Perfect for pan/tilt, robot arms
DC Motor
Spins continuously at variable speed
Perfect for fans, wheels, propellers
Stepper Motor
Moves in precise steps
Perfect for 3D printers, precise positioning
What is PWM?
PWM stands for Pulse Width Modulation. It's a way to control motors by rapidly turning power on and off. By changing how long the power is on vs off, you can control speed or position!
Controlling Servo Motors
Servo motors can move to specific angles (usually 0-180 degrees). They're perfect for pan/tilt cameras, robot arms, and steering mechanisms.
Materials Needed:
- Raspberry Pi
- Servo motor (SG90 is common and affordable)
- Jumper wires
- External power supply (recommended for larger servos)
Wiring Instructions:
- Red wire (VCC): Connect to 5V (or external 5V power supply)
- Black/Brown wire (GND): Connect to GND
- Orange/Yellow wire (Signal): Connect to GPIO 18 (PWM pin)
Note: Small servos can run from Raspberry Pi power, but larger ones need external power supply!
Servo Control Code:
Understanding the Code:
- GPIO.PWM(pin, frequency): Creates a PWM object - 50 Hz is standard for servos
- pwm.start(0): Starts PWM with 0% duty cycle
- set_angle(angle): Function that converts angle to duty cycle
- Duty cycle formula: 2% = 0°, 7% = 90°, 12% = 180°
- pwm.ChangeDutyCycle(): Changes the position of the servo
Controlling DC Motors
DC motors spin continuously. You can control their speed using PWM, and direction using an H-bridge motor driver.
DC Motor with L298N Driver:
Project: Servo Motor Position Controller
Let's create a system that moves a servo motor to different positions!
Materials Needed:
- Raspberry Pi
- Servo motor (SG90 recommended)
- Jumper wires
- Optional: External 5V power supply
Complete Servo Control Code:
Understanding the Code:
- The servo sweeps smoothly from 0° to 180° in 10° steps
- Then sweeps back from 180° to 0°
pwm.ChangeDutyCycle(0)after movement prevents servo jitter- This creates a smooth back-and-forth motion
Common Mistakes to Avoid
⚠️ Watch Out For:
- Wrong Frequency: Servos need 50 Hz, DC motors can use 100-1000 Hz
- Not Stopping PWM: Always call
pwm.stop()before cleanup - Insufficient Power: Large motors need external power supply
- Wrong Duty Cycle: Servos need 2-12% duty cycle for 0-180°
- Forgetting Cleanup: Always call
GPIO.cleanup()when done
Summary
You've learned:
- ✅ Motors let you create moving projects
- ✅ Servo motors move to specific angles (0-180 degrees)
- ✅ DC motors spin continuously at variable speeds
- ✅ PWM (Pulse Width Modulation) controls motor speed and position
- ✅ Servos use 50 Hz PWM with 2-12% duty cycle for 0-180°
- ✅ Always stop PWM and cleanup GPIO when done
- ✅ Large motors may need external power supply
🎮 Try It: Practice with Motors!
Practice writing code to control motors. Try these challenges:
📝 Challenge 1: Servo to Center
Write code that moves a servo to 90 degrees (center position):
📝 Challenge 2: Servo Sweep
Write code that makes a servo sweep from 0 to 180 degrees and back:
🎯 Activity: Servo Motor Position Controller
What You'll Build:
Create a system that controls a servo motor to move to different positions!
Step-by-Step Instructions:
- Wire Servo: Connect signal wire to GPIO 18, power to 5V, ground to GND
- Write Code: Use the servo control code from the Learn tab
- Test Basic Movement: Try moving to 0°, 90°, and 180°
- Create Sweep: Make servo sweep smoothly from 0 to 180 and back
- Experiment: Try different speeds and step sizes
Testing Checklist:
- ✅ Servo moves to 0 degrees
- ✅ Servo moves to 90 degrees (center)
- ✅ Servo moves to 180 degrees
- ✅ Servo sweeps smoothly without jitter
💪 Practice Challenges
Challenge 1: Servo Sequence
Create a sequence where servo moves: 0° → 45° → 90° → 135° → 180° → 0°
Challenge 2: Slow Sweep
Make the servo sweep very slowly (hint: use smaller steps and longer delays):
Challenge 3: Button-Controlled Servo
Use a button to control servo position:
- Press button once: Move to 0°
- Press again: Move to 90°
- Press again: Move to 180°
- Press again: Back to 0° (cycle repeats)
Challenge 4: Code Detective
What's wrong with this servo code? Find and fix the mistakes!