• LED (Digital) Exercise | ACE-Lab

    LED (Digital)

    Developed by Dr James E. Pickering

    The video below shows what your system should do by the end of this exercise. Follow the steps on this page to build the Simulink model, generate the code, and run it on the hardware so that your LED behaves in the same way.

    1. Learning Outcomes

    After completing this exercise, learners should be able to apply a simple model-based workflow to control a digital output device using Arduino-compatible hardware.

    1
    Build a deployable Simulink model

    Create a simple model that connects a Pulse Generator to an Arduino Digital Output block.

    2
    Configure Arduino digital output hardware

    Wire an LED circuit correctly and assign the model output to Arduino digital pin 9.

    3
    Apply sample-based timing parameters

    Set sample time, pulse period, and duty cycle values that produce the required LED behaviour.

    4
    Deploy and verify behaviour on hardware

    Run code generation, monitor the deployed model, and compare observed LED behaviour with the expected output.

    2. Hardware Set-Up

    The LED circuit should be assembled so that the Arduino can drive the LED directly from a digital output pin. A resistor must be included to limit current and protect the LED.

    Connect Arduino digital pin 9 to the LED circuit through a 220 Ohm resistor. The LED cathode should be connected to GND. This allows the digital output signal to switch the LED between OFF and ON states.

    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 circuit carefully so that current flows through the resistor before reaching the LED, and ensure that the cathode is connected to ground. A correct wiring sequence makes the later deployment stage straightforward and reliable.

    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
    Hardware arrangement for the LED digital output exercise.

    4. Running Simulink Code Generation

    In this part of the exercise, you will configure and run a Simulink model to perform code generation on a supported Arduino Uno board.

    Connect the Arduino board

    Connect the Arduino Uno to your computer using a USB cable.

    Open Model Settings

    In your Simulink window, open Model Settings by selecting Modelling → Model Settings.

    Select the hardware board

    From Hardware Implementation, choose Arduino Uno under Hardware board, then click Apply and OK.

    Confirm the Hardware tab

    A new Hardware tab should now appear in Simulink after the target board is configured.

    Check the working directory

    Make sure MATLAB is operating and saving in a suitable working directory before clicking Run.

    Deploy the model

    Confirm that Arduino Uno is displayed, change the simulation stop time to inf, and click Monitor & Tune to compile and deploy the model to hardware.

    Simulink hardware implementation and deployment view
    Hardware implementation and Monitor & Tune configuration for Arduino deployment.

    5. Troubleshooting Checks

    Use these checks if the model deploys but the LED does not behave as expected. They are deliberately practical and focused on the most common lab faults.

    The LED does not turn on

    Check the LED orientation first: the long leg should connect towards the resistor and pin 9, while the short leg should connect to GND. Also confirm the resistor is in series, not in a disconnected breadboard row.

    The LED stays permanently on or off

    Re-check the Pulse Generator settings, especially sample-based mode, sample time, period, pulse width, and amplitude. Then confirm the Digital Output block is set to pin 9.

    Simulink cannot deploy to the board

    Confirm that the hardware board is set to Arduino Uno, the USB cable is connected, the correct support package is installed, and the simulation stop time is set to inf before using Monitor & Tune.

    6. Additional Exercises

    After completing the main task, learners can extend the exercise by changing timing requirements, adding additional digital outputs, and documenting how their revised behaviour was verified.

    Exercise 1: Modify the Timing Requirements

    Choose new user-defined values for the LED ON/OFF period and the sample time. Update the Simulink model accordingly and verify that the deployed system operates with the revised timing parameters.

    Exercise 2: Out-of-Sequence LED Control

    Extend the system by adding a second LED connected to a different Arduino digital output pin.

    • First, write a new set of requirements specifying out-of-sequence, phase-shifted LED behaviour.
    • Then, implement and deploy the Simulink model to demonstrate asynchronous operation of the two LEDs.

    7. Concluding Remarks

    This exercise has demonstrated the complete workflow for implementing a simple digital control task using Simulink and an Arduino Uno, from defining requirements through to real-time execution on hardware. By developing, configuring, and deploying a Simulink model to control an LED, learners gain practical experience in linking model-based design with embedded hardware implementation.

    The exercise reinforces key concepts including digital input/output operation, timing through sample-based signals, and automatic C-code generation using the Simulink Arduino support package. It also provides a foundation for more advanced tasks involving sensors, actuators, and closed-loop control.