(ECDW) —Early Collision Detection Warning System Ultrasonic Distance Measurement Using Python Code

Story

Mapping distance like RADAR requires a rotating sensor that picks up the distance at different levels and maps into a 2D view. Just like RADAR, to rotate the UltraSonic Distance sensor, we need a servo that accurately rotates it. Once RPi2 rotates servo at a specific angle, Raspberry Pi then measures distance and plot it into a 2D view as shown in the title image of the article. But we can directly operate servo from Raspberry Pi that provides access to PWM that will communicate with Raspberry Pi via I2C bus and operates servo.

Collision Detection Warning System Ultrasonic Distance Measurement

RADAR are the most precise but are much expensive. So let’s make our own distance mapper device which acts like RADAR using UltraSonic Distance Sensor (HC-SR04) and a Servo.

Fig 1. Radar Operated PC with servo motor and Ultrasonic Sensor

In this tutorial, we will combine what we have learned before, controlling our radar position through the servo motor, as shown in the example below:

The above gif shows the radar-controlled by pre-programmed with fixed Pan/Tilt angles.

Fig 1.1 Below the block diagram of our project
Fig 1.2. Pan Tilt Angle Positions

Step 1: Used Instruments

Main parts:

Fig 2. Pan Tilt Ultrasonic Sensor Mounted on top of 4X4

What is ultrasonic?

ultrasonic sensors measure distance by using ultrasonic waves. The sensor head emits an ultrasonic wave and receives the wave reflected back from the target. Ultrasonic Sensors measure the distance to the target by measuring the time between the emission and reception.

About the HC-SR04 Distance Sensor

HC-SR04 Distance Sensor

This cheap sensor provides 2 cm to 400 cm of ultrasonic distance measurement functionality with a ranging accuracy that can reach up to 3 mm. Each HC-SR04 module includes an ultrasonic transmitter, a receiver and a control circuit.

There are only four pins that you need to worry about on the HC-SR04:

  • VCC (Power +red wire),
  • Trig (Trigger),
  • Echo (Receive), and
  • GND (Ground -black wire).

As an extra detail, if you connect Trig and Echo pins together, you can control this sensor using only one pin on the Raspberry Pi. (Really useful if you have many sensors).

Features:

  • Operating Voltage: 5V DC
  • Operating Current: 15mA
  • Measure Angle: 0–180° or -70 to +70 (Required for python code servo motor movement)
  • Ranging Distance: 2cm — 4m

How to show the relation between origin and objects?

Most have seen the RADAR screen on the television. RADAR is an object detection system that will determine the range and angle of an object. By considering the RADAR idea, we are gonna make such a view that will be able to map object distance related to an angle.

I have provided 2D map for up-to 300cm ranging between -70 to +70 degrees ahead of the sensor. You can implement 2D map of your own choice of angle and distances. Even full circular just like RADAR. Just a little bit of coding will be necessary.

Step 2: Ultrasonic Distance Measurement Using Python

Working of HC-SR04

If you needed to Enabled it, press [OK] and reboot your Pi. Make a simple test to verify if everything is OK:

Step 3: Voltage Divider

Resistors R1 and R2 are used to balance out the output as the sensor’s high signal is at +5V, whereas Raspberry pi works on +3.3V level.

Hence, the voltage divider helps in reducing the output voltage level.

voltage divider

Or R1 = 5 k

R2 = 10k

Ultrasonic ranging module HC-SR04 provides 2cm to 400cm non-contact distance measurement range. Its accuracy can reach ±3mm with a cycle period of 50ms, which is the minimum time delay recommended before measurements are taken. The module includes an ultrasonic transmitter, receiver and control circuit.

Step 4: Circuit Design for Servo motor and Sensor:

FInal Circuit Design
Breadboard block diagram of our project
RPi.GPIO Pin Design

The servos should be connected to an external 5V supply, having their data pin (in my case, their yellow wiring) connect to Raspberry Pi GPIO as below:

  • GPIO 17 ==> Tilt Servo
  • GPIO 27 ==> Pan Servo

Do not forget to connect the GNDs together ==> Raspberry Pi — Servos — External Power Supply)

You can have as an option, a resistor of 1K ohm in series, between Raspberry Pi GPIO and Server data input pin. This would protect your RPi in case of a servo problem.

in this project, I have used only GPIO 27 Pan servo for motor movement on horizontal direction and also you can use GPIO 17 for vertical movement of your choice.

Step 4.1: Speed Calculating:

Connection of HC-SR04 module with Raspberry pi

Step 5: Python code:

Only to be sure that everything is in the right location, let’s check our environment after all updates:

├── Documents
└── moz_radar
├── radar.py
├── ultrasonicsensor.py
├── colors.py
└── target.py
└── display.py

Now lets get to the software part. The ultrasonic modules operate on GPIO level and it requires no additional software to be installed.

For details and final code, please visit my GitHub repository:

Step 6: Discussion

In addition to warnings and alerts, some collision avoidance systems assist drivers in mitigating imminent risks. These systems will override the driver, changing the throttle of the vehicle or applying the brakes. The tools to inform these systems are similar to alerts, including radar, lasers, and cameras, however the response of the vehicle is more proactive. Collision avoidance features that actively assist drivers include:

With the use of radar, lasers and cameras, collision avoidance alert systems include:

  • Forward-collision Warning (FCW): Visual and/or audible warning to alert driver of collision risk. The IIHS has already observed a 27% reduction in front-to-rear crashes through this technology.
  • Blind-spot Warning (BSW): Visual and/or audible notification that a vehicle is in the driver’s blind spot in a neighboring lane; an additional warning may sound if a turn indicator is used when a vehicle is in the blind spot. IIHS studies have shown a 14% reduction in lane-change crashes and a 23% reduction in lane-change crashes with injuries in vehicles with this capability.
  • Cross Traffic Warning: A visual, audible, or haptic alert if an object is currently out of camera range, but appears to be moving into it. Studies show a 22% reduction in reverse crashes.
  • Lane Departure Warning (LDW): A visual, audible, or haptic alert that a driver is crossing lane markings. This technology has provided a 11% drop in sideswipe and head-on crashes, with a 21% reduction of injuries in the crashes of those types that occur.
Radar Fitted Vehicle

All you need for this Raspberry Pi is an Ultrasonic Sensor for detecting the objects, a small hobbyist Servo Motor for rotating the sensor, and Python code for controlling them.

UltraSonic and IrfraRed are basic fundamental sensor for measurement of distance. These sensor measures the distance in the visible sight of them with the range of 80cm to 500cm. Both sensor have their pros and cons.

--

--

--

I have been employed in the Oil and Gas Industry since five years ago and I am currently a Chartered Member of the Institution of Mechanical Engineers.

Love podcasts or audiobooks? Learn on the go with our new app.

How to create a simple QR generator in html/js with opening statistics tracking

How to Detect the Age or Gender of People in an Image using Java

The When, Why and How of the Builder Pattern

How to Validate an Email Address using Java

Updating your GoLinks Slack integration to granular permissions

Find S3 files using Python

Flutter Push Notification with Firebase Cloud Messaging (FCM) and Routing to Specific Screen

How to decrypt a PDF File and Disable its Password in C# .NET Framework

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Mozahid Sufiyan

Mozahid Sufiyan

I have been employed in the Oil and Gas Industry since five years ago and I am currently a Chartered Member of the Institution of Mechanical Engineers.

More from Medium

How to process and plot Audio data with Python

Moving average filter (C vs Python)

Image processing with Keres in Python- Part 1

String Theory of Python