Arduino UNO Distance Project

Ultrasonic Sensor HC-SR04 and Arduino to distance calculation using Processing App:

Let's build an IoT project using Ultrasonic HC-SR04 Sensor and Arduino UNO to calculate the distance between the Ultra Sonic HC-SR04 device and an object. We will use a processing application to show the distance between the Ultra Sonic device and the object on the screen of the laptop (monitor).

The basic principle of ultrasonic Sensor distance measurement is based on ECHO. When sound waves are transmitted to the environment, the waves are returned as ECHO after hitting the object. Therefore, it is sufficient to calculate the travel time of the two sounds: the departure time and the time of return to the origin after hitting the object. As we know the speed of sound, we can calculate the distance after a certain calculation.

Requirements of hardware in this Project:

  1. Arduino UNO board
  2. USB cable connector for Arduino UNO
  3. Ultra Sonic HC-SR04
  4. Jumper wires male to female
Requirements of hardware in this Project

Requirements of software in this Project:

  1. Arduino IDE
  2. Processing IDE

Open Arduino IDE and write the following code:

Open Arduino IDE
#include <Mouse.h>    
const int trigpin= 8;  
const int echopin= 7;  
long duration;  
int distance;  
void setup(){  
  pinMode(trigpin,OUTPUT);  
  pinMode(echopin,INPUT);  
  Serial.begin(9600);  
}  
void loop(){  
  digitalWrite(trigpin,HIGH);  
  delayMicroseconds(10);  
  digitalWrite(trigpin,LOW);  
  duration=pulseIn(echopin,HIGH);  
  distance = duration*0.034/2;  
  Serial.println(distance);  
}  

Save your program and compile.

Compile and Run

Connect your Arduino UNO board to your laptop or desktop via Arduino UNO USB cable. Remove all other connections with the Arduino UNO board, such as the Ultrasonic sensor, then upload the program to the Arduino UNO board.

UNO Board Connections

Remember: Before uploading the code in the Arduino UNO device, make sure your Arduino serial port is selected otherwise it displays an error message Serial port not selected.

To select your serial port open Device Manager -> Ports -> Arduino Uno, and then upload your code.

Port Selections

Upload your program in the Arduino device.

upload your programs

Digital circuit diagram:

Digital circuit diagram

Now download the Processing IDE.

Download Processing IDE from https://processing.org/download/

After downloading, open the Processing IDE and write the following code:

 open the Processing IDE

Full code is shown in below:

import processing.serial.*;    
Serial myPort;    
String data="" ;  
PFont  myFont;    
void setup(){  
  size(1366,900); // size of processing window  
  background(0);// setting background color to black  
  myPort = new Serial(this, "COM3", 9600);  
  myPort.bufferUntil('\n');  
}  
void draw(){  
  background(0);  
  textAlign(CENTER);  
  fill(255);  
  text(data,820,400);  
  textSize(100);  
  fill(#4B5DCE);  
  text("              Distance :        cm",450,400);  
  noFill();  
  stroke(#4B5DCE);  
}  
void serialEvent(Serial myPort){  
   data=myPort.readStringUntil('\n');  
}   

Run the code.

Now, connect your Ultrasonic sensor module and Arduino UNO device. Input the power supply into the Arduino device using an Arduino USB cable or a 220V AC adapter.

Place an object in front of the Ultrasonic Sensor module. It shows the distance of that object on the processing IDE screen.

In the following image, we have placed an object in front of Ultrasonic Sensor. The object is close to the Ultrasonic Sensor and it shows the distance of 3cm.

shows the distance
Show Distance
show distance

Related Topics

Arduino UNO Bluetooth LED Controls

How to Build a Bluetooth controlled LED light Setup Using Arduino UNO board: Let's build an IoT project using Arduino UNO and the Bluetooth module HC-05 to control an LED. In this project,...

2 minutes read.

Eclipse IoT

Eclipse IoT The two technologies IoT and M2M (Machine to Machine) are becoming trendy in the IT industry. The ability to connect sensors, actuators, and a wide range of devices open up a...

2 minutes read.

IoT Technologies

The IoT is based on three main technologies: Hardware (includes chips and sensors)Communication (includes wireless network)  Software (includes data storage, analytics, front-end applications) The reason the Internet of Things is emerging so rapidly today...

1 minute read.

Skills required for an IoT Developer

"Internet of Things" is about communication between software and devices. An IoT development team should be essentially large, and developers are divided into different areas according to their respective work. Some of these...

1 minute read.

IoT Software

The IoT software addresses significant areas of networking and action through platforms, integrated systems, associated systems, and middleware. These applications are responsible for data collection, device integration, real-time analysis, and process extension in the...

1 minute read.

Arduino UNO Radar Project

Arduino Radar Project In this project, we will explain how to create a simple radar system using Arduino and Processing IDE. The Arduino radar project is implemented using Processing Applications. Radar is a long-range object detection...

9 minutes read.

Detect darkness Using Arduino and LDR Sensor

Detect the darkness with the help of Arduino board and LDR Sensor In this project, Arduino and LDR Sensor turns on lights automatically, when an LDR sensor detects darkness. Requirements of hardware...

2 minutes read.

Session Layer Protocols

1) MQTT MQTT stands for Message Queue Telemetry Transport. It was introduced by IBM in 1999 and standardized by OASIS in 2013. It is used for remote monitoring in IoT. Its...

2 minutes read.

Challenges of IoT

Challenges of IoT There are two categories of challenges to overcome for the IoT organization to grow. It is accepted by all the technologies not only for IoT. Two significant challenges in IoT: Technological...

3 minutes read.

IoT GE Predix

The GE Predix stands for the General Electric Predix. GE Predix is a software platform that collects data from industrial instruments. It provides a cloud-based PaaS (platform as a service)...

1 minute read.

5G

The next generation of mobile networks is a new global wireless standard after 1G, 2G, 3G, and 4G networks. It enables a new network to connect virtually everyone, including machines,...

5 minutes read.

What is an IoT Device?

A device in which a sensor is connected and able to transfer data from one device to another over the internet is called an IoT device. IoT devices consist of software, wireless sensors, actuators,...

1 minute read.

IoT Architecture

Our approach to the IoT architecture is reflected in the IoT architecture diagram that presents the diagram of an IoT system and explains how it is connected to store, collect,...

2 minutes read.

IoT-ThingWorx

IoT-ThingWorx The ThingWorx is an IoT platform that helps commercial corporations assess and release the value of IoT. It promises the tools and technology necessary to developed. It implements a robust...

2 minutes read.

Arduino UNO Distance Project

Ultrasonic Sensor HC-SR04 and Arduino to distance calculation using Processing App: Let's build an IoT project using Ultrasonic HC-SR04 Sensor and Arduino UNO to calculate the distance between the Ultra Sonic HC-SR04 device and...

2 minutes read.

IoT Tutorial

IoT stands for the Internet of Things. The term "Things" refers to objects (home appliances and devices) that we use in our daily life. These objects are accessible or connected...

4 minutes read.

Arduino UNO Home Automation

Build a Bluetooth controlled Home Automation Setup Using Arduino UNO board and Relay module Do you want to automate your home? Have you ever wanted to control your home appliances by swipe the screen...

4 minutes read.

3D Printing

3D printing is making 3D solid objects from a digital file, achieved using additive processes. In this process, an object is created by laying down successive layers of the material...

4 minutes read.

Network layer Protocols

Network layer Encapsulation ProtocolsNetwork Layer Routing Protocols 1. Network layer Encapsulation Protocols a) 6LoWPAN 6LoWPAN stands for IPv6 Over Low-Power Wireless Personal Area Network. It is mostly used standard in this category. It...

2 minutes read.

Components of IoT

There are the following components used in IoT: Devices and sensorsCloud ComputingUser interfaceNetworking connectionGateway Devices and sensors Devices and sensors are the components of the connectivity layer. These smart sensors continuously collect data from the...

2 minutes read.