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:
- Arduino UNO board
- USB cable connector for Arduino UNO
- Ultra Sonic HC-SR04
- Jumper wires male to female
Requirements of software in this Project:
- Arduino IDE
- Processing IDE
Open Arduino IDE and write the following code:
#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.
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.
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.
Upload your program in the Arduino device.
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:
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.