
- Suggested Learning Resources
- ACE-Lab Shop
- …
- Suggested Learning Resources
- ACE-Lab Shop
- Suggested Learning Resources
- ACE-Lab Shop
- …
- Suggested Learning Resources
- ACE-Lab Shop
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.
1Use Simulink Arduino support packageBuild and deploy a Simulink model for a simple Arduino LED circuit.
2Explain PWM brightness controlDescribe how PWM duty cycle changes perceived LED brightness across the 8-bit range.
3Create a sine-wave-based PWM modelUse a Sine Wave, Abs block, Slider Gain, Scope blocks, and an Arduino PWM block.
4Deploy and verify real-time behaviourRun 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.
1Connect digital pin 9 on the Arduino to a chosen column on the breadboard using a male-to-male jumper wire.
2Insert 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.
3Insert 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.
Hardware set-up for the LED PWM exercise. 3. Simulink Set-Up and PWM Testing
In this part of the exercise, you will develop a Simulink model to control the Arduino PWM output and vary the LED brightness. A sine wave signal is used to smoothly vary the PWM duty cycle between 0 (OFF) and 255 (fully ON).
Add a Sine Wave block
From Simulink → Sources, add a Sine Wave block. Configure it as a sample-based signal for the brightness command.
Add an Abs block
From Simulink → Math Operations, add an Abs block and connect it to the Sine Wave output so that the command remains non-negative.
Add a Slider Gain block
Add a Slider Gain block and set the limits so the PWM command can be scaled up to 255, representing the full 8-bit output range.
Add the Arduino PWM block
From Simulink Support Package for Arduino Hardware → Common, add a PWM block and set the output pin to 9.
Add Scope blocks
Add three Scope blocks to observe the sine wave, processed signal, and scaled PWM command during testing.
Deploy and test brightness
Use the Arduino code-generation workflow, set the stop time to inf, deploy the model, and observe the LED brightness variation while adjusting the Slider Gain.
Simulink set-up for the LED PWM exercise, showing the Sine Wave, Abs, Slider Gain, Scope blocks, and PWM output on pin 9. Build the PWM model from the attached exercise instructionsDeployment 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.
Advancing automatic control engineering (ACE) education through global collaboration


