You are on page 1of 12

EE463: Mechatronics

NAMES: AASHUTOSH PRATAP S11187390

EDWARD SINGH S11186222

LAB DEMONSTRATOR: MR. SAYAD KUTTY

EXPT. NUMBER: 2

TITLE: INTERFACE ULTRASONIC SENSOR WITH ARDUINO BOARD

DATE: 03/03/23

1
AIM

The objectives of this lab are:

 To familiarize with the functionality and programming of the Arduino UNO/MEGA.


 Create an Arduino IDE program to interface an ultrasonic sensor (HCSR04) .
 Determine the centimeter-based calibration for the obstacle distance in front of the sensor.
 To Measure and plot the graph of “error vs distance” and “actual distance vs measured results”

INTRODUCTION

To begin with, to create and design intelligent devices and systems that can perceive, act upon, and control their
surroundings. The interdisciplinary area of mechatronics integrates computer, mechanical and electrical
engineering [1]. Robotic arms used in biomedical devices, drones, assembly and manufacturing lines, wearable
technology, autonomous vehicles, smart homes, and prosthetic limbs are all examples of Mechatronic systems
that are commonplace in contemporary culture [2]. To provide unique answers to challenging issues,
mechatronics blends cutting-edge technology including actuators, artificial intelligence, sensors, embedded
systems, and microcontrollers. The next generation of technologists and engineers must be prepared to meet the
problems of the twenty-first century, and mechatronics training and education are essential for this [3].

Figure 1: HC-SR04 Sensor [6].

Moreover, the sensor is one of the basic elements of mechatronics systems. A sensor is an instrument that can
measure and identify physical characteristics like motion, length, pressure and temperature [4]. It then
transforms those characteristics into electrical impulses that a computer or microcontroller can handle. Based on
the different operating principles, sensors may be categorized into many categories. Some of these could
possibly include: inductive, capacitive, ultrasonic sensors, magnetic, optical, and resistive. Due to their
precision, dependability, and simplicity of microcontroller integration, ultrasonic sensors, which assess
proximity and distance using high-frequency sound waves, are commonly utilized in mechatronics applications
[5].
2
The four pins used to interface the sensor are Vcc, Trigger, Echo and Ground. The table below sown the pin
descriptions:

Table 1: HC-SR04 Pin Characteristics [7].


Vcc Connect to +5v supply
Trigger Sends out an 8-cycle burst at a high state of 10µs
Echo Waits for reflected the 8-cycle burst wave
Groun
Connect to ground
d

Also, the HC-SR04 is a reasonably priced and simple to use distance measurement sensor with a range of 2 to
400 cm. Two ultrasonic transducers make up the sensor. One is a receiver which searches for reflected waves
whereas the other is a transmitter that emits the ultrasonic sound pulses [7]. Its main specifications include:

Figure 2: Specification of HC-SR04 ultrasonic Sensor[6].

The principle of ultrasonic operation is simple, if an object obstructs the 40000 Hz signal, it bounces back to the
module. The distance is then determined by accounting for the travel time and speed.

Figure 3: HC-SR04 Working Principle [8].

3
Furthermore, for the purpose of building and prototyping mechatronics projects, an Arduino is utilized. AN
Arduino is an open-source microcontroller platform that offers a user-friendly interface and a programmable
environment [9]. Arduino boards, such the Arduino MEGA and Arduino UNO are available in a variety of
shapes and sizes, and they can all be customized using the C++ based Arduino Integrated Development
Environment (IDE). The analogue and digital input/output pins on Arduino boards may connect to a variety of
actuators and sensors, including LCD screens, servo motors, LEDs, and ultrasonic sensors [10].

Figure 4: Arduino micro, UNO and MEGA [11].

All in all, this lab will provide students a working knowledge of Arduino programming, how to connect sensors
to microcontrollers, and practical mechatronics abilities. Many mechatronics applications, such as control
systems robotics and automation, will benefit from the abilities developed in this lab.

INSTRUMENTS

 Arduino MEGA/ UNO board.


 Measuring tape.
 Breadboard.
 Connecting wires.
 HCSR04 ultrasonic sensor.
 LEDs ×1 .
 10 k resistors.
 PC with Arduino IDE installed.

4
PROCEDURE

In order to successfully complete the lab, the following procedures were followed:

1. The hardware setup was constructed according to Figure 4 below.

Figure 5: Wring Diagram of Setup.

2. Utilizing the Arduino IDE, a program sketch was created to interface the sensor, obtained the distance
measurement (in centimeters) and displayed the result in serial monitor. The program utilizes interrupts
and an LED was used to show the pulsing of the ISR at each second interval.
3. A measuring tape was used to measure the actual distance of the of the ultrasonic sensor. Utilizing a
book as a target object, it was placed at 10 cm intervals from a range of 2 to 200 cm.
4. An actual distance vs program distance and error vs actual distance graph was plotted and the
observation was made according to the plot.

5
Flowchart of Arduino Sketch:

Figure 6: Flowchart for Ultrasonic Sensor Program Sketch.

RESULTS

Figure 7: Hardware implementation of Ultrasonic sensor.

6
Table 2: Actual and Measured Distance by Ultrasonic Sensor.
Actual Distance [cm] Program Distance [cm] Error [cm]
2 3.16 1.16
10 10.19 0.19
20 20.79 0.79
30 29.5 0.5
40 40.55 0.55
50 50.07 0.07
60 60.67 0.67
70 70.4 0.4
80 79.1 0.9
90 89.19 0.81
100 99.12 0.88
110 110.05 0.05
120 120.1 0.1
130 129.4 0.6
140 137.5 2.5
150 150.2 0.2
160 159.1 0.9
170 169.91 0.09
180 179.21 0.79
190 189.9 0.1
200 198.4 1.6

Graphs:

Actual vs Program Distance


250
Actual Distance (cm)

200
150
100
50
0
0 50 100 150 200 250
Program Distance (cm)

Figure 8: Graph of actual vs program distance.

7
Error Vs Actual Distance
3

2.5

Error ( Cm) 1.5

0.5

0
0 50 100 150 200 250
Actual Distance ( Cm)

Figure 9: Graph of error vs actual program distance.

DISCUSSION

This experiment was on interfacing ultrasonic with the Arduino IDE. For the first objective the program was
written and the hardware interface was done using the bread board.

The Arduino pins 6 and 7 were interfaced to the ultrasonic sensor whereas the LED was connected to pin 13.
The setup function configured the LED pin as an output, the serial communication was initialized at a 9600
baud rate, and configures Timer 1 to handle interrupts. Setting the OCR1A register to 62500 (the value caused
the interrupt to occur every 1 second), the WGM12 bit was set in the TCCR1B register for CTC mode, and the
CS12 bit was set to prescale the clock frequency by 256 that allowed the timer to produce an interrupt once per
second. It is specified that the interrupt service routine (ISR) read the distance from the ultrasonic sensor, that
converted the duration to centimeters using the function microsecondsToCentimeters, and displayed the
distance to the serial monitor. As the loop function is empty, it has no effect. The interrupt service procedure,
called by Timer 1, manages the main programme flow.

By dividing the duration by 29 (the speed of sound in centimeters per microsecond) and then by 2, the
microsecondsToCentimeters function takes a duration in microseconds as input and returns the distance in
centimeters (since the ultrasonic pulse travels to the object and back).

8
The second task was to calibrate obstacle distance in front of the sensor, where mobile phone was used as
obstacle and the results at an interval of 10 cm from 0 to 200 cm was taken. Since ultrasonic sensors measure
distance from 2 cm to 450 cm, the measurement at 0 cm was not recorded correctly. The 2 cm mark was used as
the starting distance. The distance measured and the LED state was displayed in the serial monitor. Also, each
time a measurement was obtained, the LED blinked.

The last task was to plot the graph of actual vs program distance and error vs actual distance. The actual vs
program distance graph was a linear graph. The error vs actual distance graph showed the error relationship
between the actual and measured program distance. The error present in the experimental results was less due to
sensor calibration. From the error vs actual distance graph it can be observed that at 140 cm the most errors was
present and at 110 cm the least number of error was visible.

CONCLUSION

To conclude, the ultrasonic sensor was interfaced using Arduino Uno. The program was written to find the
distance between the sensor and the obstacle with the help of sensor calibration. The distance from the
ultrasonic sensor was calculated using the formula, distance equals duration of high echo pulse multiplied by
speed of sound with everything divided by two. The sound waves goes from sensor to the object and than object
to sensor, due to that the equation was divided by 2, since the distance required was one way from sensor to
object only. The Arduino in built function “pulseIn” was used to measure the duration of echo pulse in
microseconds, whereas, “microsecondsToCentimeters” function was used to convert the duration of distance in
centimeters. Lastly, the graphs of both the actual distance vs measured distance and error vs actual distance was
plotted to verify the efficiency of the calibration made using the ultrasonic sensor. From the observation it was
noted that less error was present in the results.

9
REFERENCES

[1] “sample - Introduction to Mechatronics and Measurement Systems 5th 5E.pdf.” Accessed: Mar. 13,
2023. [Online]. Available: https://pdfuni.com/sample/SciencePhysics/SCPH1-100/SCPH558/sample%EF%BC
%8DIntroduction%20to%20Mechatronics%20and%20Measurement%20Systems%205th%205E.pdf

[2] R. Dell’Erba, “ROBOTICS IN ITALY Education, Research, Innovation and Economics outcomes”.

[3] M. G. Violante and E. Vezzetti, “Guidelines to design engineering education in the twenty-first century
for supporting innovative product development,” Eur. J. Eng. Educ., vol. 42, no. 6, pp. 1344–1364, Nov. 2017,
doi: 10.1080/03043797.2017.1293616.

[4] R. C. Luo, “Sensor technologies and microsensor issues for mechatronics systems,” IEEEASME Trans.
Mechatron., vol. 1, no. 1, pp. 39–49, Mar. 1996, doi: 10.1109/3516.491408.

[5] A. S. A. Kumar, B. George, and S. C. Mukhopadhyay, “Technologies and Applications of Angle


Sensors: A Review,” IEEE Sens. J., vol. 21, no. 6, pp. 7195–7206, Mar. 2021, doi:
10.1109/JSEN.2020.3045461.

[6] “Open Electronics Project: Ultrasonic Distance Sensor HC-SR04 Interfacing with 8051.”
https://openelectronicsproject.blogspot.com/2015/07/ultrasonic-distance-sensor-hc-sr04.html?m=1 (accessed
Mar. 13, 2023).

[7] Dejan, “Ultrasonic Sensor HC-SR04 and Arduino - Complete Guide,” How To Mechatronics, Feb. 02,
2022. https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-sr04/ (accessed Mar. 13, 2023).

[8] “Ultrasonic Sensor Module (HC-SR04),” Personal Blog, May 22, 2020.
https://zinminhtetj3.wordpress.com/2020/05/22/ultrasonic-sensor-module-hc-sr04/ (accessed Mar. 13, 2023).

[9] “Elsevier Enhanced Reader.” https://reader.elsevier.com/reader/sd/pii/S1574013721000046?


token=E51F2C0EDBCCB4EB411076F1FAD9F9ADE2348D94D8E6592D01C38F2B521B2FC5A848D1C369
8092702607ECA74864E043&originRegion=us-east-1&originCreation=20230312215405 (accessed Mar. 13,
2023).

[10] “Arduino Cookbook: Recipes to Begin, Expand, and Enhance Your Projects - Michael Margolis, Brian
Jepson, Nicholas Robert Weldin - Google Books.” https://books.google.com.fj/books?
hl=en&lr=&id=3b3dDwAAQBAJ&oi=fnd&pg=PP1&dq=The+analogue+and+digital+input/
output+pins+on+Arduino+boards+may+connect+to+a+variety+of++actuators+and+sensors,
10
+including+LCD+screens,+servo+motors,+LEDs,
+and+ultrasonic+sensors.&ots=eFJV694IU9&sig=B53mt8_tI9XyJ3RqyncwZU0R5No&redir_esc=y#v=onepag
e&q&f=false (accessed Mar. 13, 2023).

[11] “Arduino 开 发 板 比 较 指 南 | Arrow.com.”


https://www.arrow.com/zh-cn/research-and-events/articles/arduino-uno-vs-mega-vs-micro (accessed Mar. 13,
2023).

[12] “Arduino 101: Timers and Interrupts,” RobotShop Community, Aug. 07, 2011.
http://community.robotshop.com/forum/t/arduino-101-timers-and-interrupts/13072 (accessed Mar. 05, 2023).

[13] Oscar, “Arduino Timer and Interrupt Tutorial,” Oscar Liang, Feb. 04, 2013.
https://oscarliang.com/arduino-timer-and-interrupt-tutorial/ (accessed Mar. 05, 2023).

APPENDIX

The code for the Arduino sketch is as follows:

#include <avr/io.h> // Include AVR input/output library


#include <avr/interrupt.h> // Include AVR interrupt library

const int ledPin = 13; // Assign pin 13 to the LED


const int pingPin = 7; // Assign pin 7 to the trigger pin of the ultrasonic sensor
const int echoPin = 6; // Assign pin 6 to the echo pin of the ultrasonic sensor

void setup() {
Serial.begin(9600); // Initialize serial communication at 9600 baud rate
pinMode(ledPin, OUTPUT); // Set LED pin as output
noInterrupts(); // Disable interrupts

TCCR1A = 0; // Clear timer control register A


TCCR1B = 0; // Clear timer control register B
TCNT1 = 0; // Set timer count register to 0

OCR1A = 62500; // Set output compare register to trigger interrupt every 1 second
TCCR1B |= (1 << WGM12); // Set timer control register B to clear timer on compare match (CTC) mode
TCCR1B |= (1 << CS12); // Set timer prescaler to 256
TIMSK1 |= (1 << OCIE1A); // Enable timer 1 output compare match interrupt

interrupts(); // Enable interrupts


}

ISR(TIMER1_COMPA_vect) { // Interrupt service routine for timer 1 output compare match


11
digitalWrite(ledPin, !digitalRead(ledPin)); // Toggle the LED pin state
int state = digitalRead(10); // Read the state of pin 10 and assign it to state variable
float duration, cm; // Declare variables to store duration and distance in centimeters
pinMode(pingPin, OUTPUT); // Set trigger pin as output
digitalWrite(pingPin, LOW); // Set trigger pin low
delayMicroseconds(2); // Delay for 2 microseconds
digitalWrite(pingPin, HIGH); // Set trigger pin high
delayMicroseconds(10); // Delay for 10 microseconds
digitalWrite(pingPin, LOW); // Set trigger pin low again
pinMode(echoPin, INPUT); // Set echo pin as input
duration = pulseIn(echoPin, HIGH); // Measure duration of echo pulse in microseconds
cm = microsecondsToCentimeters(duration); // Convert duration to distance in centimeters using
microsecondsToCentimeters function
Serial.print(cm); // Print distance in centimeters to serial monitor
Serial.print("cm"); // Print units to serial monitor
Serial.println(state); // Print the state of pin 10 to serial monitor
}

void loop() {
// The loop function is empty since the main program flow is handled by the interrupt service routine
}

float microsecondsToCentimeters(float microseconds) { // Function to convert duration to distance in


centimeters
return microseconds / 29 / 2; // Convert duration to distance using speed of sound and return the result
}

12

You might also like