You are on page 1of 35

A SEMINAR REPORT ON

INTERFACING ULTRASONIC SENSOR TO ATMEGA 328


MICROCONTROLLER
BY

MOFFAT EMEDIONG AKPABONG


20171038413

ROBOTICS AND AUTOMATION OPTION

SUBMITTED TO

DEPARTMENT OF MECHATRONICS ENGINEERING


SCHOOL OF ELECTRICAL SYSTEMS ENGINEERING
AND TECHNOLOGY

FEDERAL UNIVERSITY OF TECHNOLOGY, OWERRI

IN PARTIAL FULFILMENT OF THE REQUIREMENTS


FOR THE

AWARD OF

BACHELORS OF ENGINEERING DEGREE (B. ENG) IN

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:…………………….

Engr. Dr. S. E. Mbonu


MCE Seminar Coordinator

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 TABLES viii

LIST OF FIGURES ix

CHAPTER ONE 1

INTRODUCTION 1

1.1 Background of the Work 1


1.2 Problem Statement 1
1.3 Aim and Objectives 1
1.4 Justification of the Work 2
1.5 Scope of Work 2

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

Table 2.1: Features of ATmega328 Microcontroller 7

viii
LIST OF FIGURES

Fig. 2.1: ATmega328 chip 6

Fig. 2.2: ATmega328 pinouts 8

Fig. 2.3: Block Diagram for ATmega328 9

Fig. 2.4: An HC-SR04 Ultrasonic Sensor 10

Fig. 2.5: Mode of Operation of an Ultrasonic sensor 10

Fig. 2.6: HC-SR04 Ultrasonic Sensor Pinouts 12

Fig. 3.1: Block Diagram of a Smart Water Pumping System with IOT
monitoring and Metering 14

Fig. 3.2: Block Diagram for Interfacing a Water Level Sensor to


Atmega328 Microcontroller 16

Fig. 3.3: Circuit Diagram of Interfacing a Water Level Sensor to


ATmega328 Microcontroller on Proteus 17

Fig. 3.4: Flowchart Diagram for Ultrasonic Sensor Distance Measurements 19

Fig. 4.1: Distance Readings Displayed on Virtual Terminal of Proteus


Simulation Software 22

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.

1.2 Problem Statement

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.3 Aim and Objectives

The aim of this seminar is to interface ultrasonic sensor to ATmega328


microcontroller. In order to achieve the aim the research efforts shall be geared
towards the following objectives:

1. To interface the HC-SR04 ultrasonic sensor to ATmega328 using


Arduino UNO development board.
2. To develop an algorithm for reading and displaying the sensor values.
3. To simulate the system using proteus simulation software.
4. To document results.

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.

1.5 Scope of Work

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

There is a whole class of research work dedicated to the interfacing of ultrasonic


sensor to atmega 328 microcontroller. There have been different proposals and
designs to achieve this work. The ultrasonic sensor has been applied primarily
to measure distances, the use of it in water level sensing is not common. This
section reviews water level sensors, the efficency of using ultrasonic in
measuring water level and the review of the microcontroller used to interface
the sensor.

2.1 Overview of Water Level Sensors

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.

2.1.1 Optical Water Level Sensor

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.

Advantages: non-contact measurement, high accuracy, and fast response.

3
Disadvantages: Not to be used under direct sunlight, water vapor affects the
measurement accuracy (Helen, 2021).

2.1.2 Capacitance Liquid Level Sensor


Capacitance level switches use 2 conductive electrodes (usually made of metal)
in the circuit, and the distance between them is very short. When the electrode
is immersed in the liquid, it completes the circuit.

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.

Disadvantages: The corrosion of the electrode will change the capacitance of


the electrode, and it needs to be cleaned or recalibrated (Helen, 2021).

2.1.3 Float Level Sensor

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.

2.1.4 Radar Level Gauge

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.

Advantages: wide application range, not affected by temperature, dust, steam,


etc.

Disadvantages: It is easy to produce interference echo, which affects the


measurement accuracy (Helen, 2021).

2.1.5 Diaphragm Liquid Level Sensor

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.

Disadvantages: Since it is a mechanical device, it will need maintenance over


time (John, 2017).

2.1.6 Tuning Fork Level Sensor

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.

Disadvantages: Cannot be used in viscous media (John, 2017).

2.1.7 Ultrasonic Liquid Level Sensor

The ultrasonic level gauge is a digital level gauge controlled by a


microprocessor. In the measurement, the ultrasonic pulse is emitted by the
sensor (transducer). The sound wave is reflected by the liquid surface and

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.

Advantages: non-contact measurement, fast response time and the measured


medium is almost unlimited, and it can be widely used for measuring the height
of various liquids and solid materials.

Disadvantages: The measurement accuracy is greatly affected by the


temperature and dust of the current environment (John, 2017).

2.2 Review of ATmega328 Microcontroller

Fig. 2.1 ATmega328 chip

The ATmega328 is a microcontroller chip that was created by the Atmel


Corporation and is a member of the AVR family of microcontrollers. Microchip
Technology Inc. eventually purchased Atmel Corporation. The ATmega328,
which is the ATmega168's replacement, is a well-liked component for
embedded systems and do-it-yourself electronics projects.
ATmega328 is an Advanced Virtual RISC (AVR) microcontroller. It supports
8-bit data processing. ATmega-328 has 32KB internal flash memory.
ATmega328 has 1KB Electrically Erasable Programmable Read-Only Memory
(EEPROM). This property shows if the electric supply supplied to the micro-
controller is removed, even then it can store the data and can provide results
after providing it with the electric supply. Moreover, ATmega-328 has 2KB
Static Random Access Memory (SRAM). Other characteristics will be
explained later. ATmega 328 has several different features which make it the
6
most popular device in today's market. These features consist of advanced RISC
architecture, good performance, low power consumption, real timer counter
having separate oscillator, 6 PWM pins, programmable Serial USART,
programming lock for software security, throughput up to 20 MIPS etc.
The Arduino UNO is based on the ATmega 328 microcontroller, it is the UNO’s
heart. Its excellent features include cost-efficiency, low power dissipation,
programming lock for security purposes, real timer counter with separate
oscillator (Nasir, 2017).

Table 2.1 Features of ATmega328 microcontroller

Parameter Value

CPU type 8-bit AVR

Maximum CPU speed 20 MHz

Performance 20 MIPS at 20 MHz

Flash memory 32 KB

SRAM 2 KB

EEPROM 1 KB

Package pin count 28 or 32

Capacitive touch sensing channels 16

Maximum I/O pins 23

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:

VCC is a digital voltage supply.

AVCC is a supply voltage pin for analog to digital converter.

GND denotes Ground and it has a 0V.

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.

Fig. 2.2 ATmega328 pinouts

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).

Fig. 2.3 Block diagram for ATmega328

Specifications of Atmega 328:

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

Fig. 2.4 An 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).

The sensor is composed of two ultrasonic transducers. One is transmitter which


outputs ultrasonic sound pulses and the other is receiver which listens for
reflected waves. It’s basically a SONAR which is used in submarines for
detecting underwater objects (Dejan, 2015).

2.3.1 Principle of Operation of an Ultrasonic Sensor

Fig. 2.5 Mode of operation of an ultrasonic sensor

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:

Distance = Speed x Time

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.

Distance = (Speed x Time) / 2 = (34cm/ms x 1.5ms) / 2 = 25.5cm.

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.

Here are its main specifications:

Operating Voltage - 5V DC

Operating Current - 15mA

Operating Frequency - 40KHz

Min Range - 2cm / 1 inch

Max Range - 400cm / 13 feet

Accuracy - 3mm

Measuring Angle - <15°

Dimension - 45 x 20 x 15mm

Fig. 2.6 HC-SR04 ultrasonic sensor pinouts

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

A review paper published by Oluwole et al. (2017) provides an overview of the


principles and applications of various water level sensors, with a focus on the
use of these sensors in smart water management systems. The paper discusses
the challenges and opportunities of water level sensing in the context of
sustainable water resource management (Oluwole, Adeosun, & Akanbi, 2017).

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).

A paper by Srividhya et al. (2015) provides an overview of the ATmega328


microcontroller, including its architecture, memory organization, and
peripherals. The authors also discuss various applications of the microcontroller
in the fields of robotics, automation, and embedded systems. One Limitation is
that the paper does not provide a detailed discussion on the programming of the
microcontroller, which is a crucial aspect of its usage. A research gap identified
is the need for more empirical studies on the performance of the microcontroller
in real-world scenarios (Srividhya, Gowthaman, & Ramesh Kumar, 2015).

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

MATERIALS AND METHOD

3.1 Materials Used

To interface an ultrasonic sensor with an ATmega328 microcontroller, the


following are required:

A. HC-SR04 Ultrasonic Sensor and Library


B. ATmega 328 microcontroller embedded in the Arduino UNO and
Library
C. Proteus Software
D. Arduino IDE

3.2 Description of Block Diagram of the Project

Signal ASIC Driver


Conditioner ATmega328

GPRS IoT Gateway Actuator

IoT Cloud Monitoring Process Water Level


Platform Plant Sensor

Fig. 3.1 Block diagram of a smart water pumping system with IoT monitoring and
metering

The project title is “SMART WATER PUMPING SYSTEM WITH IOT


MONITORING AND METERING” .

Description of the different blocks in the diagram above:

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.

3.3 Description of Block Diagram for Interfacing Level Sensor to


ATmega328 Microcontroller

From the block diagram in previous subsection, this seminar report is concerned
with the connection of the water level sensor to the ATmega328.

Water Level Signal ASIC Display


Sensor Conditioner ATmega32
8
Fig. 3.2 Block diagram for interfacing a water level sensor to ATmega328
microcontroller

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 simulation was designed on proteus software and is shown below:

Fig. 3.3 Circuit diagram of interfacing a water level sensor to ATmega328


microcontroller on Proteus

3.4.1 Design Procedures:

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.

To interface an ultrasonic sensor with the ATmega328 microcontroller, we need


to connect the sensor to the microcontroller's input/output (I/O) pins. The
sensor's transmitter pin is connected to the microcontroller's output pin, and the
sensor's receiver pin is connected to the microcontroller's input pin
(Elementzonline, 2017). We use the following steps:

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)

Send out sound wave

Wait for Echo to return

Has Echo
NO returned?

Wait for time out YES


Compute Distance using
the formula = (s x t)/2
NO
Is time
out? Display the results

YES
End

Fig. 3.4 Flowchart diagram for ultrasonic sensor distance measurements

3.4.3 Program Code

Using the Arduino IDE, the program code for the project was written:

// defines pins number

const int trigPin = 5;

const int echoPin = 4;

// defines variables

long duration;

19
int distance;

void setup() {

Serial.begin(9600); // Starts the serial communication

pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output

pinMode(echoPin, INPUT); // Sets the echoPin as an Input

void loop() {

// Clears the trigPin

digitalWrite(trigPin, LOW);

delayMicroseconds(3000);

digitalWrite(trigPin, HIGH); // Sets the trigPin on HIGH state for 3000 micro
seconds

delayMicroseconds(3000);

digitalWrite(trigPin, LOW);

duration = pulseIn(echoPin, HIGH); // Reads the echoPin, returns the sound


wave travel time in microseconds

distance= duration*0.034/2; // Calculating the distance

Serial.print("Distance: "); // Prints the distance on the Serial Monitor

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.

The working model of the proposed distance measurement system using


ultrasonic sensor was successfully designed and implemented. This
implementation has been the readily used in the fast growing electronic
applications.

4.1 Description of Results

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

5.1 Contributions of Work to Overall Project Actualization

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.

5.2 Challenges and Recommendations

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:

a) Power Issues: Power outage was experienced when simulating the


design and preparing the report for this work. A backup power source
was used to continue the work. It is recommended that university
environment be equipped with constant supply of power.
b) Programming the microcontroller to read sensor data and display the
distance data presented another difficulty. The use of pre-written code
and libraries were used to solve the problem. It is recommended that
open-source platforms should be used in protype development.
c) Another recommendation is that this work was simulated and has to be
implemented physically to validate the results.

23
5.3 Conclusion

There is possibility of using ultrasonic sensor to measure water level, though it


was not specifically designed for the purpose.

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

Dejan. (2015). Ultrasonic Sensor HC-SR04 and Arduino-Complete Guide.


Retrieved from How To Mechatronics:
https://howtomechatronics.com/tutorials/arduino/ultrasonic-sensor-hc-
sr04/

Elementzonline. (2017, December 6). Design Analysis Interfacing Ultrasonic


Sensor With Arduino. Retrieved from ElementzTech Blog:
https://elementztechblog.wordpress.com/2017/12/06/interfacing-
ultrasonic-sensor-with-arduino/

Helen. (2021, June 15). Water Level Sensor Definition, Applications, Benefits
and Types. Retrieved from Renkeer.

John. (2017). Water Level Sensors. Retrieved from OurPCB:


https://www.ourpcb.com/water-level-sensors.html

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.

Mirza, B. K. (2019, April 3). Virtual Terminal in Proteus. Retrieved from


ProjectIoT123: https://projectiot123.com/2019/04/03/virtual-terminal-
in-proteus/

Nasir, S. Z. (2017, August 4). Introduction to Atmega. Retrieved from The


Engineering Projects:
https://www.theengineeringprojects.com/2017/08/introduction-to-
atmega328.html

Oluwole, O. O., Adeosun, O. S., & Akanbi, A. O. (2017). A review of water


level sensors and their applications in smart water management
systems. International Journal of Advanced Research in Electrical,
Electronics and Instrumentation Engineering, 6(5), 4171-4181.

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

Srividhya, N., Gowthaman, N., & Ramesh Kumar, V. (2015). A Review on


ATmega328 Microcontroller. International Journal of Advanced
Research in Computer Science and Software Engineering, 5(7), 384-
389.

26

You might also like