Research On Use of Arduino
Research On Use of Arduino
Research On Use of Arduino
SUBMITTED TO
AWARD OF
MECHATRONICS ENGINEERING
MAY, 2023
CERTIFICATION
This is to certify that this work, “Interfacing Ultrasonic Sensor to ATmega 328
Microcontroller” was carried out by MOFFAT EMEDIONG AKPABONG, in
partial fulfilment of the requirements for the award of the Bachelor of
Engineering (B. Eng.) degree in Mechatronics Engineering, Federal University
of Technology, Owerri.
Approved By:
………………………… Date:…………………….
Engr. Dr. S. E. Mbonu
Project Supervisor
…………………………. Date:…………………….
ii
DEDICATION
This technical report is dedicated to GOD ALMIGHTY for the grace, wisdom and
strength throughout the training and for the successful completion of this research
work.
I also dedicate this work to my parents Mr & Mrs Akpabong Moffat Udoekpo for
their support and concern towards my academic career, may God bless you. And
also, my lecturers at Federal University of Technology, Owerri.
iii
ACKNOWLEDGEMENTS
I thank God Almighty for putting this idea into the heart and mind of the officials
in this department. I appreciate all my lecturers and course mates for their support
and help. I specially appreciate my Project supervisor, Engr. Dr. S. E. Mbonu for
contributing and encouraging immensely and providing all resources needed for
me to achieve this work. I also want to thank my Head of Department, Engr. Prof
C. A. Okoronkwo, my Course Adviser, Engr. Dr. G. C. Nzebuka, and other
laboratory and academic staff of Mechatronics Engineering department, Federal
University of Technology, Owerri, who assisted me in one way or the other in
acquiring and building on this knowledge. I would like to specifically acknowledge
my team members for their exceptional contribution to the seminar. They
demonstrated outstanding commitment and went above and beyond their duties to
ensure that the seminar was completed on time and to the highest standard.
iv
ABSTRACT
This seminar investigates the possibility of using ultrasonic sensor as a level sensor
to measure water level in automatic water pumping system. A number of sensors
have been used in water level measurements but most of them are not contactless
leading to possible contamination of the water being measured. In the
methodology, ultrasonic sensor was interfaced to Atmega 328 microcontroller and
was simulated using proteus design suite. Results showed that the sensor can
measure a distance between 2cm-400cm. This is a reasonable distance for level
measurements. The results of this work will be applied in water level
measurements for validation given that water does not have a solid surface
originally designed for ultrasonic sensors. When fully validated, the results of this
work/seminar can be applied in order areas of control system that require level
measurements.
v
TABLE OF CONTENTS
COVER PAGE i
CERTIFICATION ii
DEDICATION iii
ACKNOWLEDGEMENTS iv
ABSTRACT v
TABLE OF CONTENTS vi
LIST OF FIGURES ix
CHAPTER ONE 1
INTRODUCTION 1
CHAPTER TWO 3
LITERATURE REVIEW 3
2.1 Overview of Water Level Sensors 3
2.1.1 Optical Water Level Sensor 3
2.1.2 Capacitance Liquid Level Sensor 4
2.1.3 Float Level Sensor 4
2.1.4 Radar Level Gauge 4
2.1.5 Diaphragm Liquid Level Sensor 5
2.1.6 Tuning Fork Level Sensor 5
2.1.7 Ultrasonic Liquid Level Sensor 5
2.2 Review of Atmega328 Microcontroller 6
2.3 Review of HC-SR04 Ultrasonic Sensor 10
2.3.1 Principle of Operation of an Ultrasonic Sensor 10
vi
2.4 Overview of Related Works 13
CHAPTER THREE 14
MATERIALS AND METHOD 14
3.1 Material Used 14
3.2 Description of Block Diagram of the Project 14
3.3 Description of Block Diagram for Interfacing Level Sensor to
ATmega328 Microcontroller 16
3.4 Interfacing of HC-SR04 Ultrasonic sensor to ATmega328
Microcontroller using Proteus Design Suite 17
3.4.1 Design Procedures 17
3.4.2 Flowchart Algorithm for Interfacing of HC-SR04 Ultrasonic
Sensor to Atmega 328 Microcontroller 19
3.4.3 Program Code 19
CHAPTER FOUR 21
RESULTS 21
4.1 Description of Results 21
CHAPTER FIVE 23
CONCLUSION 23
5.1 Contributions of Work to the Overall Project Actualization 23
5.2 Challenges and Recommendations 23
5.3 Conclusion 24
REFERENCES 25
vii
LIST OF TABLES
viii
LIST OF FIGURES
Fig. 3.1: Block Diagram of a Smart Water Pumping System with IOT
monitoring and Metering 14
ix
CHAPTER ONE
INTRODUCTION
1.1 Background of the Work
This seminar is a subset of a project, “Smart Water Pumping System with IoT
Monitoring and Metering”. There are different methods of water level sensing
which include the use of float switches, pressure sensors, capacitance sensors,
conductivity sensors, optical sensors and magnetic sensors. Primarily, the
ultrasonic sensor uses echo sound principle to measure distance which is
normally efficient with a solid surface.
This study is done to investigate the possibility of the use of ultrasonic sensor
to measure water level in a smart water pumping system and to see whether the
water surface can reflect the signal. This work will look at the basic principle
of operation of ultrasonic sensor, develop the algorithm, code and simulate it
using the necessary software which will be integrated in the main project.
Water contains hydrogen and oxygen ions. Thus, when sensors come in contact
with water, it could lead to change in pH value of water thereby contaminating
the water. There is need to investigate the possibility of implementing automatic
water pumping system using a contactless sensor that is compatible with
microcontrollers.
1
1.4 Justification of the Work
There are different useful reasons why this work has to be undertaken. One can
precisely measure distances by connecting an ultrasonic sensor to a
microcontroller and using this information to control other project components.
For students and hobbyists who are interested in learning about microcontrollers
and sensors, interfacing an ultrasonic sensor to an ATmega328 microcontroller
can be an excellent teaching project. This work can be applied in water level
sensing.
This work is an aspect of a smart water pumping system with IoT monitoring
and metering. It is intended to take care of the water level sensing aspect of the
project. This seminar will simulate the water level sensing of the overall project
without implementing the physical project.
2
CHAPTER TWO
LITERATURE REVIEW
Water level sensor is a device that measures the liquid level in a fixed container.
According to the method of measuring the liquid level, it can be divided into
two types: contact type and non-contact type. Contact water level sensors, also
known as "probing" sensors, make physical contact with the liquid being
measured. These sensors typically use a probe or a float mechanism to detect
the height of the liquid level and convert it into an electrical signal for output.
Non-contact water level sensors do not make physical contact with the liquid.
Instead, they use various technologies such as ultrasonic, radar, or optical
sensors to detect the distance between the sensor and the liquid surface, and then
calculate the liquid level based on this measurement. Some types of water level
sensor are listed below.
The optical sensor is solid-state. They use infrared LEDs and phototransistors,
and when the sensor is in the air, they are optically coupled. When the sensor
head is immersed in the liquid, the infrared light will escape, causing the output
to change. These sensors can detect the presence or absence of almost any
liquid. They are not sensitive to ambient light, are not affected by foam when in
air, and are not affected by small bubbles when in liquid.
3
Disadvantages: Not to be used under direct sunlight, water vapor affects the
measurement accuracy (Helen, 2021).
Advantages: can be used to determine the rise or fall of the liquid in the
container. By making the electrode and the container the same height, the
capacitance between the electrodes can be measured. No capacitance means no
liquid. A full capacitance represents a complete container.
The float switch is the original level sensor. They are mechanical equipment.
The hollow float is connected to the arm. As the float rises and falls in the liquid,
the arm will be pushed up and down. The arm can be connected to a magnetic
or mechanical switch to determine on/off, or it can be connected to a level gauge
that changes from full to empty when the liquid level drops. The use of float
switches for pumps is an economical and effective method to measure the water
level in the pumping pit of the basement.
Advantages: The float switch can measure any type of liquid and can be
designed to operate without any power supply.
Disadvantages: They are larger than other types of switches, and because they
are mechanical, they must be used more frequently than other level switches.
A radar liquid level is a liquid level measuring instrument based on the principle
of time travel. The radar wave runs at the speed of light, and the running time
can be converted into a level signal by electronic components. The probe sends
out high-frequency pulses that travel at the speed of light in space, and when
4
the pulses meet the surface of the material, they are reflected and received by
the receiver in the meter, and the distance signal is converted into a level signal.
The diaphragm or pneumatic level switch relies on air pressure to push the
diaphragm, which engages with a micro switch inside the main body of the
device. As the liquid level increases, the internal pressure in the detection tube
will increase until the microswitch is activated. As the liquid level drops, the air
pressure also drops, and the switch opens.
Advantages: There is no need for power in the tank, it can be used with many
types of liquids, and the switch will not come into contact with liquids.
The tuning fork level gauge is a liquid point level switch tool designed by the
tuning fork principle. The working principle of the switch is to cause its
vibration through the resonance of the piezoelectric crystal. Every object has its
resonant frequency. The resonant frequency of the object is related to the size,
mass, shape, force of the object.
Advantages: It can be truly unaffected by flow, bubbles, liquid types, etc., and
no calibration is required.
5
received by the same sensor. It is converted into an electrical signal by a
piezoelectric crystal. The time between the transmission and reception of the
sound wave is used to calculate the Measure of the distance to the surface of the
liquid.
Parameter Value
Flash memory 32 KB
SRAM 2 KB
EEPROM 1 KB
External interrupts 3
7
USB interface No
Functions associated with the pins must be known in order to use the device
appropriately.
ATmega-328 pins are divided into different ports which are given in detail
below:
Port A consists of the pins from PA0 to PA7. These pins serve as an analog
input to analog to digital converters. If analog to digital converter is not used,
port A acts as an eight (8) bit bidirectional input/output port.
Port B consists of the pins from PB0 to PB7. This port is an 8-bit bidirectional
port having an internal pull-up resistor.
8
Port C consists of the pins from PC0 to PC7. The output buffers of port C has
symmetrical drive characteristics with source capability as well high sink.
Port D consists of the pins from PD0 to PD7. It is also an 8-bit input/output port
having an internal pull-up resistor.
AREF is an analog reference pin for analog to digital converter (Shawn, 2019).
a) Operating voltage = 5 DC V
b) Input voltage = 7-12V
c) DC current per I/O pin = 20mA
d) DC current for 3.3V pin = 50mA
9
2.3 Review of HC-SR04 Ultrasonic Sensor
The Ultrasonic Sensor is a type of electronic sensor that uses sound waves to
measure distances. It works by emitting high-frequency sound waves and then
measuring the time it takes for those waves to bounce back. The sensor consists
of a transceiver that sends and receives the sound waves, a microcontroller, and
a signal conditioner.
The HC-SR04 is an affordable and easy to use distance measuring sensor which
has a range from 2cm to 400cm (about an inch to 13 feet).
10
The working principle of the ultrasonic water level sensor is that the ultrasonic
transducer (probe) sends out a high-frequency pulse sound wave when it
encounters the surface of the measured level (material), is reflected, and the
reflected echo is received by the transducer and converted into an electrical
signal. The propagation time of the sound wave. It is proportional to the distance
from the sound wave to the surface of the object. The relationship between the
sound wave transmission distance S and the sound speed C and the sound
transmission time T can be expressed by the formula: S=C×T/2.
It emits an ultrasound at 40 000 Hz which travels through the air and if there is
an object or obstacle on its path It will bounce back to the module. Considering
the travel time and the speed of the sound you can calculate the distance.
In order to generate the ultrasound we need to set the Trig pin on a High State
for 10 µs. That will send out an 8 cycle ultrasonic burst which will travel at the
speed of sound. The Echo pins goes high right away after that 8 cycle ultrasonic
burst is sent, and it starts listening or waiting for that wave to be reflected from
an object. If there is no object or reflected pulse, the Echo pin will time-out after
38ms and get back to low state. If we receive a reflected pulse, the Echo pin will
go down sooner than those 38ms. According to the amount of time the Echo pin
was HIGH, we can determine the distance the sound wave traveled, thus the
distance from the sensor to the object.
For that purpose we are using the following basic formula for calculating
distance:
We actually know both the speed and the time values. The time is the amount
of time the Echo pin was HIGH, and the speed is the speed of sound which is
340m/s. There’s one additional step we need to do, and that’s divide the end
result by 2. And that’s because we are measuring the duration the sound wave
needs to travel to the object and bounce back.
Let’s say the Echo pin was HIGH for 2ms. If we want the get the distance result
in cm, we can convert the speed of sound value from 340m/s to 34cm/ms.
11
So, if the Echo pin was HIGH for 2ms (which we measure using the pulseIn()
function), the distance from the sensor to the object is 34cm.
Operating Voltage - 5V DC
Accuracy - 3mm
Dimension - 45 x 20 x 15mm
The sensor has 4 pins. VCC and GND go to 5V and GND pins on the Arduino,
and the Trig and Echo go to any digital Arduino pin. Using the Trig pin we send
the ultrasound wave from the transmitter, and with the Echo pin we listen for
the reflected signal. [5]
12
2.4 Overview of Related Works
Liu et al. (2019) paper provides a comprehensive review of various water level
sensors, including float switches, pressure sensors, capacitive sensors,
ultrasonic sensors, optical sensors, and magnetic sensors. The paper compares
the advantages and disadvantages of each type of sensor and discusses their
applications in different fields, such as environmental monitoring, agriculture,
and industrial automation (Liu, Hu, Zhang, & Xu, 2019).
Kim and Lee (2016) conducted a review of the ultrasonic sensor for obstacle
detection in autonomous vehicles. They discussed the sensor's technical
specifications, ranging capabilities, and accuracy. The study provides a review
of the sensor based on its technical specifications, but it does not provide
empirical data on the performance of the sensor in real-world conditions.
Further studies are necessary to evaluate the sensor's performance in different
environments and under various conditions (Kim & Lee, 2016).
13
CHAPTER THREE
Fig. 3.1 Block diagram of a smart water pumping system with IoT monitoring and
metering
1. Water Level Sensor: The ultrasonic sensor is used as a water level sensor
in the system in which the output is a serial communicator and it has a signal
conditioner embedded in it. It emits ultrasonic waves towards the water
surface and measures the time it takes for the waves to bounce back. Based
14
on the time taken, the sensor calculates the distance to the water surface and
provides an accurate measurement of the water level. The Ultrasonic sensor
HC-SR04 uses sound waves to detect distance of object by measuring time
spent for the object/body to reflect the ultrasonic sound waves emitted by it.
Ultrasonic sensors emit short, high frequency sound pulses at regular
intervals.
2. Signal Conditoner: The signal conditoner is embedded in the HC-SR04
Ultrasonic sensor. The signal conditioner is a component that processes the
electrical signals generated by the sensor and prepares them for further
processing by a microcontroller or other digital devices. The signal
conditioner also adjusts the signal level to match the input requirements of
the connected device and removes any noise or interference from the signal.
3. ATmega328 Microcontroller: It represents the internal components and
architecture of the microcontroller, providing the necessary functionality to
execute programs, interact with peripherals, and perform a wide range of
tasks in various electronic applications.The Atmega328 microcontroller is
embedded in Arduino UNO and it is programmed to make the sensor
measures the water level of a tank and the level data collected is used for
the pumping process.
4. Driver: Drivers are electronic components or circuits that provide the
necessary signals and power to control the actuators in the water pump
system. They act as intermediaries between the microcontroller/PLC and the
actuators, translating the control signals into suitable voltages or currents
required by the actuators. The driver used is a 12V Relay module which is
connected to the microcontroller uses the level data gotten from the sensor
to trigger on/off the DC Pump.
5. Actuator: Actuators are devices or mechanisms that physically control
operations and are driven by valves, elcetric motorsand others. The actuator
used is a DC pump which is driven by the relay module.
6. IoT Gateway: The IoT gateway (GPRS) acts as a communication interface
between the sensors and the cloud or central monitoring system. It collects
data from the sensors and sends it to the cloud for further processing and
analysis using GSM networks.
15
7. IoT Cloud Monitoring Platform: The cloud platform receives the data
from the IoT gateway and performs data processing, storage, and analysis
tasks. The cloud platform enables remote monitoring and control of the
water pumping system. Users can access the system through a web-based or
mobile application to monitor real-time data, receive alerts, and remotely
control the pumping operations. The type used in this project is the
Thingspeak server.
8. Process Plant: The water tank represents the storage vessel where water is
held within the process plant. It can be a reservoir, storage tank, or any other
container that holds the water.
From the block diagram in previous subsection, this seminar report is concerned
with the connection of the water level sensor to the ATmega328.
The water level sensor used is the Ultrasonic sensor in which the output is a
serial communicator and it has a signal conditioner embedded in it. The sensor
is used to measure the water level in a tank and sends the signal collected to the
microcontroller. The main function of the signal conditioner is to amplify and
filter the signal generated by the ultrasonic transducer, which converts the sound
waves into electrical signals. The microcontroller then uses the signal or data
received from the sensor to monitor the water level data on a display interface
which can be the Proteus software virtual terminal or Arduino IDE serial
monitor.
16
3.4 Interfacing of HC-SR04 Ultrasonic Sensor to ATmega 328
Microcontroller using Proteus Design Suite
The HC-SR04 ultrasonic distance sensor has 4 Pins: Vcc, GND, Trigger and
Echo. The Vcc and GND are used to power up the sensor and are connected to
power and ground rails on Arduino UNO.
1. Download the Arduino UNO and Ultrasonic Library for Proteus and
extract it to Proteus program file.
2. Load Proteus and add the two components.
3. Connect the VCC and GND pins of the ultrasonic sensor to the 5V and
GND pins of the ATmega328 microcontroller, respectively.
17
4. Connect the trigger pin of the ultrasonic sensor to any digital output pin
of the ATmega328 microcontroller. In this example, we will use PIN 5
as the trigger pin.
5. Connect the echo pin of the ultrasonic sensor to any digital input pin of
the ATmega328 microcontroller. In this example, we will use PIN 4 as
the echo pin.
6. Write the code on the Arduino IDE to control the ultrasonic sensor using
the ATmega328 microcontroller. The code should include the following
steps:
a. Send a 10us pulse to the trigger pin of the ultrasonic sensor. Wait for
the pulse to return to the echo pin.
b. Calculate the distance between the sensor and the object using the
time it took for the pulse to return.
7. Compile the code and copy the hex file-path.
8. On Proteus, Double click on the Arduino board to insert the hex file of
code (Shah, 2020).
9. Connect an active variable resistor (POT-HG) to the test pin of the
Ultrasonic sensor and connect its terminals to ground and 5v. This
allows for change of resistance during the simulation time which
changes distances being measured.
10. Connect the Virtual Terminal to the Arduino board:
a. The Rx pin of the Arduino is connected to the Tx pin of the Virtual
Terminal and the Tx pin of the Arduino is connected to the Rx pin
of the Virtual Terminal (Mirza, 2019).
11. Hit the simulation button and the data that is received from the sensor is
displayed continously on the Terminal window.
18
3.4.2 Flowchart for Ultrasonic Sensor Distance Measurements
Start
Initialize time to
be zero (t=0)
Has Echo
NO returned?
YES
End
Using the Arduino IDE, the program code for the project was written:
// defines variables
long duration;
19
int distance;
void setup() {
void loop() {
digitalWrite(trigPin, LOW);
delayMicroseconds(3000);
digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 3000 micro
seconds
delayMicroseconds(3000);
digitalWrite(trigPin, LOW);
Serial.print(distance);
Serial.print(" cm ");
Serial.println();
delay(100);
20
CHAPTER FOUR
RESULTS
The HC-SR04 ultrasonic sensor has a maximum detection range of 400cm and
minimum detection range of 2 cm, hence it is unable to pick up anything closer
or farther than this. A 2 cm blind spot exists as well, which is the space between
the sensor and the transducer. The HC-SR04 ultrasonic sensor picks up items at
an angle of around 15 degrees that are conical in shape. This indicates that things
inside of this cone-shaped range can be detected by the sensor, but those outside
of it might not.
After designing the circuit on Proteus, I was able to monitor the distances
measured on the virtual terminal and also able to vary the distances by
connecting an active variable resistor to the test pin of the ultrasonic sensor such
that when I vary the resistance, the distance value changes. The diagram below
shows the readings displayed on the virtual terminal after running the
simulation.
Based on results obtained, it is clear that the Ultrasonic sensor can be used for
water level sensing. However, thie result is to be validated practically in the
main project.
21
Fig. 4.1 Distance readings displayed on virtual terminal of proteus simulation
software
22
CHAPTER FIVE
CONCLUSION
This work is the core part of the overall project, it is the main mechanism that
drives the entire system. The main contribution of this seminar is that it
developed an algorithm that can be used to sense water level which can be
applied in the major project.
The investigation showed the possibility that distance data from the ultrasonic
sensor can be turned into actual water level readings, which can be utilized to
operate the water pumping system, by interfacing the sensor to an ATmega328
microcontroller.
There are some challenges that were encountered in the course of this work.
There are listed below, with corresponding recommendations on how they were
solved:
23
5.3 Conclusion
This study investigated the possibility of the use of ultrasonic sensor in water
level measurement by interfacing the HC-SR04 ultrasonic sensor to
ATmega328 using Arduino UNO development board, developing an algorithm
for reading and displaying the sensor values, simulating the system using
proteus simulation software and displaying the results on a virtual terminal. It
is hoped that this possibility will be validated in the ongoing final year project.
24
REFERENCES
Helen. (2021, June 15). Water Level Sensor Definition, Applications, Benefits
and Types. Retrieved from Renkeer.
Kim, H. J., & Lee, J. H. (2016). A review of HC-SR04 ultrasonic sensor for
obstacle detection in autonomous vehicles. Journal of Institute of
Control, Robotics and Systems, 22(3), 205-211.
Liu, J., Hu, H., Zhang, Q., & Xu, B. (2019). A comprehensive review on water
level sensors . Journal of Sensors, 1-13.
25
Shah, S. (2020, May 8). How to Simulate Arduino Projects Using Proteus.
Retrieved from Maker Pro: https://maker.pro/arduino/projects/how-to-
simulate-arduino-projects-using-proteus
Shawn. (2019). Atmega, The One Microcontroller You Should Start With.
Retrieved from SeeedStudio Blog:
https://www.seeedstudio.com/blog/2019/10/22/atmega328p-the-one-
microcontroller-you-should-start-
with/#:~:text=ATmega328P%20is%20a%20high%20performance,Ard
uino%20Fio%20and%20Arduino%20Uno
26