Neon Realm

Day 8: Advanced Control – Pulse Width Modulation (PWM)

Day 8: Advanced Control – Pulse Width Modulation (PWM)


Welcome to Day 8 of your adventure with Kai and Orion! Today, we’ll learn how

to use PWM on MicroPython and adjust the out levels of our LED


Learning Objectives


By the end of this lesson, you will:


  • Understand the concept of Pulse Width Modulation (PWM) and its applications.
  • Learn how to use PWM to control the brightness of an LED.
  • Write and upload MicroPython code to implement PWM with the Raspberry Pi Pico WH.


Materials Needed


  • Raspberry Pi Pico W
  • USB cable
  • Breadboard
  • LED
  • 220 ohm resistor
  • Jumper wires (Male to Male)


Initial Situation: After mastering basic input and output controls, Kai and Orion are ready to explore more advanced techniques, starting with PWM.


Kai adjusted his steampunk goggles, examining the new setup Orion had suggested.


Kai: “PWM, huh? This sounds interesting!” he said. Orion hovered closer, ready to guide him through the process.


Call to Action: Orion explains the importance of PWM and introduces the concept of using it to control the brightness of an LED.


Setting Up the Scene


  • Story: Kai and Orion gather their materials in the tech shop, setting up their lab.
  • Activity: Gather your materials and prepare your workspace.


Kai: Arranging the components on the workbench “Alright, I’m ready. What’s next, Orion?”

Orion: Hovering near the workbench, eyes glowing “First, let’s set up our PWM circuit.”


Introduction to Concepts


  • Story: Orion explains the basics of PWM and its role in controlling devices like LEDs and motors.
  • Activity: Learn about how PWM works and how it can be used to control the brightness of an LED.


Orion’s Explanation:


Orion: “PWM, or Pulse Width Modulation, is a technique used to control the power delivered to electrical devices. By adjusting the duty cycle, we can control the brightness of an LED.”


Practical Application


  • Story: Kai connects the components under Orion’s guidance, creating a circuit with an LED controlled by PWM.
  • Activity: Connect the LED to the breadboard and integrate it with the Raspberry Pi Pico WH for PWM control.


Orion’s Tutorial on Wiring the PWM Circuit:


  1. Orion: “Connect the negative (shorter) leg of the LED to a resistor.”
  2. Orion: “Connect the other end of the resistor to the ground rail of the breadboard which is connected to a ground pin on the Pico W.
  3. Orion: “Connect the positive (longer) leg of the LED to a GPIO pin on the Raspberry Pi Pico WH (e.g., GPIO 15).”


Kai: Placing the components with care “I think I’ve got it. How does this look?”

Circuit Diagram

Orion: Inspecting the setup “Excellent, Kai. Now let’s write the code to control the LED with PWM.”


Writing Code


  • Story: Orion guides Kai through writing the code to control the LED using PWM.
  • Activity: Write and upload MicroPython code to control the brightness of the LED with PWM.


Orion’s Tutorial on Coding:


Orion: “Open Thonny IDE and ensure the Raspberry Pi Pico WH is connected. Select ‘MicroPython (Raspberry Pi Pico)’ as the interpreter.”

Kai: Focused on the screen “Alright, Thonny is open and the interpreter is set.”

Orion: “Now write the following code to control the LED with PWM:


Kai: Typing the code, his eyes scanning each line “Okay, I’ve entered the code. What’s next?”