Warning: Declaration of Db::query($query) should be compatible with mysqli::query($query, $resultmode = NULL) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 29

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Deprecated: Function get_magic_quotes_runtime() is deprecated in /var/www/u1300519/data/www/acoptex.com/_lib/CacheLite/Lite.php on line 757

Warning: Cannot modify header information - headers already sent by (output started at /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php:0) in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 168

Warning: session_start(): Cannot start session when headers already sent in /var/www/u1300519/data/www/acoptex.com/_config/config.php on line 169

Warning: Use of undefined constant MYSQL_NUM - assumed 'MYSQL_NUM' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 92

Warning: Use of undefined constant MYSQL_ASSOC - assumed 'MYSQL_ASSOC' (this will throw an Error in a future version of PHP) in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64

Warning: mysqli_fetch_array() expects parameter 2 to be int, string given in /var/www/u1300519/data/www/acoptex.com/_lib/class.Db.php on line 64
jobs.html_title
0

members

Easy Basics: Project 009f Ultrasonic Sensor HC-SR04 - Ultrasonic radar

of Acoptex.com in UNO

Basics: Project 009f

Project name: Ultrasonic Sensor HC-SR04 - Ultrasonic radar

Tags: Arduino, Arduino Uno, Ultrasonic Sensor, HC-SR04, arduino radar, ultrasonic radar, Processing

Attachments: arduinosketch1, processingsketch1, processingsketch2

In this project, you needed these parts :

1.Aruduino Uno R3 (you can also use the other version of Arduino)

2.Ultrasonic Sensor HC-SR04 module

3.Arduino IDE (you can download it from here) and Processing IDE (you can download it from here)

4.Jumper cables M-M, F-M

5. DC 5V MG91 or SG90 Micro Servo Motor 1 pc

6.Ultrasonic sensor mount bracket 1 pc

7. Capacitor 1 pc (100 uF) Optional

General

In this project we will learn how to connect HC-SR04 Ultrasonic Module and DC 5V MG91 or SG90 Micro Servo Motor to Arduino board and make ultrasonic radar system.

Understanding the HC-SR04 Ultrasonic Module

The HC-SR04 Ultrasonic Sensor is a very affordable proximity/distance sensor that has been used mainly for object avoidance in various robotics projects. It essentially gives your Arduino eyes / spacial awareness and can prevent your robot from crashing or falling off a table. 

The HC-SR04 ultrasonic sensor uses sonar to determine distance to an object like bats do. It offers excellent non-contact range detection with high accuracy and stable readings in an easy-to-use package. From 2cm to 400 cm or 1” to 13 feet. It operation is not affected by sunlight or black material like Sharp rangefinders are (although acoustically soft materials like cloth can be difficult to detect). It comes complete with ultrasonic transmitter and receiver module.

Features:

  • Power Supply :+5V
  • DCQuiescent Current : <2mA
  • Working Current: 15mA
  • Effectual Angle: <15°
  • Ranging Distance : 2cm – 400 cm/1″ – 13ft
  • Resolution : 0.3 cm
  • Measuring Angle: 30 degrees
  • Trigger Input Pulse width: 10uS
  • Dimension: 45mm x 20mm x 15mm

Ultrasonic Sensor emits an ultrasound at 40 000 Hz which travels through the air and if there is an object or obstacle on its path It will bounce back to the module. Considering the travel time and the speed of the sound you can calculate the distance.The HC-SR04 Ultrasonic Module has 4 pins, Ground, VCC, Trig and Echo. The Ground and the VCC pins of the module needs to be connected to the Ground and the 5 volts pins on the Arduino Board respectively and the trig and echo pins to any Digital I/O pin on the Arduino Board.In order to generate the ultrasound you need to set the Trig on a High State for 10 µs. That will send out an 8 cycle sonic burst which will travel at the speed sound and it will be received in the Echo pin. The Echo pin will output the time in microseconds the sound wave traveled.For example, if the object is 10 cm away from the sensor, and the speed of the sound is 340 m/s or 0.034 cm/µs the sound wave will need to travel about 294 u seconds. But what you will get from the Echo pin will be double that number because the sound wave needs to travel forward and bounce backward.  So in order to get the distance in cm we need to multiply the received travel time value from the echo pin by 0.034 and divide it by 2.

 

You can see datasheet here.

Understanding the servo motor

You can read more about it here.

When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100uf capacitor across power and ground right next to the male headers you can smooth out any voltage changes that may occur. This capacitor called decoupling capacitor because its reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that’s the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode.

Signals and connections of HC-SR04 Ultrasonic Module

"Trig" stands for Trigger Pulse Input

"Echo" stands for Echo Pulse Output

"VCC" stands for power supply (+5V)

"GND" stands for GND (0V)

Duration for the sound to reach echo is twice the duration required to reach the object that will create echo. That is if you shout in front of a wall the voice to reaches the wall and returns back to your ear. The moment you hear the sound, the voice has traveled twice the distance between you and the wall. Thus the time taken will be twice as the speed of sound is constant. Hence the following formula:

d = v/t;

therefore d = 29 / (time in miliseconds / 2).

or d = 340 / (time in seconds / 2).

Signals and connections of servo motor

Wiring

The following picture shows the needed connections with the Arduino Uno 

When a servo motor starts to move, it draws more current than if it were already in motion. This will cause a dip in the voltage on your board. By placing a 100uf capacitor across power and ground right next to the male headers you can smooth out any voltage changes that may occur. This capacitor called decoupling capacitor because its reduce, or decouple, changes caused by the components from the rest of the circuit. Be very careful to make sure you are connecting the cathode to ground (that’s the side with a black stripe down the side) and the anode to power. If you put the capacitors in backwards, they can explode.

Assemble ultrasonic sensor and servo motor as it shown below:

Code

1. Arduinosketch1.

With this sketch we will be able to receive the values for the angle and the distance measured by the sensor from the Arduino Board. This values sent to Serial monitor. The code is simple and well explained.

2. Processingsketch1.

First we will use the SerialEvent() function to read the data from the Serial Port and we will put the values of the angle and the distance into the variables iAngle and iDistance , both of them will be used later to draw the radar, lines, detected objects and so on. 

Function drawRadar() used to draw the radar, consists of arc() and line() functions.

Function drawLine() used to draw the line that is moving along the radar. Its center of rotation is set with the translate() function and using the line() function in which the iAngle variable is used the line is redrawn for each degree.

Function drawObject() used to draw detected objects. It gets the distance from ultrasonic sensor, transforms it into pixels and in combination with the angle of the sensor draws the object on the radar.

Function drawText() used to draw the text on the screen in particular locations.

All of functions mentioned above called in the main function draw(), which repeats all the time and draws the screen. Function fill() with 2 parameters used for simulating motion blur and slow fade of the moving line.

This code was used from Arduino Radar Project by Dejan Nedelkovski.

3. Processingsketch2.

New Updated version of the code to fit any screen resolution. Just change the values in function size() to your screen resolution size.

Step by Step instruction

  1. Open Arduino IDE.
  2. Plug your Adruino Uno board into your PC and select the correct board and com port
  3. Open up serial monitor and set your baud to 9600 baud
  4. Verify and upload the arduinosketch1 to your Adruino Uno
  5. Download and install the Processing IDE software, you will need it to view the 3D representation of the data from the ultrasonic sensor. Processing is similar to Arduino, except for a couple of functions and is mainly used for visualizing data and rendering it in 2D/3D models.
  6. Open Processing IDE.
  7. Go File, then select Open. Navigate to the folder where you saved the files: processingsketch1ultrasonicradar.pde or processingsketch2ultrasonicradar.pde.
  8. Press Open button.
  9. You need to check the serial port which is defined in this code - by default it set to COM4. Replace “COM4” with the COM port where your Arduino is connected (check this by going into Arduino IDE -> Tools –> Port -> Serial Port).
  10. Run the processing sketch code (processingsketch1ultrasonicradar.pde or processingsketch2ultrasonicradar.pde) by pressing the button with the “play” symbol.
  11. The interaction between the Arduino Uno and the Processing IDE will be enebled and you will see the visualization of ultrasonic sensor data.

Summary

We have learnt how to connect HC-SR04 Ultrasonic Module and DC 5V MG91 or SG90 Micro Servo Motor to Arduino board and make ultrasonic radar system.

Libraries:

  • Servo library is included in your installed Arduino IDE.

Sketch:

  • See attachment on the begining of this project description.


Other projects of Acoptex.com
Medium Basics: Project 083w Sipeed Maixduino board - Using PlatformIO IDE of Acoptex.com in Sipeed Maixduino 08-08-2019
Medium Basics: Project 083e Sipeed Maixduino board - Uploading MaixPy of Acoptex.com in Sipeed Maixduino 04-08-2019
Medium Basics: Project 083f Sipeed Maixduino board - Using MycroPython of Acoptex.com in Sipeed Maixduino 04-08-2019

jobs.published_at
jobs.viewed