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

Radar and the DJI Farming Drone


Drones are become widely used outside of the consumer industry. One of the branches of drone use is in farming. How are drones used? Well, they can be used for crop degradation detection, water dispersion observation, and crop dusting just to name a few. DJI AGRS MG-1 drone performs crop dusting. To see DJI’s farming page click here.

cropdusting
Credit: DJI.com

Why use drones vs other means of crop dusting? A couple of answers:

  1. Drones are generally quick and easy to set up vs a tractor, or a plane.
  2. Drones are cheaper than a tractor and plane in the long run.
  3. One does not need a hard and expensive flying lesson to fly a drone unlike a plane.
  4. Drones can perform more even dusting of fertilizer and chemicals
  5. Drones will help improve land utilization since a drone may fly over vs a tractor driving through a field.
agras
Credit: DJI.com

So how is a drone more efficient at spraying chemicals than a plane or someone manually? Sensors….But not just any sensors, it has radar and most likely a liquid management software system.

How does radar work?

Radar more or less is pretty simple to understand but slightly harder to implement. Radar frequency signal, like sound but typically not audible to the human ear, is produced by an antenna or speaker. This signal will travel until it hits an object in which the signal will then bounce off and return to the antenna or receiver speaker. The time from when the signal was produced to when the signal is received is calculated. That calculation is then used to determine the distance the object is from the radar sensor and in DJI’s case, it’s drone.

radar
Credit: Georg Wiora (Dr. Schorsch) – Self drawn with Inkscape 

DJI uses microwave radar and gets centimeter precision which means the frequency that is used to produce the signal is in the range of 3-30GHz or 1-10cm. Kind of a broad range but that is better than the 100Mhz to 100 Ghz that Microwave frequency is quantified as.

Having a drone with radar, getting centimeter precision, the ability to fly at the optimum height to evenly spread chemicals to crops, and perform the task quicker than manual workers is a win win for farmers trying to bring their crop yields up and costs down.

As stated, this is only one subset of what drones are used outside of the consumer world to better the world. If you have an idea or have created an idea that will better the world through drones or some other invention and you may be featured here on the site. Please send me an email @  Addcalmtoengineering@gmail.com   with your idea or invention.

Disclaimer: This blog is solely Tinee9’s opinions through electrical engineering experience and does not have exact knowledge of the inner workings of DJI products that would compromise IP, or patent infringement. DJI is not a sponsor of Tinee9 but Tinee9 is apart of DJI affiliate program to help earn revenue to keep the website going.