The first objective of the project is to control the PW output from the sensor and accurately measure the distance and display it on the seven-segment display. Secondly, it is aimed to adjust the speed of the motor according to the distance from the sensor. In the third and last part, it is aimed to get police siren output from the buzzer when the distance is between 30-50cm.

Ultrasonic Distance Sensor - MaxSonar MB7360#

The N4 pin in the PmodD section of the expansion connectors of the Boolean Board FGPA board is set as input.

DC Motor Driver - BTS7960 43A Dual H-Bridge#

For the RPWM/LPWM/R_EN/L_EN/R_IS/L_IS inputs, which are the control pins of the motor driver board, the T6-R7-T5-R6-R6-R5-P6 pins in the PmodC section of the expansion connectors of the Boolean Board FGPA board are set as output.

12-24V DC Motor#

We connect the + and - pole of the motor to the M+ and M- inputs of the BTS7960 43A Dual H-Bridge motor driver we use for the connections of the motor. To run the motor driver, we connect the 24V-1.5A power supply to the B+ and B- inputs of the motor driver.


A BC547 Npn BJT is used to drive the buzzer. We connect the + pole of the buzzer to the emitter of the BJT. We connect the 5V we received from the Boolean board to the collector of the BJT. Finally, we connect the output pin, which we wrote the code and received from the A14 pin, to the Base of the BJT.


After all the pin settings were decided, the codes were started to be created one by one.

Distance sensor and printing distance on 7-Segment#

The Problem That I Faced and Solution#

As I explained before, the distance sensor will give us high pulse width with 300uS for a distance of 30cm. Recording this distance and converting it to decimal system was one of the problems I faced. I solved this problem by creating a clk_div by reducing the clock signal from 100-Mhz to 1Mhz. 1Mhz frequency will give us 1uS period time. If we set a counter and increase this counter by 1 on each rising edge of the 1Mhz clock signal as long as the PW output from the sensor is 1, we will have a value of 300. We will measure the distance by saving this value in a register called distance.

As the distance changes, although the duration of the PW output at logic 1 changes, we will increase the counter by 1 on each rising edge since we have a clock signal with a period of 1uS. This will give us the distance in mm.

It is clearly visible in the testbench I created in Vivado. (For the example, the high pulse width was set to 500uS.

That is 1-Mhz Clock signal called clk_div (for measuring the exact distance):

1-Mhz Clock

As long as the signal from the sensor remains at logic 1, the counter will start counting on each rising edge of clk_div and assign this value to distance. As seen in the testbench, the high pulse width of the sensor signal is set to 500uS. the time it stays at logic 1 level is 0.5ms, i.e. 500uS. the distance value was measured as 500mm.

Distance in mm

The Problem That I Faced and Solution#

The second problem I encountered was to display this value on the 7-Segment, which is built into the FPGA, in the next step. To solve this problem I decided to take the values stored in distance one by one. Suppose the distance is 1251mm. We want to see 1 value in AN0, 5 values in AN1, 2 values in AN2 and 1 value in AN1.

  • For the first digit, mod10 of the distance value gives us the first digit.

  • For the second digit, it is found by dividing the distance value by 10 and taking the mod10 value.

  • The third digit is found by dividing the distance value by 100 and taking the mod10 value.

  • The fourth digit is found by dividing the distance value by 1000.

After obtaining the digits one by one, the BCD to 7-Segment Decoder was used.


12-24V DC Motor#

After receiving the data from the sensor, the next operation will be to increase or decrease the speed of the motor according to the distance from the sensor. For this, we first create a PWM signal at a frequency of 5kHz. Our 100% Duty cycle value is 20,000. Since the motor can have current up to 4.5A during full power, we keep its maximum value at 50% Duty cycle. Duty Cycle change according to the incoming data is as follows:

  • If the measured distance is between 3.5m - 5 meters, the engine will run at 50% duty cycle.

  • If the measured distance is between 2-3.5 meters, the engine will run at 40% duty cycle.

  • If the measured distance is between 1-2 meters, the engine will run at 30% duty cycle.

  • If the measured distance is between 50cm-1meters, the engine will run at 20% duty cycle.

  • If the measured distance is between 30-50 centimeters, the engine will run at 10% duty cycle.

We can look at the testbench result to better see the change in the speed of the motor, that is, the PWM signal. I increased the distance I started as 50cm and if we look at the RPWM value representing the PWM signal of the motor, we can see that the Duty Cycle value is gradually increasing. Thus, we have automatically controlled the speed of the motor according to the distance.

Motor PWM Levels

Buzzer - Police Siren#

We start with my” tone” counter. We use only 29 bits to make it doubly as fast. Then here comes the trick to get an up-ramp. We extract bits 17 through bit 23 of the tone counter, like that: tone[23:17]. This gives us 7 bits, that go from 0 to 127 at some medium speed. After it reaches 127, it rolls back to 0 and up again. To get a down-ramp. We invert the same bits, like that: (~tone[23:17]). This gives us 7 bits again, that go down from 127 to 0. To switch between the up-ramp and the down-ramp, we use tone[24]. As soon as the up-ramp hits 127, we switch to the down-ramp, until it goes to 0, and then back to the up-ramp.

So “ramp” value goes from 7’b0000000 to 7’b1111111. To get a usable value to produce a sound, we pad it with the 2 bits ‘01’ up front, and the 6 bits ‘000000’ in the back. This way, “clkdivider” have a value ranging from 15’b010000000000000 to 15’b011111111000000, or in hex 15’h2000 to 15’h3FC0, or in decimal 8192 to 16320. With a 100MHz input clock, that produces a sound from 765Hz to 1525Hz for an high pitch siren.

We connect the speaker output we obtained to the base of the BJT in the buzzer circuit we built earlier by outputting from the A14 pin. The desired sound is attached below.


As explained earlier in the introduction, this project was inspired by new generation cars. Today, cars can adjust the required engine speed by measuring the distance between them and the car in front of them. In this direction, it was desired to make the same task more adapted to the project.

In addition, while driving, in some cases, unexpected sudden braking of the car in front may occur even if it is not the fault of the driver. In this project, it is planned to automatically change the speed of the car’s engine both in cases where the driver is careless and by taking the car in front as a reference.

Future Improvement#

Many things can be added to improve this project:

1- Object detection can be done using a camera. While the sensor measures the distance, it can be determined whether the object encountered from the camera is a human, a car or a motorcycle. 2- Location tracking can be done using the GPS plugin. 3- A system can be created that will instantly send an SMS to the ambulance system if a collision occurs by using a more sensitive sensor.


I had done certain projects with STM32F407 microcontroller using certain components before, but I did not know how to use these components with FPGA. I learned to realize these in the project by using the topologies in the digital circuit design that I learned in the course. For example, how to create a pwm signal to control the motor, how to get data from a distance sensor, how to control it using a 7-segment display decoder. In my opinion, while designing digital circuits with FPGA, since coding is done at the logic level, there is a chance to reach everything easily, even if it is more difficult during the project. This is a great freedom for the designer.