Develop a parking sensor module using an ultrasonic range finder.


You will develop a parking sensor module in 5 stages, starting with a simple version and gradually adding functionality. This is always a good approach to a complicated problem. Save each version in a separate file: you can then revert to a previous (functioning) version if you get stuck. You should submit (see below) the programs up to the highest version that you have reached during the lab (not everyone will complete the highest versions).

In addition to the programme, each student must submit a short individual report on the final version of the program. The report must not exceed 2 pages and should contain:

  • your name and student number
  • a summary of the functionality of your program (i.e. what it does),
  • a description of how the program operates, based on a block diagram or flowchart,
  • comments on the program, such as any shortcomings and ways in which it might be improved.

Parking sensor module

Objectives and specification

You are asked to develop a microcontroller based system for a parking sensor module.

The system will use an ultrasound unit to detect the distance to an object. The parking module should:

  • Detect the distance to an obstacle.
  • Display the distance on an LCD display
  • Provide a visual indication of the distance detected by using differently coloured LEDs to indicate different distance ranges (far, medium, close).
  • Use a buzzer to give an auditory indication of the distance.

The final system might use a button to turn it on and off.

The versions below suggest a successive implementation, adding features as you go along. Each version corresponds to one item in the specification above.


To setup the IDE follow the instructions given in Lab 1. Remember to select the correct board (Arduino Due [Programming Port]) and port under the Tools menu.

For this lab, the setup consists of the Arduino board and a breadboard with the same components as in Lab 1 (multicolour LED connected to pins 3, 4 and 5, a pushbutton connected to pin 2 (note this is different from lab 1!), and a LCD panel connected to pins 49, 47, 45, 35, 33, 31 and 29). The following components have been added:

  • An ultrasonic range module (HC-SR04) to detect the distance to an object. The module is connected to pins 8 and 9.
  • A buzzer for audio output (connected to pin 10).

The overall setup is shown in Figure 2 below.

Ultrasonic range module

The ultrasonic range module provides non-contact measurement function between 2cm - 400cm. The ranging accuracy can reach 3mm. The module includes ultrasonic transmitter, receiver and the control circuit, and requires a power supply of 5V. The basic principle of operation is as follows (see timing diagram in Figure 1 below):

  1. A pulse of at least 10µs duration applied at the trigger port, initiates the measurement.
  2. The Module automatically sends eight ultrasonic pulses at 40 kHz and detects whether the echo signal.
  3. If an echo signal is detected, a corresponding pulse of the duration of the echo is delivered at the echo port. If no echo signal is detected within the range (<400cm) then a pulse of that duration is returned.


  • When implementing your programme, remember that you need to setup the pins you are going to use as inputs or as outputs, and initialise any variables, so you start with a defined state!
  • You should have a look at what you did in Lab 1, and reuse any code from those programmes where appropriate.

Version 1:

In this version you should implement the timing diagram shown in Figure 1 in order to obtain the reading from the ultrasound module. This needs to be done in two steps:

  1. You need to provide the trigger signal to initiate a reading from the module. For this you need to provide a pulse of 10µs duration on pin 8 (which is connected to the trigger input of the module). Use calls to the digitalWrite() function to first set the pin to HIGH and then to LOW. To make sure that the pulse it 10µs long, you can use the function delayMicroseconds() in between the calls to digitalWrite().
  2. You then need to detect the pulse on the echo pin and measure its duration which tells you how long the echo took: You first need to detect that the echo pin (pin 9) goes HIGH, and then measure the time until it goes LOW again. To implement this you can use “do-while”-loops, similar to the code for “waiting for a button press” which you implemented in Lab 1.

In Lab 1 you used a timed loop to measure the time it took to press the button. Here you need to measure the duration of the pulse, but this is much shorter than in Lab 1 (usually < 1ms). You could use the function delayMicroseconds() but the loop timing will not be very accurate (due to the overhead of the other function calls you need). A more precise way to measure the duration is to use the function micros() (some of you have used millis() in Lab 1 and will be familiar with this approach). The idea is to use micros() to measure the time when the start of the pulse has been detected and to assign this to a variable. When the end of the pulse is detected, you can call micros() again and substract the start time. The difference is the time of the pulse.

To see what’s going on you should then display the pulse time on the Serial Monitor (using Serial.print(), like in Lab 1).

You then need to convert the pulsewidth detected to a distance. You can assume that the speed of sound is about 340m/s. Remember that the echo-time corresponds to twice the distance to the obstacle, because the sound has to travel there and its reflection back.

Display the result as a distance (in millimetre) on the Serial Monitor. Check its accuracy using an obstacle at a known distance. (To set a known distance you can use the dimensions of this A4 page (210mmx297mm)).

Version 2:

In this version you should display the distance detected on the LCD display instead of the Serial Monitor. Use the code from Lab 1 for this!

Version 3:

It would be good to have a visual indication of the distance range. Use the multicolour LED module to display different colour depending on the range detected. For example, you could use green if the distance is >300mm, blue for distances between 100mm and 300mm, and red for a distance below 100mm.

Version 4:

In addition to the visual indication it would also be helpful to have an auditory signal which corresponds to the distance. Use the buzzer for this!

The buzzer is “active” which means it emits a beeping sound when its input is HIGH and mutes when its input is LOW. One way to generate a sound which is related to the distance measurement is to emit brief beeps at an interval which is related to the distance (similar to a parking sensor in a car). Using the distance measured in millimetres as the delay between beeps in milliseconds works quite well in this setup.

Think about how this code will behave for long distances, or when no echo is detected. Specifically, how will the delay() function affect your programme in these cases? How can you mitigate the resulting problems?

Version 5:

At the moment the system is always on. You should use the button (remember to configure it INPUT_PULLUP, which makes it “active low”) to turn the parking module off and on.

As a first step you can implement this in such a way that the parking sensor is on while the button is pressed, and off when the button is not pressed.

You can also try to use a button press to turn the system on, and another to turn the system off. (You will find that this version can be a bit challenging.)

More extensions (optional):

  • You could use the 2nd line of the LCD panel to show a bar graph which indicates the distance.
  • You could also include some averaging to avoid quickly changing values.

on the way to be a Electrical Engineer & Designer......