Switch Controlled LEDs

Vivado Project 2: Use Switches to Control LEDs


This project demonstrates how to use Verilog® HDL with an FPGA board. In this project you will use a switch on your FPGA board to turn on an LED. In doing this, you will learn the first steps of writing Verilog code and observe how a switch can control LEDs on an FPGA board. You won't need to have a really thorough understanding of digital engineering to make this project work; however, if you would like to know the inner workings of the digital circuit, more information is available in Project 1.

Before you begin, you should:
  • Have the Xilinx® Vivado WebPACK™ installed.
  • Have your FPGA board set up.
After you're done, you should:
  • Understand Top Module is the boundary of the implemented digital system.
  • Know how to use assignment statements in Verilog to make connections.
  • Know how to use switches to provide input signals to a digital system.
  • Know how LEDs show the status of output signals of a digital system.
  • Know how to use UCF files to map I/Os (inputs/outputs) of a system to physical devices on-board.


Qty Description
1 Digilent® Nexys™4, or Basys™3 FPGA Board
1 Xilinx Vivado Design Suite: WebPACK (2014.4 Recommended)

Background Knowledge

A Digital circuit contains a power supply, devices, and conduction nets. Some nets provide the circuit with inputs from the “outside world,” while others conversely provide information from the circuit to the outside world. The nets that provide an interface between the circuit and the outside world are called ports.

Circuits need input devices to generate input signals. Input devices can take many forms, among them keyboards (such as on a PC), buttons, and switches. Circuits also need output devices to communicate their state to the user. In this project, a switch is used as the input device and an LED is used as the output device.

The digital circuit we will build is called the “led_sw,” as shown in Fig. 1 below. The circuit created in this project will be implemented inside the FPGA board. The board has an input port called “sw,” which receives an input signal from the external switch in the circuit and an output port called “led,” which drives the external LED in the circuit. The “led_sw” is a simple circuit that bypasses the signal on the input port and directly sends information to the output port. You can view this as a direct wired connection between the net “sw” and the net “led.” The circuit will be implemented using Verilog HDL. On different FPGA boards, switches and LEDs are connected to different pins on an FPGA chip. Thus, a Xilinx design constraint (XDC) is needed to map the input and output net of the circuit to the physical pin location on the FPGA chip. Take Basys3 as an example, the Slide Switch 0 (SW0) is connected to FPGA pin V17, and FPGA pin U16 drives LED 0 (LD0).

When you slide the switch to the ON position, a high voltage will be placed on FPGA pin V17, which is mapped to the input port of the circuit “led_sw.” The digital circuit then transmits the signal onto the output port LED, which is connected to FPGA pin U16. The high voltage on the output port “led” will cause a voltage drop between node A and node B. This voltage drop will drive current through the LED, which will light the LED and inform the user that the switch is on.

Figure 1. led_sw digital circuit diagram.

Step 1: Create a new project

  1. Create a Verilog module as explained in the previous project, which can be accessed through the link provided at the right, and name it “led sw”.
  2. Once you have created the module an inputs and outputs setting page will appear.
    • Add “sw” as an input; ignore the “bus,” “MSB,” and “LSB.”
    • Add “led” as an output; ignore the “bus,” “MSB,” and “LSB.”

Step 2: Design the Circuit in Verilog HDL

In step 1, a template has been generated by the Xilinx tools as follows:

				module led_sw(
					output led,
					input sw

In this code, you have demonstrated that the net “led” is the output and the “sw” is an input. Now it is time to implement the circuit. At this time we will use an “assign” statement in Verilog to connect the output port “led” to the input port “sw”. The “assign” statement is used so that whenever a signal is placed on the input net it will be transmitted directly to the output port.

				assign led = sw;

Lastly, you must always make sure to end the module with the “endmodule” code.

When you have completed this project file, it should look like this:

				'timescale 1ns/1ps
				module led_sw(
					output led,
					input sw

				assign led = sw;


Step 3: Create a Xilinx Design Constraints (XDC)

The circuit has been implemented but the Xilinx tools still need to know which physical pins on the FPGA the input and output ports are mapped to. The first line on the XDC file refers to the pin location of port sw. The second line refers to the IO Standard of port sw. You don't need to know the details of IO Standards yet, just remember that for each port you must have both the pin location and IO Standard. The XDC file will give the tools for this information. Please choose your board in the dropdown menu to see the UCF file corresponding to your board, as pin locations vary from board to board:

			set_property PACKAGE_PIN U9 [get_ports {sw}]					
				set_property IOSTANDARD LVCMOS33 [get_ports {sw}]
			set_property PACKAGE_PIN T8 [get_ports {led}]					
				set_property IOSTANDARD LVCMOS33 [get_ports {led}]

You can find the location you need from the schematic of your FPGA board and marked by the switches or LEDs on your board, or you can download the master XDC for your board from the Digilent website and copy the corresponding lines for this step.

Step 4: Generate Bit File and Test it on FPGA Board

Double-click on “Generate Bitstream” to generate the bit file and download it to your FPGA board. After you program your board, you can slide on the SW0 on your board to turn on LD0.

Test Your Knowledge!

Now that you've completed this project, try these modifications:

  • In this project, we used only one switch to control one LED. You can modify the code a little bit and use all 16 switches on your board to control the 16 LEDs. To do so, you would assign SW0 to control LD0, SW1 to control LD1, and so on.
  • Now think of a way to modify your project a little bit to change which switch controls which LED. For example, make SW0 control LD6.

Challenge Problem

  • If you are confident in your ability to use switches to control LEDs, go ahead and try out the design challenge problem below for some extra practice!

  • Other product and company names mentioned herein are trademarks or trade names of their respective companies. © 2014 Digilent Inc. All rights reserved.