Javatpoint Logo
Javatpoint Logo

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

Let's build an IoT project using Ultra Sonic HC-SR04 and Arduino (Arduino UNO) to calculate distance between Ultra Sonic HC-SR04 device and an object. In this project, we will use a Processing app to display the distance between Ultra Sonic device and object on the Laptop's (Monitor) screen.

Hardware Requirements

  1. Arduino UNO board
  2. USB cable connecter for Arduino UNO
  3. Ultra Sonic HC-SR04
  4. Jumper wires male to female

Software requirements

  1. Arduino software
  2. Processing software

The working principle of Arduino-Bluetooth Module

The Ultra Sonic HC-SR04 emits ultrasound at 40,000Hz that travels in the air. If there is an object or obstacle in its path, then it collides and bounces back to the Ultra Sonic module.

The formula distance = speed*time is used to calculate the distance.

Suppose, an object is placed at a distance of 10 cm away from the sensor, the speed of sound in air is 340 m/s or 0.034 cm/µs. It means the sound wave needs to travel in 294 µs. But the Echo pin double the distance (forward and bounce backward distance). So, to get the distance in cm multiply the received travel time value with echo pin by 0.034 and divide it by 2.

The distance between Ultra Sonic HC-SR04 and an object is:

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

For doing programming of Arduino device, it requires Arduino software IDE. The complete process of downloading and installation of Arduino software IDE is given at link controlling home light using WiFi Node MCU, and Relay module.

Open Arduino IDE and paste the following code.

Save your program and compile it.

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

Connect your Arduino device to your Laptop (or Monitor) via Arduino UNO USB cable. Remove all the other connections with Arduino UNO device such as Ultrasonic module while uploading the program in Arduino UNO.

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

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

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

Upload your program in Arduino device

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

Digital circuit diagram

Ultrasonic Sensor HC-SR04       Arduino UNO

VCC --------------------------------> 5v

Trig --------------------------------> Pin 8

Echo --------------------------------> Pin 7

GND --------------------------------> GND

Now download the Processing from https://processing.org/download/.

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

Follow the instruction to install the Processing app.

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

Paste the following code in the Processing IDE and run it. The Processing IDE displays the distance between Ultra Sonic module and an object.


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

Initially, it displays the 0 as no activity in the process.

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

Now, connect your Ultrasonic HC-SR04 module and Arduino device. Input the power source to Arduino device using Arduino USB cable or 220v AC adapter.

Place an object in front of the Ultrasonic HC-SR04 module and show the distance of that object in Processing IDE display.

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




Help Others, Please Share

facebook twitter google plus pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA