• LED (PWM) Exercise | ACE-Lab

    LED (PWM)

    Developed by Dr James E. Pickering

    This exercise extends the previous LED PWM output task by using Pulse Width Modulation (PWM) to vary LED brightness smoothly. Follow the steps on this page to build the Simulink model, deploy it to Arduino Uno, and test brightness control over the 8-bit PWM range.

    Watch the demonstration

    PWM control goal

    Use a sine-wave-based signal, an Abs block, a Slider Gain, and an Arduino PWM block on pin 9 to vary the LED from OFF to full brightness.

    1. Learning Outcomes

    After completing this exercise, learners should be able to use model-based design to generate a PWM output and control LED brightness on Arduino-compatible hardware.

    1
    Use Simulink Arduino support package

    Build and deploy a Simulink model for a simple Arduino LED circuit.

    2
    Explain PWM brightness control

    Describe how PWM duty cycle changes perceived LED brightness across the 8-bit range.

    3
    Create a sine-wave-based PWM model

    Use a Sine Wave, Abs block, Slider Gain, Scope blocks, and an Arduino PWM block.

    4
    Deploy and verify real-time behaviour

    Run the model continuously on Arduino Uno and observe smooth LED brightness variation.

    2. Hardware Set-Up

    This exercise uses the same hardware configuration as the previous LED PWM exercise, but the signal applied to the LED is now a PWM-capable Arduino output.

    Connect Arduino digital pin 9 to the LED circuit through a 220 Ohm resistor. The long LED leg (anode) connects to the resistor, and the short leg (cathode) connects to GND. Pin 9 is used because it supports PWM output on Arduino Uno.

    Required hardware:

    • Arduino Uno board
    • USB cable (Type A to Type B)
    • Breadboard
    • LED
    • 220 Ohm resistor
    • 2 × male-to-male breadboard wires

    Assembly guidance

    Assemble the LED and resistor in series so that the PWM signal from pin 9 controls current through the LED. Correct LED polarity is essential before deploying the PWM model.

    1

    Connect digital pin 9 on the Arduino to a chosen column on the breadboard using a male-to-male jumper wire.

    2

    Insert a 220 Ohm resistor with one end in the same column as the wire from pin 9 and the other end in a different row.

    3

    Insert the LED such that the long leg (anode) is connected to the free end of the resistor, and the short leg (cathode) is connected to an Arduino GND pin using a jumper wire.

    Arduino Uno connected to an LED and resistor on a breadboard for PWM control
    Hardware set-up for the LED PWM exercise.

    Deployment Reminder

    Deploy the PWM model using the same code-generation procedure as the previous LED exercise. The model should execute continuously on Arduino Uno while the LED brightness varies smoothly.

    Set continuous execution

    Set the simulation stop time to inf before deployment.

    Deploy with Monitor & Tune

    Compile and deploy the model to the Arduino so the PWM signal runs in real time.

    Observe and adjust

    Observe the LED brightness variation and explore the effect of adjusting the Slider Gain.

    4. Troubleshooting Checks

    Use these checks if the model deploys but the LED brightness does not vary as expected.

    The LED does not turn on

    Check the LED orientation, resistor placement, GND connection, and that pin 9 is connected to the resistor/LED chain.

    The LED is only ON or OFF, not smoothly dimming

    Confirm that the model uses the PWM block rather than the Digital Output block, and check that the command is scaled across the 0 to 255 PWM range.

    Brightness variation is too fast, too slow, or not smooth

    Review the Sine Wave settings, Abs block, Slider Gain limits, and Scope waveforms. Adjust the sine wave frequency or amplitude and verify the PWM command before deployment.

    5. Additional Exercises

    After completing the main task, learners can extend the PWM exercise by changing signal characteristics, limiting the PWM range, and comparing PWM with digital LED control.

    Exercise 1: Change PWM Signal Characteristics

    Modify the input signal used to drive the PWM block, such as sine wave frequency or amplitude, and describe how these changes affect the rate and smoothness of LED brightness variation.

    Exercise 2: Modify PWM Range and Behaviour

    Rewrite the PWM-related requirements by selecting new limits for the PWM output range, for example restricting maximum brightness below 255. Update the Simulink model and observe the effect on LED brightness. Explore the use of the Saturation block.

    Exercise 3: Compare Digital and PWM Control

    Using observations from this PWM exercise and the previous LED PWM exercise, explain the difference between controlling an LED using a digital ON/OFF signal and using a PWM signal to vary brightness.

    6. Concluding Remarks

    This exercise has extended the previous digital LED control task by introducing Pulse Width Modulation (PWM) as a method for analogue-style control using a digital output. By varying the PWM duty cycle, learners demonstrate how LED brightness can be smoothly adjusted rather than simply switched ON or OFF.

    Through the development and deployment of a Simulink model using a sine wave input, learners gain practical experience in applying model-based design to PWM control on embedded hardware. The exercise reinforces signal scaling, duty cycle control, and real-time execution using automatically generated code.

    The LED PWM exercise is a useful stepping stone toward applications such as motor speed control, dimming systems, and closed-loop control, where clear requirements, correct hardware interfacing, and disciplined Simulink configuration are essential.