Pulse-Width Modulation
Run the Pulse-Width Modulation Demo
Pulse-width modulation is the way for microcontrollers to send a precise signal to a device such as a motor controller to control motor speed. PWM signals can also be used to change the brightness of an LED. This simulation has a slider that allows the user to change the "duty cycle" of a waveform. The simulation is a bright green line on a black background, just like the old CRT oscilloscope displays.
Sample ChatGPT Prompt
Generate a p5.js sketch file of a simulation of a static pulse width
modulation (PWM) waveform on a 600x300 canvas.
Add a range control slider to adjust the Duty Cycle of the waveform.
Make the default Duty Cycle be 50%.
The min should be 0% and the max should be 100%.
Place the range control slider at the bottom of the canvas.
Make the leftSliderMargin be 155.
Make sure the range control slider has a width of canvasWidth-leftSliderMargin-20 using the
style attribute.
At the end of the draw() function add a text() function that displays
the label and value of the duty cycle.
I have tested this prompt several times using GPT-4 and it seems to work very well except for the precise layout of the duty cycle label, value and slider positioning. I had to tweak these by hand.
Edit This MicroSim Using the P5.js Editor
Sample Response
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
|
Key Learnings from the Simulation
- We can use the
map()
function to transform the x coordinate into the time period of a waverform (t.) - We can draw a square wave using a
sin()
function with a threshold test. - We can use the
beginShape()
andendShape()
around as set ofvertex()
point placements to generate waveforms. - We can use the "number format" function
nf()
to display the percentage without a decimal point.
Lesson Plan
Lesson Plan Prompt
I have a webpage that simulates a pulse-width-modulation waveform.
The page has a slider that allows students to change the duty cycle from 0% to 100%.
When the students change the slider, the waveform is updated with the duty cycle.
Generate a lesson plan for a 9th-grade robotics class that uses this simulation.
For follow up activities, we have Raspberry Pi Picos that we program with MicroPython.
Return the response in a single raw markdown file.
Lesson Plan: Understanding Pulse-Width Modulation in Robotics
Objective:
Students will understand the concept of PWM and its application in controlling robotic components like motors and LEDs.
Materials Needed:
- Computers with internet access for the PWM simulation webpage.
- Projector for demonstrations.
- Optional: Physical components such as a small motor, LED, and Arduino for real-world demonstration.
Duration:
1 hour
Lesson Outline:
1. Introduction to PWM (15 minutes)
- Begin with a brief overview of PWM and its significance in robotics.
- Explain key terms: duty cycle, frequency, analog vs. digital signals.
2. Demonstration of PWM Simulation (10 minutes)
- Project the PWM simulation on the screen.
- Show how changing the duty cycle affects the waveform.
- Relate this to how it would affect a motor's speed or an LED's brightness.
3. Hands-On Activity with the Simulation (20 minutes)
- Students will use the simulation on their computers.
- Tasks:
- Set the duty cycle to various levels (0%, 25%, 50%, 75%, 100%) and observe changes.
- Students note their observations regarding the waveform changes.
4. Real-World Application Discussion (10 minutes)
- Discuss how PWM is used in real-world robotics.
- If available, demonstrate using a motor or LED with an Arduino.
- Encourage students to think of examples where PWM might be used in everyday technology.
5. Reflection and Q&A (5 minutes)
- Encourage students to ask questions.
- Discuss the importance of PWM in precise control in robotics.
Assessment:
- Students will submit a brief report on their findings from the simulation.
- Include a few quiz questions at the end of the session to gauge understanding.
Extension Activity:
- For advanced students or as a follow-up lesson, introduce a Python program that uses PWM to control a motor speed using a motor driver chip such as an L293D or control an LED brightness.
Notes:
- Ensure that all students understand the basic concepts before moving to the hands-on activity.
- Assist students who might have difficulty with the simulation or concepts.
- Encourage group discussions and peer learning.