Motion Sensor/Counter Controlled Lights

Introduction: Motion Sensor/Counter Controlled Lights

This project was created as a final project for a Digital Design course at Cal Poly, San Luis Obispo (CPE 133).

Why are we doing this?
We want to help conserve natural resources in the world. Our project focus on saving electricity. By saving more electricity, we will be able to conserve the natural resources that are used to generate electricity. As we begin 2018, natural resources are being consumed at an incredible rate. We want to be conscious of our impact on our environment and play our part in preserving natural resources. Electronics can be implemented in various ways to save energy which helps the environment as well as our economic state.
*This model was created using the components available to us.

What was our inspiration?
People often forget to turn off their holiday lights, and wasted energy by leaving them on overnight. In reality, this project will conserve electricity because the “holiday lights” would only be on when people are nearby, thus conserving energy when no one is around. Moreover, we wanted to design a timer so the lights would turn off completely after a given time to ensure that they don't turn on due to movement detected at 3 a.m., for instance.

How could you use this design?
This design can be implemented for all sorts of lights, whether they are decorative, practical, or both. If you want your desk light to only function for 6 hours at a time, for example. You would need to set a counter to 21,600 seconds (6 hours x 3,600 seconds/hour). While the counter is actively increasing, the motion sensor would control the light. Thus, every time it turns off during that span of time, you just need to wave your hand in front of the motion sensor and it will turn back on. If you fall asleep at your desk and wake up 7 hours later, your movement will not turn it on.

Step 1: Required Software and Hardware


  • Vivado 2016.2 (or a more recent version) can be found here
  • Arduino IDE 1.8.3 (or a more recent version) can be found here


  • 1 Basys 3 board
  • 1 Arduino Uno
  • 2 Breadboards
  • 1 Ultrasonic Ranging sensor HC-SR04
  • 9 Male-to-male wires
  • 1 LED
  • 1 100Ω Resistor

Step 2: Codes (Vivado)

Finite State Machine (see state diagram above):

The LED required a finite state machine. An LED has only two states of being on and off. Only two inputs control the state of the LED, the counter and the sensor. The only time the LED should be on is when the sensor detects movement and when the counter is counting from zero to thirty seconds. Any other case the LED will be off.

File name: LEDDES


The counter allows us to limit the length of time during which the motion sensor can activate the LED. Its value is displayed on the Basys 3 Board’s seven segment display through a source code (“sseg_dec”).
When the Reset switch is down (value: '0'), the counter begins increasing every second from 0 to 30. When it reaches 30, it freezes on that number. It will not restart from 0 until the Reset switch is toggled to ‘1’ and back to ‘1.’ If Reset becomes ‘1’ while the counter is going, the counter will freeze on whatever value it had reached. When Reset goes back to ‘0,’ the counter will restart from 0 to 30.
This implementation also requires the use of a clock signal, its code is provided below ("clk_div2").

File name: FinalCounter


Seven Segment Display:

This code allows the seven segment display to display decimal values. One submodule acts as decoder between an 8-bit binary input an a 4-bit Binary Coded Decimal. The other divides the clock signal to refresh its value at a certain rate.

File name: sseg_dec

Clock Signal:

This code allows the counter to increase in increments of 1 second. It divides the input clock frequency into a slower frequency. We adapted to provide a period of 1 second by change constant max_count : integer := (3000000)” to “constant max_count: integer := (50000000).”

File name: clk_div2

Provided files: sseg_dec, clk_div2
*These source files were provided by Professor Bryan Mealy.

Step 3: Understanding How They Come Together (Schematics of VHDL Components)

The main file ("MainProjectDES") contains all the subfiles discussed previously. They are connected in the above manner. The different components are interconnected using port maps to send a signal from one element to another.

As you may have noticed, the FinalCounter provides a 5-bit output while sseg_dec requires an 8-bit input. To compensate, we set the signal connecting both components to begin with "000" and add-on the 5-bit output from the counter. Thus providing an 8-bit input.


In order to run these codes on the Basys 3 Board, a constraints file was required, telling each signal where to go and how the parts were connected.

Step 4: Code (Arduino)

We programmed the Arduino Uno to use the motion sensor to detect movement and provide an output that signals the LED to light up. Additionally, using the sensor to detect motion requires running loops that constantly look for change in distance. Essentially, it needs a timer that runs concurrently to output a “high” signal for the LED to light up while the timer need to be reset once there is new motion detected, which is almost impossible to implement on Vivado based on the scope of knowledge of the class. Moreover, we used an Arduino because it would not be feasible to use HC-SR04 with the Basys 3 Board since the board only supplies 3.3V while the sensor requires a 5V power supply. For the implementation of the detecting movement, it’s actual coding as opposed to CAD in VHDL.

We used the pulse-in built in function for the sensor to retrieve the time passed between the sound initially emitted from the sensor and the sound that bounces back when hitting an object. Then we use the speed of sound and time interval to calculate the distance between the object and the sensor. From that, we store the current distance and keep track of it. We check the distance every 150ms.
We also used the elapsedmil library to run an internal timer inside the arduino to keep track of time elapsed. If we detect a distance change, which corresponds to a motion, the timer is reset to zero and it will keep the light on until 3 seconds have passed. Whenever the sensor detects another movement, the timer is reset to 0 and signal for the LED light will be "high" for the next 3 seconds.We have attached a copy of our Arduino code below.

Step 5: How Our Components Fit Together!

As you can see in the "Basys3: Pmod Pin-out Diagram*" and the photo of the Arduino Uno Board, we highlighted and labeled the ports we used.

1. The LED and Basys 3 Board

The LED is connected in series with the 100Ω resistor.
-The white wire connects the resistor to pin PWR of the Basys 3 board.
-The yellow wire connects the LED to pin H1 of the Basys 3 board.

2. The Motion Sensor and the Arduino Uno

-The orange wire connects Vcc (power) of the motion sensor to pin 5V of the Arduino Uno board.
-The white wire connects pin Trig of the motion sensor to pin 10 of the Arduino Uno board.
-The yellow wire connects pin Echo of the motion sensor to pin 9 of the Arduino Uno board.
-The black wire connects pin GND of the motion sensor to pin GND of the Arduino Uno board.

[The wires we used were too short to reach the components, thus they were interconnected]

3. The Basys 3 Board and the Arduino Uno

The yellow wire connects pin A14 of the Basys 3 board to pin 6 of the Arduino Uno board.

*This diagram was taken from Digilent's "Basys 3™ FPGA Board Reference Manual" which can be found here.

Step 6: Demonstration

Step 7: Time to Test It Out!

Congratulations! You have made it to the end of our motion sensor & counter controlled light project! Thank you very much for reading through our Instructables post. Now it is time for you to try to build this project yourself. If you follow every step carefully, you should have a motion sensor & counter controlled light that works similar to ours! We wish you the best of luck in building this project, and hope it can contribute to saving electricity as well as natural resources!

Be the First to Share


    • Make It Bridge

      Make It Bridge
    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • For the Home Contest

      For the Home Contest