PWM Tutorial 1 – Using PWM to make an LED “Breathe”

Objective

In this exercise, you will wire and program an LED to “breathe” on and off (example).

Resources

Parts Needed

Item Quantity Detail
5V Power Supply 1
CY8CKIT-042 (Pioneer or BLE) PSoC® 4 Evaluation Board 1
USB micro B cable 1 Included in PSoC® kit
AOTF2618L MOSFET driver circuit 1 (Digikey) (datasheet)
FAN8100N H-Bridge driver circuit 1 (Digikey) (datasheet)
Benchtop Power Supply 1 Available at lab workbenches
Benchtop Multimeter 1 Available at lab workbenches
Gear Motor (model 1) OR 1 Available at lab workbenches (Jameco) (datasheet)
Gear Motor (model 2) 1 Available at lab workbenches (AllElectronics) (datasheet) supplied in class, 1 per station (do not take with you)

Critical Information and Concepts

Concept Importance
a. Which ports on the PSoC® you can connect external LEDs and buttons to. Needed in order to control an external LED and read the button in software. Can be connected to most unused GPIO pins listed in the Appendix of the appropriate Prototyping Kit Guide. (also discussed in Section 4.3.5 of the Pioneer Kit and Section 5.1.4 of the BLE Kit). Make sure you are not connecting to the PSoC® 5LP GPIO Header. The PSoC® 5LP is only used as a programmer.
b. What the logic high (1) voltage coming out of the PSoC® I/O (GPIO) pins is. Needed in order to calculate a proper current-limiting resistor value for an LED. Study section A.2 (including all subsections) in the appropriate Prototyping Kit Guide to determine where power and ground pins are located on the PSoC® board. With the PSoC® board plugged into the computer via USB, measure the supply voltage of the PSoC® board using a multimeter. This will be equal to the logic high voltage on a GPIO pin.
c. How to calculate the value of a series current-limiting resistor for an LED circuit. Needed to prevent an LED connected to a GPIO pin from drawing too much current and burning out. Example and equation (based on Ohm’s Law) can be found in Section 3.5 of Scherz & Monk.
d. What a pullup resistor is and why it is necessary to properly interface a switch to a microcontroller. Needed to understand how to properly wire the switch so that it is always providing a voltage to the PSoC®. Can be found in Section 12.6.9 of Scherz & Monk.
e. What a PWM signal is. Needed to understand the output of the PWM component. Can be found in Section 13.5.3 and 14.4 of Scherz & Monk.
f. How to increment and decrement a counter using a for() loop in C. Needed to change the duty cycle (Compare value) of the PWM. See Loops in C for more information.

Steps

Figure 1: Example of connecting an external LED to a microcontroller using a transistor to switch
Figure 1: Example of connecting an external LED to a microcontroller using a transistor to switch

  1. Create a MOSFET motor circuit above to accommodate a LED and current limiting resistor, as seen above. Note the power source.

    Note that the LED uses the 5V regulated voltage from the ICC1 voltage regulator or the benchtop power supply, rather than unregulated power.

    Figure 2: LED symbol, wording, and leads, from https://commons.wikimedia.org/wiki/File:%2B-_of_Led.svg
    Figure 2: LED symbol, wording, and leads, from https://commons.wikimedia.org/wiki/File:%2B-_of_Led.svg

    1. Assuming that the forward voltage of the LED $ V_{f}$ = 1.7V and that the internal resistance of the AOTF2618 is near zero when on, use Ohm’s Law to calculate the value of the current-limiting resistor in series with it that will maintain 20mA or less when connected and turned on, as seen above.
    2. Connect a jumper wire from the selected PSoC® Pioneer board port/pin to the breadboard, for activating the above transistor. Add a LED and current-limiting resistor to the breadboard. Connect the PSoC® ground pin to the breadboard ground pin. Note that LEDs are polarized, and that current flows in the direction of the arrow.
  2. Connect the CY8CKIT-042 (or CY8CKIT-042-BLE) to your computer via USB.

  3. In PSoC® Creator™, create a new project for the CY8CKIT-042 (or CY8CKIT-042-BLE) in a new workspace for HW3.

  4. In the Top Design, create a PWM(v3.30) component (not the TCPWM mode version). Select One Output. Set the period to “Max” (remember this value) and the compare value to ```0``. Leave all other settings at their defaults.

  5. Create a Clock component and wire it to the clock input on the PWM. Leave the clock at its default frequency.

  6. Create one (1) Digital Output pin.

    1. In the configuration pane, make sure the “HW connection” checkbox is checked and set the “Drive mode” to Strong drive.
    2. Wire the pin to the “pwm” output on the PWM component.
    3. In the Design Wide Resources > Pins window, connect the output pin to the port to which you connected the external LED and series current-limiting resistor.
  7. Build the project once in order to expose the newly specified hardware to the coding API

  8. Open main.c and add two lines of code in the line before the for(;;) loop: one to initialize and one to start the PWM subsystem.

    Hint: In the Top Design, right-click on the PWM component, choose “Open Datasheet…”, scroll to the Application Programming Interface (API) section, and find the line of code to start the PWM component. Remember to use the name of your specific instance of the PWM component in the Top Design (e.g., PWM_1_FunctionName()).

  9. Declare a new variable, ii, as an integer and initialize it to zero.

  10. Inside the main for(;;) loop add the following code:

    1. Add an inner for() loop that increments ii from 0 to the PWM’s “max” value (logged above). Execute a CyDelay(5) in each iteration of the loop.

      1. Inside this second inner for() loop, change the duty cycle (via changing the Compare value), passing in the value of ii.

      Hint: In the API section of the PWM component datasheet, find documentation about the function of the “Compare” value.

    2. After the first inner for() loop, copy a duplicate of the first for() loop.

      1. Again, change the duty cycle (via changing the Compare value), passing in the value of (255-ii).
  11. Compile, download, and run your solution working with the modified AOTF2618L MOSFET driver circuit from ICC3. The LED should “breathe” using the PWM component.

Continue on to PWM Tutorial 2 – Using PWM and an H-Bridge to Control Motor Speed