
- In Practice (coming soon!)
- …
- In Practice (coming soon!)
- In Practice (coming soon!)
- …
- In Practice (coming soon!)
On/Off Power Switch Exercise | ACE-Lab On/Off Power Switch
Developed by Dr James E. Pickering
The video below introduces the switched LED circuit. Follow the steps on this page to assemble the hardware, deploy the Simulink PWM model, and then use the physical on-off switch to interrupt and restore the LED circuit path.
1. Learning Outcomes
After completing this exercise, learners should be able to combine a Simulink PWM output workflow with a simple physical switching element in an Arduino LED circuit.
1Build a deployable PWM Simulink modelCreate a model that drives an Arduino PWM output using a Sine Wave, Abs, Slider Gain, and PWM block.
2Wire a switched LED circuit correctlyPlace an on-off switch in the LED current path so the circuit can be opened and closed physically.
3Configure Arduino pin 9 for output controlRoute the PWM command to Arduino pin 9 and connect the hardware path through the switch, resistor, and LED.
4Verify model behaviour against hardware behaviourDeploy the model, observe the LED brightness variation, and confirm that the switch interrupts and restores the circuit.
2. Hardware Set-Up
The switch is placed into the LED circuit path so that continuity can be interrupted or restored. Pin 9 on the Arduino is routed through the switch, then through the resistor and LED to ground.
Required hardware:
- Arduino Uno board supported by Simulink
- USB cable, Type A to Type B
- Breadboard
- LED
- 220 Ohm resistor
- 2 × male-to-male breadboard wires
- On-off power switch
Assembly guidance
Assemble the circuit so that current flows from Arduino pin 9 through the closed switch, resistor, and LED before returning to GND. Opening the switch should break the circuit path even while the Simulink model is still running.
1Connect the on-off power switch to the breadboard. Use a jumper wire to connect Arduino pin 9 to the same column as one terminal of the switch.
2Insert a 220 Ohm resistor so that one end is in the same column as the opposite terminal of the switch. Place the other end of the resistor in a different breadboard row.
3Connect the long leg of the LED, the anode, to the free end of the resistor. Complete the circuit by connecting the LED cathode column to an Arduino GND pin.
Hardware arrangement for the On/Off Power Switch exercise. 3. Simulink Set-Up
The switch is added in the hardware path, so the Simulink model can use the same PWM-based structure as the earlier LED PWM exercise. The model generates a varying command signal and sends it to Arduino pin 9.
Add a Sine Wave block
From Simulink → Sources, add a Sine Wave block and connect it to an Abs block from Math Operations.
Add a Slider Gain block
Connect a Slider Gain block from Math Operations. Set the upper limit to 255 so the command maps to the Arduino PWM range.
Add the PWM block and Scopes
From the Simulink Support Package for Arduino Hardware → Common, add a PWM block. Add Scope blocks from Sinks to monitor the generated signals.
Configure the model settings
Set the Sine Wave to sample-based mode, set samples per period to 300, set sample time to 0.01 seconds, and set the PWM pin to 9.
Test the switch behaviour
After deployment, close the switch to allow the PWM-controlled LED behaviour and open the switch to interrupt the circuit path.
Key properties to modify
- Sine Wave, sine type → sample based
- Sine Wave, samples per period → 300
- Sine Wave, sample time → 0.01 seconds
- Slider Gain, high limit → 255
- PWM block, output pin → 9
Simulink model for PWM control of the LED, used with the physical on-off switch in the circuit path. 4. Running Simulink Code Generation
Configure Simulink for the Arduino Uno target, deploy the PWM model, and then verify the effect of the physical switch on the live circuit.
Connect the Arduino board
Connect the Arduino Uno to your computer using the USB cable.
Open Model Settings
In Simulink, open Model Settings by selecting Modelling → Model Settings.
Select the hardware board
Under Hardware Implementation, choose Arduino Uno as the hardware board, then click Apply and OK.
Deploy the model
Set the simulation stop time to inf, then use Monitor & Tune to compile and deploy the model to the Arduino.
Verify the switch
Observe the LED when the switch is closed, then open the switch and confirm that the LED turns off because the circuit path is broken.
5. Troubleshooting Checks
Use these checks if the model deploys but the LED or switch does not behave as expected.
The LED does not turn on when the switch is closed
Check the LED orientation, confirm the resistor is in series, and make sure the switch terminals are connected across the intended breadboard break rather than the same connected row.
The switch does not appear to change anything
Confirm that Arduino pin 9 routes through the switch before the resistor and LED. If the switch is bypassed by another wire, opening it will not interrupt the circuit.
The LED brightness does not vary
Re-check the Sine Wave, Abs, Slider Gain, and PWM settings. The Slider Gain high limit should be 255, and the PWM block should be assigned to pin 9.
Simulink cannot deploy to the board
Confirm the Arduino support package is installed, the board is set to Arduino Uno, the USB connection is active, and the model 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 the PWM behaviour, adding switch-state observations, and documenting how their revised behaviour was verified.
Exercise 1: Modify the PWM Brightness Pattern
Choose new values for the sine wave timing or slider range. Deploy the revised model and verify that the LED brightness changes while the switch is closed.
Exercise 2: Compare Model Control and Physical Switching
Explain the difference between changing the PWM command in Simulink and opening the physical switch in the circuit. Record what happens in each case.
7. Concluding Remarks
This exercise extends the earlier LED activities by introducing a physical on-off switch into the hardware path. The result demonstrates how a circuit element can change system behaviour even when the deployed Simulink model continues to run.
By combining a PWM model with a switched circuit path, learners reinforce their understanding of circuit continuity, PWM-driven LED control, and the relationship between model-based design and practical embedded hardware implementation.
Advancing automatic control engineering (ACE) education through global collaboration

