UltraSonic Sensor Tutorial #1: How to Use HR-SR04 Sensor

Picture of Tinee9: DIY Drone Avoidance Sensor

Tinee9 is back with another quick tutorial. This tutorial will talk about the HC-SR04 Ultrasonic sensor, how to use it with arduino, and how it could be used for Drone obstacle avoidance and landing.

Tutorial Difficulty: Beginner

Step 1: Drones and Environment Sensing

Picture of Drones and Environment Sensing
Picture of Drones and Environment Sensing
Picture of Drones and Environment Sensing

DJI Mavic Air has 6 visual sensor and 2 downward infrared sensor. The sensors’ information combined, allow the Air to sufficiently detect and avoid obstacles in nearly every direction. There are 2 cameras in front, 2 on the bottom, and 2 in the rear of the drone. These cameras take in the information around, which then the computer on board stitches the pieces together and creates a 3D environment. This environment is then dissected in software to determine what is an obstacle and how far way it is. When the camera looking downward does not have enough information, the infrared sensor facing down ward sends out a infrared signal and then receives it in the other channel. This allows the drone to land because it gives the information of how far away the drone is from the ground.

Step 2: DIY Drone Avoidance Sensor

Picture of DIY Drone Avoidance Sensor
Picture of DIY Drone Avoidance Sensor

Since the DJI camera sensors are lot more complicated and probably cost more money, so we will use a different sensor. The sensor we will use today is the HC-SR04 Ultrasonic Sensor. This sensor sends out an sound wave that we can’t hear but maybe dogs can and waits until the sound comes back to the receiving sensor on board. Once the receiving sensor on board receives the signal, the computer makes a quick calculation of how far away an object is. This calculation is based on the speed of sound and the time it took to get back.

In the picture on the left above, this is an example of how the sound wave leaves one sensor, hits the ruler that I am holding, and then bounces back to the receiving sensor.

So how would this work on a self made drone, well if we put enough of these on we would be able to determine objects above, behind, below, and in front of us at all times. And if the sensors had enough resolution and range we could possibly and more accurately hold a position in air more accurately than a GPS signal.

Step 3: Materials

Picture of Materials
Picture of Materials

Materials list is pretty simple:

1x Arduino Nano

4x Male to Male Jumpers

1x HC-SR04 Ultrasonic Sensor

1x Breadboard

1x USB to Mini USB cable

1x PC with Arduino IDE

You can collect all materials (except PC and USB cable) in the link here. Link

Step 4: Connection

Picture of Connection
Picture of Connection

1. Place the Arduino on the breadboard (This is saying you have headers on your Arduino already)

2. Place HC-SR04 Sensor facing out of the Breadboard like above

3. Connect a Jumper from Arduino D13 Pin to HC-SR04 Trigger Pin

4. Connect a Male to Male Jumper from Arduino D13 Pin to HC-SR04 Trigger Pin

5. Connect a Male to Male Jumper from Arduino D12 Pin to HC-SR04 Echo Pin

6. Connect a Male to Male Jumper from Arduino +5V Pin to HC-SR04 VCC Pin

7. Connect a Male to Male Jumper from Arduino GND Pin to HC-SR04 GND Pin

8. Connect USB to USB mini from Computer USB to Arduino Nano USB Mini

Step 5: Load Code and Test

IMG_20180619_225659.jpg
Ultrasonic sensor

Turn On your PC and load up your Arduino IDE. If you do not have Arduino IDE please visit Arduino.org and follow their instructions.

Paste the Code Below into your Arduino IDE and Program. Once you have programmed and place your hand or an object in front of the sensor, you should see the arduino monitor show the distance changing as the object moves closer or father away.

By learning how the code below work you can then manipulate it for your need for an RC car or drone that avoids objects or even an automatic doorbell that has no button.

#define trigPin 13

#define echoPin 12

#define led 11

#define led2 10

void setup() { Serial.begin (9600); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); pinMode(led, OUTPUT); pinMode(led2, OUTPUT); }

void loop() { long duration, distance; digitalWrite(trigPin, LOW); // Added this line delayMicroseconds(2); // Added this line digitalWrite(trigPin, HIGH); // delayMicroseconds(1000); – Removed this line delayMicroseconds(10); // Added this line digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); distance = (duration/2) / 29.1; if (distance < 4) { // This is where the LED On/Off happens digitalWrite(led,HIGH); // When the Red condition is met, the Green LED should turn off digitalWrite(led2,LOW); } else { digitalWrite(led,LOW); digitalWrite(led2,HIGH); } if (distance >= 200 || distance <= 0){ Serial.println(“Out of range”); } else { Serial.print(distance); Serial.println(” cm”); } delay(500); }

Below is a video of someone performing an obstacle avoidance test for the DJI Mavic Air

Website Donation

To help support my website please donate so that I can continue to educate, enlighten, and inspire people to invent and make the world a better place through technology.

$1.00

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s