Introduction: SmartSwitch Light Switch

Have you ever unintentionally left the lights on as you leave the room? Are you interested in how simple projects using a Field Programmable Gate Array or FPGA can improve your life and conserve resources? Well this is the project for you. This Instructable will walk you through how to create a prototype FPGA system that will track the number of people in a room and turn the lights on and off accordingly.

Credits to:

1. Michael Djaja, Ian Brown, William Le, and Christian Cooper for the development of this project

2. Digilent Inc for the Basys3 Master XDC File for Vivado designs obtained from

3. Professor Bryan Mealy for the clock_div2 file

4. Light Bulb cover image picture from

5. Xlinix for the Vivado software which can be obtained from

6. Digilent Inc for Pmod.png in step 7 taken from

Additional thanks to:

1. Jacob Labog

2. Cal Poly Robotics Club

This project was done for a Digital Design final project for California Polytechnic State University, San Luis Obispo.

Step 1: Materials

For this project you will need the following materials:

1. The Vivado Software which will create the VHSIC Hardware Description Language or VHDL that runs the FPGA, other software can be used, but it will make our source code significantly less useful.

2. Basys 3 FPGA board

3. 2 Infrared Leds from RadioShack that come with photodiodes

(part number 276-0142)

4. 39 ohm resistor for the infrared Leds

5. Variety of LEDs

6. 2 Infrared sensors (part number VS1383B)

Step 2: Creating the Counter

The cornerstone of the projects VHDL will be the up/down counter that tracks the number of people in the room. This counter can be designed to count up to a number of your choosing, but we designed ours to count up to 512, as we felt that it would be unlikely that more than 512 people would be in the room simultaneously. In addition the counter should not count down below 0 people. The inputs of the counter will be the clock signal, and a select signal. The select signal will be the output of the FSM we will build in the next step. The output of the counter will just be the number of people in the room.

The counter keeps track of both the current count and a value, called select, that determines how the count will change with the next clock cycle. Whether the counter goes up or down will be dictated by the select signal. We set up the count_next signal equal to the count_reg + 1 or count_reg -1, depending on the select signal. In addition, the counter can also be reset to 1 or 0 in order to manually turn the light on or off.

Remember to take into account what input device you will be using to control the select signal. For the purposes of this tutorial, we used a 3 way switch, allowing us to switch between setting the count to zero, one, or allowing the automatic detection system to decide.

Step 3: Creating the FSM (Finate State Machine)

This module will interpret data from the sensors in order to decide the select signal used by the counter. The states are as follows: Idle, going out, coming in, add, and subtract. When the outside sensor is activated, the state transitions to "coming in" in anticipation of the inside sensor being activated. When the inside sensor is activated from there the state goes to "add".The "going out" state transitions work the same way, just with the sensors flipped. These states will show whether to add or subtract the number of people. Consult the state diagram if you have any other questions about the state transitions.


Step 4: Creating the Comparator

A comparator that was made beforehand was used to compare a 9 bit value that will check whether or not the count is greater than or less than zero. This will determine whether to turn on or off the light. .

Step 5: Combining the Counter, FSM and Comparator

The goal of the this step is to put together the counter, FSM, and comparator. Keep in mind that if the FSM and counter were properly designed to each other they should work together. The outputs of the FSM will be interpreted by the counter. Also connect the outputs of each module to the appropriate inputs as per the block diagram. We used the outputs of our FSM (A and S) in our counter, so whenever the switch is set to Auto the counter uses A and S to decide what count_next will be.

Step 6: Interpreting Sensor Input

The infrared LEDs used in this project came for a pack from RadioShack which included a photodiode.

To tell which is which in the pack, the LEDs are the tinted diodes, while the photodiodes are not tinted. The photodiodes were supposed to be able to pick up infrared light, but we found that that the voltage difference between infrared light being picked up and infrared light not being picked up was too small. So, we used some VS1838B infrared receivers, which were from the Cal Poly Robotics Club. The VS1838B's had to be given a particular infrared signal in order to be activated. In particular, the receivers requires a 38kHz signal that was on for 600 microseconds, then off for 900 microseconds in order to be activated. The code for the 38kHz signal is given in clock_div2. The code for setting a high for 600 microseconds, then low for 900 microseconds is given in clock_div3.

The VS1838B receiver output a high signal when idle, then a modulated frequency after something had run across the the receiver. In order to smooth out that modulated frequency, Smooth_Rec was written.

Step 7: Debugging

First, check your wiring. In this project, the LEDs and receivers failed at multiple instances to work due to bad connections and forgetting to ground the LEDs and or receivers. It's the most tedious to fix, but more often than not, it's probably the source of the problem.

In order to test to see if the VS1838B's are working use some sort of remote control since they give off the same signal that the VS1838B detects.

In order to test to see if the LEDs are working, you can use an old smartphone that has a camera without an infrared filter in order to see the infrared light. Then, to check for the correct signal use an oscilloscope. If you don't have an oscilloscope, verify that the VS1838B's are working with a remote control. Then, assuming the code for the correct signal has been uploaded, try swinging the LED across the receiver a couple times to see if the receiver is picking anything up.

Step 8: Building the Hardware

In order to wire up the sensors to the board, use the Pmod pinouts provided on the Basys 3 board. The Pmod schematics are attached as an image. Make sure to follow the picture. Also, put the leds slightly in front of the receivers, since the range at which they emit is 80 degrees from center to both sides. The receivers are capable of receiving an infrared signal from 20 meters away and 45 degrees from center in both directions, according to the data sheet. Note this is a prototype. Reference the Basys3_Master file in order to find out the connections between the board and the wiring.

Step 9: Putting Everything Together

Program the basys 3 board that is hooked up to the sensors and receivers with the Final_Final VHDL. Test out the prototype and let us know if you have any suggestions that could improve this project.