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

IoT - Platform

IoT Platform: As in IoT, all IoT devices are connected to other IoT devices and applications to transmit and receive information through protocols. There is a difference between the IoT...

2 minutes 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.

Salesforce IoT Cloud

Salesforce IoT Cloud IoT Cloud is a platform from Salesforce. Salesforce is designed to store and process IoT data. It is powered by Thunder. Thunder is an event processing engine designed to capture, filter, and...

2 minutes read.

Arduino UNO Board with PIR Motion Sensor

Arduino UNO Board with PIR Motion Sensor In this project, we will create a simple circuit with an Arduino UNO and PIR motion sensor detects movement. An LED light up when the movement...

2 minutes read.

Contiki OS

Contiki OS Contiki is an open-source operating system (OS) that works on small, low-power microcontrollers. It allows the development of applications. The application allows efficient use of hardware while providing standardized low-power wireless communication...

2 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.

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.

IoT Protocols

IoT Protocols A protocol is a set of basic rules that allow electronic devices to communicate with each other. These instructions include the types of data that can be sent, the...

1 minute 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.

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.

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.

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.

Download and Install Arduino in Windows

Download and Install Arduino in Windows Step 1. Download  IDE from https://www.arduino.cc/en/Main/Software. Step 2. Scroll down the page and click on Windows app link, as shown in the following figure. Step 3. Click on the...

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 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.

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.

IoT Hardware

IoT hardware includes a wide range of devices, such as routing devices, bridges, sensors, etc. These IoT devices support key tasks and functions such as system activation, security, action specifications, communication, and detection...

2 minutes read.

IoT Ecosystem

The following figure shows the seven-layer model of the IoT Ecosystem. The first layer represents the market or application domain that includes smart grid, connected home, smart health, or smart cities, etc.The second layer consists...

1 minute 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.

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.