
LED (PWM) Exercise
1. Learning Outcomes
After completing this Exercise, you will be able to:
- Understand the use of the Simulink Arduino support package for algorithm design for a simple circuit, e.g., for an LED.
- Understand the operation of Pulse Width Modulation (PWM) to a simple circuit, e.g., for an LED.
2. Hardware Setup
This is the same hardware setup as used in the previous exercise. Note that there is a video at the end of this Section to help with understanding.
The exercise involves connecting an LED to a pulse width modulation (PWM) Arduino output pin and controlling the brightness via using the 8-bits (256 different levels), i.e., the 256 different values of 'brightness'.
Required hardware for this exercise:- Arduino Uno board (supported by Simulink)
- USB Cable Type A to B
- Breadboard
- LED
- 220 Ohm resistor
- 2 x male-male breadboard wires
Set-up the hardware as shown and following these steps:
- Use a male-to-male jumper wire to connect Pin 9 on the Arduino to a chosen column on the breadboard.
- Insert a 220Ohm resistor so that one end is in the same column as the wire from Pin 9. Place the other end of the resistor in a different row.
- Insert the LED so that its long leg (anode) is connected to the free end of the resistor. Then complete the circuit by connecting the LED’s short leg (cathode) to a GND pin on the Arduino using a male-to-male jumper wire.

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, i.e., to vary the brightness of the LED. A sine wave signal will be used to vary the PWM values between zero (off) and 255 (fully on), smoothly adjusting the LED brightness across its full range.
The steps to set up the model are as follows:
- Use a Sine Wave block (from Sources) as the input and connect it to an Abs (absolute value) block (from Math Operations), as shown in (2).
- Connect a Slider Gain block (from Math Operations). You can specify the lower (Low) and upper (High) limits of the gain to constrain the output voltage. For example, to limit the voltage to 2.3 V: 256 × 2.3 = 117.76 ≈ 117. In this case, the upper limit (High) for the Slider Gain would be set to 117. However, for this exercise, set the upper limit to 255 (equivalent to 5 V on the Arduino Uno) (3).
- Add a PWM block (from Common in the Simulink Support Package for Arduino Hardware) and connect three Scopes (from Sinks).
- Then run the steps from the previous exercise to perform the code generation (a recap link is provided below), to create an LED that is varying in brightness! Explore the effect of the gain value within the Slider Gain. Watch the video below for further help!

Key Properties to Modify
- Sine Wave (Sine type) → Set to Sample-based
- Sine Wave (Sample per period) → Set to Sample-based
- Sine Wave (Samples per period) → Set to 300
- Sine Wave (Sample time) → Set to 0.01 seconds
- Slider Gain (High) → Set to 255
All other properties may remain as their default values.
Advancing automatic control engineering (ACE) education through global collaboration




