PSoC5LP Lab 02: Switch and LED Control with C Code
Objective
- Turn the LED on when the switch is pressed and turn it off when released.
- Implementing a toggle function, where pressing the switch turns the LED on, and pressing it again turns it off.
Overview
In embedded systems, switches typically serve as input devices, while LEDs act as output indicators to visually reflect the system’s status. In this experiment with PSoC Creator and C code, the LED's behavior is managed by configuring specific GPIO pins—one to receive the switch input and another to control the LED output. When the switch is pressed, it sends a signal that triggers the microcontroller to change the LED's state.
Required Reading Material
- Lesson KB 01: Create a PSoC Project using PSoC Creator
- Input Signal Edge Detection using Software
- PSoC Creator Component Datasheet:
Required Components
If you use the Cypress CY8CKIT-059 Kit, the onboard LED and button will be used in this lab.
The following components are required for this lab.
Push Button (Optional, can use onboard components) | x 1 | |
Resistor 200ohm (if using an external LED) | x 1 | |
Red LED (or onboard LED) | x 1 |
Circuit / Schematic
Cypress CY8CKIT-059 Kit
The CY8CKIT-059 board contains a single push button and a blue User LED. The push button connects the GND and the P2[2] pin and the blue user LED is connected to a resistor and P2[1] of the target PSoC 5LP device.
EagleSoC Development/Mini Board
Procedure
Creating a New Project
- Launch PSoC Creator.
- Got to File ➤ Open Project ➤ Project/Workspace.
- Open the PSoC5LP workspace in the EE4450 folder.
- After PSoC Creator opens the workspace, right-click on Workspace 'PSoC5LP' in the Workspace Explorer and select Add ➤ New Project...
- Select the correct PSoC5LP device model number, use the "Empty schematic" template, and enter the project name 02_GPIO_CCode.
Adding PSoC Creator Components
Open the "TopDesign.cysch" Schematic File, add the following components:
- Add a Digital Input Pin:
- Navigate to the Ports and Pins catalog in the component selection panel.
- Drag and drop the Digital Input Pin onto the schematic.
- Add a Digital Output Pin:
- Similarly, go to the Ports and Pins catalog.
- Drag and drop the Digital Output Pin onto the schematic.
The TopDesign schematic shown as below:
Configure the Components
- Config the Digital Input Pin (Pin_1):
- Click on the Pin_1 component in the schematic.
- Rename the component to nSW1 to reflect its function as a switch.
- Set the Drive Mode to Resistive pull-up, which ensures the pin defaults to high when not pressed.
- Uncheck the box for HW connection to disable the hardware connection, as shown in the diagram.
- Config the Digital Output Pin (Pin_2):
- Select the Pin_2 component.
- Rename it to LED1, indicating that it controls the LED.
- Uncheck the HW connection box, as the LED will be software-controlled..
The new TopDesign.cysch file is shown as below:
Click the Generate Application button to allow PSoC Creator to generate or update the various API files.
Pin Assignment
Device | Port.Pin | Direction | Drive Mode |
---|---|---|---|
Experiments
Exp#01: Control the LED
- Objective: Create a simple function to control the LED based on the switch's state.
- Steps:
- When the user presses the SW1 switch (connected to nSW1), the LED (connected to LED1) should turn on.
- When the user releases the switch, the LED should turn off.
- Implementation:
- In the C code, continuously check the state of nSW1. If the switch is pressed (input logic is low), set the LED1 pin high to turn on the LED. If the switch is released (input is high), set the LED1 pin low to turn it off.
Exp #02: Toggle the LED
- Objective: Modify the function so that pressing the switch toggles the LED's state.
- Steps:
- When the user presses SW1 once, the LED should turn on.
- When the user presses SW1 again, the LED should turn off.
- Implementation:
- Implement a toggle mechanism using a flag. Each time the switch is pressed, check for a transition (from high to low), and alternate the state of LED1. This ensures the LED turns on and off with consecutive presses.