• Home
  • IOT
  • Articles
  • IoT project of Sonar system using Ultrasonic Sensor HC-SR04 and Arduino device

IoT project of Sonar system using Ultrasonic Sensor HC-SR04 and Arduino device

Last updated on May 30 2022
Sankalp Agarwal

Table of Contents

IoT project of Sonar system using Ultrasonic Sensor HC-SR04 and Arduino device

Let’s build an IoT project of sonar system using Ultrasonic HC-SR04 device and Arduino (Arduino UNO). The sonar system detects the objects that come within its range (angle and distance) and display its appearance on Laptop (Monitor) screen. Sonar uses the echo principle of sound waves through an object.

Hardware requirements

  1. Arduino UNO board
  2. USB cable connector for Arduino UNO
  3. Ultra Sonic HC-SR04
  4. Jumper wires male to female
  5. Micro Server SG90

Software requirements

  1. Arduino software
  2. Processing software

The working rule of the Sonar system

The Ultra Sonic HC-SR04 emits ultrasound at 40,000Hz that travels within the air. If there’s an object or obstacle comes in its path, then the acoustic wave collides with the thing and bounces back to the Ultra Sonic module. The object’s angle and distance have displayed over the screen (Monitor).

In this project, we use the processing app to display the sonar range.

Before writing a program for Sonar system, first undergo Ultrasonic Sensor HC-SR04 and Arduino to distance calculationwhere we offer the working rule of Ultrasonic device.

Write an Arduino program to live distance using Ultra Sonic HC-SR04 and rotate servo motor.

  1. #include
  2. const int trigPin = 8;
  3. const int echoPin = 9;
  4. long duration; //declare time duration
  5. int distance; //declare distance
  6. Servo myServo; // Object servo
  7. void setup() {
  8. pinMode(trigPin, OUTPUT); // trigPin as an output
  9. pinMode(echoPin, INPUT); // echoPin as an input
  10. Serial.begin(9600);
  11. myServo.attach(10); // pin connected to Servo
  12. }
  13. void loop() {
  14. // rotating servo i++ depicts increment of 1 degree
  15. for(int i=0;i0;i–){
  16. myServo.write(i);
  17. delay(30);
  18. distance = calculateDistance();
  19. Serial.print(i);
  20. Serial.print(“,”);
  21. Serial.print(distance);
  22. Serial.print(“.”);
  23. }
  24. }
  25. int calculateDistance(){
  26. digitalWrite(trigPin, LOW);
  27. delayMicroseconds(2);
  28. // Sets the trigPin on HIGH state for 10 micro seconds
  29. digitalWrite(trigPin, HIGH);
  30. delayMicroseconds(10);
  31. digitalWrite(trigPin, LOW);
  32. duration = pulseIn(echoPin, HIGH);
  33. distance= duration*0.034/2;
  34. return distance;
  35. }

Compile your code.

image001 4
Compile

Now, connect the Arduino device together with your pc using an Arduino USB connector and upload the program.

image003 3
connect

Digital circuit diagram

Ultrasonic Sensor HC-SR04 Arduino UNO

VCC ——————————–> 5v

Trig ——————————–> Pin 8

Echo ——————————–> Pin 9

GND ——————————–> GND

Micro Servo Motor SG90 Arduino UNO

Orange wire ———————-> Pin 10

Red wire ———————-> 3.3v

Brown wire ———————-> GND

Now, place the larger a part of a lover over servo motor’s rotating wheel. Place your Ultrasonic device over the servo motor to form it rotating (you may use double sided sticky tape).

Test the subsequent code within the Processing IDE and run it. The Processing IDE displays the angle distance of an object when it comes within the range of Ultrasonic device.

  1. import processing.serial.*;
  2. import java.awt.event.KeyEvent;
  3. import java.io.IOException;
  4. Serial myPort;// defubes variables
  5. String angle=””;
  6. String distance=””;
  7. String data=””;
  8. String noObject;
  9. float pixsDistance;
  10. int iAngle, iDistance;
  11. int index1=0;
  12. int index2=0;
  13. PFont orcFont;
  14. void setup() {
  15. size (1366, 768);
  16. smooth();
  17. myPort = new Serial(this,”COM3″, 9600); // change this accordingly
  18. myPort.bufferUntil(‘.’); // reads the info from the interface up to the character ?.?. So actually it reads this: angle,distance.
  19. }
  20. void draw() {
  21. fill(98,245,31);
  22. // simulating motion blur and slow fade of the moving line
  23. noStroke();
  24. fill(0,4);
  25. rect(0, 0, width, height-height*0.065);
  26. fill(98,245,31); // green color
  27. // calls the functions for drawing the radar
  28. drawRadar();
  29. drawLine();
  30. drawObject();
  31. drawText();
  32. }
  33. void serialEvent (Serial myPort) { // starts reading data from the interface
  34. // reads the info from the interface up to the character ?.? and puts it into the String variable ?data?.
  35. data = myPort.readStringUntil(‘.’);
  36. data = data.substring(0,data.length()-1);
  37. index1 = data.indexOf(“,”); // find the character ?,? and puts it into the variable ?index1?
  38. angle= data.substring(0, index1); // read the info from position ?0? to position of the variable index1 or thats the worth of the angle the Arduino Board sent into the interface
  39. distance= data.substring(index1+1, data.length()); // read the info from position ?index1? to the top of the info pr thats the worth of the space
  40. // converts the String variables into Integer
  41. iAngle = int(angle);
  42. iDistance = int(distance);
  43. }
  44. void drawRadar() {
  45. pushMatrix();
  46. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  47. noFill();
  48. strokeWeight(2);
  49. stroke(98,245,31);
  50. // draws the arc lines
  51. arc(0,0,(width-width*0.0625),(width-width*0.0625),PI,TWO_PI);
  52. arc(0,0,(width-width*0.27),(width-width*0.27),PI,TWO_PI);
  53. arc(0,0,(width-width*0.479),(width-width*0.479),PI,TWO_PI);
  54. arc(0,0,(width-width*0.687),(width-width*0.687),PI,TWO_PI);
  55. // draws the angle lines
  56. line(-width/2,0,width/2,0);
  57. line(0,0,(-width/2)*cos(radians(30)),(-width/2)*sin(radians(30)));
  58. line(0,0,(-width/2)*cos(radians(60)),(-width/2)*sin(radians(60)));
  59. line(0,0,(-width/2)*cos(radians(90)),(-width/2)*sin(radians(90)));
  60. line(0,0,(-width/2)*cos(radians(120)),(-width/2)*sin(radians(120)));
  61. line(0,0,(-width/2)*cos(radians(150)),(-width/2)*sin(radians(150)));
  62. line((-width/2)*cos(radians(30)),0,width/2,0);
  63. popMatrix();
  64. }
  65. void drawObject() {
  66. pushMatrix();
  67. translate(width/2,height-height*0.074); // moves the starting coordinats to new location
  68. strokeWeight(9);
  69. stroke(255,10,10); // red color
  70. pixsDistance = iDistance*((height-height*0.1666)*0.025); // covers the space from the sensor from cm to pixels
  71. // limiting the range to 40 cms
  72. if(iDistance40) {
  73. noObject = “Out of Range”;
  74. }
  75. else {
  76. noObject = “In Range”;
  77. }
  78. fill(0,0,0);
  79. noStroke();
  80. rect(0, height-height*0.0648, width, height);
  81. fill(98,245,31);
  82. textSize(25);
  83. text(“10cm”,width-width*0.3854,height-height*0.0833);
  84. text(“20cm”,width-width*0.281,height-height*0.0833);
  85. text(“30cm”,width-width*0.177,height-height*0.0833);
  86. text(“40cm”,width-width*0.0729,height-height*0.0833);
  87. textSize(40);
  88. text(“Angle: ” + iAngle +” ?”, width-width*0.78, height-height*0.0277);
  89. text(“Distance: “, width-width*0.36, height-height*0.0277);
  90. if(iDistance
image005 1
a
image006 1
b

So, this brings us to the end of blog. This Tecklearn ‘IoT project of Sonar system using Ultrasonic Sensor HC-SR04 and Arduino device’ blog helps you with commonly asked questions if you are looking out for a job in Internet of Things (IoT). If you wish to learn IoT and build a career in Internet of Things (IoT) domain, then check out our interactive, Internet of Things (IoT) Training, that comes with 24*7 support to guide you throughout your learning period. Please find the link for course details:

https://www.tecklearn.com/course/iot-internet-of-things-training/

Internet of Things (IoT) Training

About the Course

Internet of Things or IoT, as it is widely known, simply put, is a network of devices which can communicate with each other with regards to sending, receiving and analyzing data. Tecklearn’s IoT Training as an online training platform delivers you the best in the industry IoT knowledge by certified and experienced trainers to master IoT. Interactive sessions include two real-time projects to provide in-depth understanding of advanced IoT concepts that covers IoT methods and technologies, application deployment, network and communication protocols and integrations, security measures and real-time data management on the internet. You will learn IoT introduction, significance, building your own IoT devices, sensors, IoT communication and security. This training will help you be a part of the IoT revolution underway around the globe.

Why Should you take IoT (Internet of Things) Training?

  • The average salary for an IoT Engineer is $163,514 per year in the United States. (Indeed.com)
  • Many industries such as Eddie Stobart Transport and Logistics Company, the Amazon, Dell, Aviva, German Auto Manufacturer Daimler, the John Deere Company and Walt Disney Land are all utilizing the Internet of Things technology to monitor various activities and advance their existing systems.
  • Gartner Says 5.8 Billion Enterprise and Automotive IoT Endpoints Will Be in Use in 2020

What you will Learn in this Course?

Introduction to Internet of Things

  • What is IoT, how does it work
  • IoT vs IIoT
  • Business Cases of IIoT
  • Industry 4.0
  • Properties of IoT device
  • IoT Ecosystem
  • IoT Decision Framework
  • IoT Solution Architecture Models
  • How IoT is Transforming Businesses
  • Major IoT Boards in Market

IoT Communication Protocols

  • Types of wireless communication
  • Major wireless Short-range communication devices and properties
  • Comparison of these devices (Bluetooth, WIFI, ZigBee, 6LoWPAN)
  • Major wireless Long-range communication devices and properties, Comparison of these devices (Cellular IoT, LPWAN)

IoT Architecture

  • The IoT Stack Architecture and the various components and layers
  • The app, the data processing and platform
  • IoT OS like Contiki, FreeRTOS and mbe
  • The edge and the connected thing or device

IoT Sensors and Device Platforms

  • Introduction to IoT Sensors and the role they play in getting the IoT systems work efficiently
  • Micro-electromechanical systems revolutionizing IoT sensors
  • Use Case of Water Quality Monitoring
  • Use Case: Sericulture
  • Difference between microcontroller and microprocessor
  • IoT Device Platforms – Arduino, Raspberry Pi etc
  • Smartphone Centric Architecture
  • IoT Application Layer protocols
  • Hands On

Arduino Platform and Arduino Interfacing

  • Arduino physical board, libraries and the Integrated Development Environment
  • Arduino Shields various operations such as heat and light sensing, GPS, UI display
  • Programming Arduino using C language
  • Controlling external devices using pins on the Arduino board
  • The Arduino Interface
  • Reading inputs from various sources and providing an output
  • Working with sensors for sensing and controlling the physical world
  • Deploying various types of sensors and connecting it to the Arduino
  • Constant conversion between analog and digital signals for information exchange between the physical and digital domains
  • Hands On

Raspberry Pi Platform and Raspberry Pi Interfacing

  • Introduction to Raspberry Pi
  • Set up of Raspberry Pi environment
  • Coding for the Raspberry Pi using Python
  • Deploying Python-based Integrated Development Environment
  • Interfacing the Raspberry Pi with the physical world
  • Introducing the various input and output devices
  • Raspberry Pi expansion boards for building complex hardware setup
  • Real-time demo of Raspberry Pi interfacing
  • Hands On

Arduino Uno Wifi and IoTivity

  • Iotivity
  • Iotivity Architecture
  • Hands On

Netduino Platform and Netduino Interfacing

  • Introduction to Netduino Platform
  • Setting up the Netduino environment
  • Coding for the Netduino
  • Interfacing the Netduino with the physical world
  • Introducing the various input and output devices
  • Real-time demo of Netduino interfacing
  • Hands On

IoT for Arduino, NodeMCU and Netduino

  • Control LED light using Netduino board
  • NodeMCU
  • Blynk

Project: Building WSN with MQTT, Raspberry Pi & Arduino

Got a question for us? Please mention it in the comments section and we will get back to you.

0 responses on "IoT project of Sonar system using Ultrasonic Sensor HC-SR04 and Arduino device"

Leave a Message

Your email address will not be published. Required fields are marked *