## **Lab 12** | Pulse Width Modulation (PWM) and Actuator Control ### Design of Autonomous Systems ### CSCI 6907/4907 - Section 86 ### Prof. **Sibin Mohan** ### Spring '25 | SEH 4040 --- ## **Objectives** - Understand the fundamentals of **Pulse Width Modulation (PWM)** - Learn how to control **servo motors** and **DC motors** using PWM - Gain hands-on experience generating PWM signals to control actuators in an autonomous rover --- ## **1. Introduction** PWM is a method of **controlling power** delivered to electrical components using digital signals. It is essential for: - **Speed control** in motors - **Brightness control** in LEDs - **Precise positioning** in servo motors --- ## **2. What is PWM?** PWM switches a digital signal ON and OFF at a fast rate. By adjusting the **ON time**, we control the **average power** delivered. --- ### Example: - Signal toggles between **3.3V (HIGH)** and **0V (LOW)** - 50% duty cycle ≈ 1.65V average output --- ## **3. Duty Cycle and Control** | Duty Cycle | Signal Behavior | Effect on Device | |------------|------------------|------------------------| | 0% | Always OFF | No power | | 50% | Half ON/OFF | Medium power/speed | | 100% | Always ON | Max power/speed | **Duty cycle** = % of one cycle that signal is HIGH --- ## **4. PWM vs Analog Voltage** PWM is not true analog but **mimics** analog output: - **Capacitors** can smooth PWM signals to get a near-analog voltage - Useful for controlling devices that require variable voltages --- ## **5. Controlling Servo Motors** Servo motors respond to **pulse width**, not just duty cycle. | Pulse Width | Approx. Angle | |-------------|----------------| | 1.0 ms | 0° | | 1.5 ms | 90° | | 2.0 ms | 180° | **Period:** 20ms (50Hz signal) **Formula**: ```python duty = 2 + (angle / 18) ``` --- ## **6. Controlling DC Motors** Use **PWM for speed** and **GPIO for direction** via a motor driver (L298N, TB6612, etc.) Example: ```python pwm.ChangeDutyCycle(75) # 75% speed ``` - Use **≥1kHz** PWM to reduce motor whine --- ## **7. Real-World Applications** | Application | Frequency | Duty Cycle Range | Notes | |------------------|-----------|------------------|----------------------------------| | Servo Motors | 50Hz | ~5–10% | Pulse widths: ~1–2ms | | DC Motor Speed | ≥1kHz | 0–100% | Fast switching = smoother motion | | LED Brightness | ~500Hz | 0–100% | Higher freq = less flicker | --- ## **8. Simulator** Explore and visualize PWM with these tools: - 🎛️ [PWM Visualizer](https://pwm-visualizer.vercel.app/) - 📈 [Desmos: PWM Curve](https://www.desmos.com/calculator/hnhb191rn9) --- ## **9. Summary** - PWM lets digital systems simulate analog control - You can tune **speed, brightness, or angle** with duty cycle - Both **servo** and **DC motors** rely on PWM in autonomous systems - Understanding PWM = deeper hardware-level control