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
- Arduino UNO board
- USB cable connecter for Arduino UNO
- Ultra Sonic HC-SR04
- Jumper wires male to female
Software requirements
- Arduino software
- 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:
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.
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.
Upload your program in Arduino device
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/.
Follow the instruction to install the 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.
Initially, it displays the 0 as no activity in the process.
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.