Professional Documents
Culture Documents
MGCP Report
MGCP Report
On
DISTANCE MEASUREMENT USING ULTRASONIC SENSOR
WITH
ESP32 AND MICRO PYTHON
Submitted in partial fulfilment of the
Requirements for the award of the Degree of
Bachelor of Technology
in
ELECTRICAL AND ELECTRONICS ENGINEERING
Submitted By
M. Ajay kumar (2100069071)
Under guidance of
Dr . SVNL LALITHA
Associate. Professor
Dept. of EEE
KLEF
Green Fields, Vaddeswaram , Guntur District-522 502
2022-2023
1|Page
KLEF
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
This is to certify that this mid grad capstone project report entitled “Distance Measurement
using Ultrasonic Sensor with ESP32 and Micro Python “is a bonafide work done by M.
Ajay kumar (2100069071) in partial fulfilment of the requirements for the award of degree in
BACHELOR OF TECHNOLOGY in ELECTRICAL AND ELECTRONICS
ENGNEERING during the Academic year 2022-2023.
2|Page
KLEF
DEPARTMENT OF ELECTRICAL AND ELECTRONICS ENGINEERING
I hereby declare that this mid grad capstone project report title “Distance Measurement using
Ultrasonic Sensor with ESP32 and Micro Python” has been prepared by us in partial
fulfilment of the requirements for the award of degree “BACHELOR OF TECHNOLOGY in
ELECTRICAL AND ELECTRONICS ENGINEERING” during the Academic year 2022-
2023
We also declare that this mid grad capstone project report is of our own efforts and it has not
been submitted to any other university for the award of any degree.
3|Page
ACKNOWLEDGEMENT
My sincere thanks to Dr. SVNL LALITHA in the Lab for their outstanding support
throughout the project for the successful completion of the work.
I express my gratitude to Dr. A. Pandian, Head of the Department for Electrical and
Electronics Engineering for providing us with adequate facilities, ways and means by which I
able to complete this mid grad capstone project.
I would like to place on record the deep sense of gratitude to the honourable Vice Chancellor,
K L Deemed to be University for providing the necessary facilities to carry the mid grad
capstone project.
Last, but not the least, I thank all Teaching and Non-Teaching Staff of our department and
especially my classmates and my friends for their support in the completion of my mid grad
capstone project.
4|Page
ABSTRACT
This project demonstrates the seamless integration of ultrasonic sensors with the ESP32
microcontroller using Micro Python, enabling precise distance measurement. Ultrasonic
sensors, known for emitting and detecting ultrasonic waves, serve as the cornerstone for
accurately calculating distances based on wave reflection times. We delve into the
implementation of sophisticated distance measurement algorithms, emphasizing their vital
role in the realms of IoT and robotics. Leveraging the ESP32's robust capabilities and
programming flexibility offered by Micro Python, this project achieves a high level of
accuracy in distance measurement. We further explore potential real-world applications,
showcasing the versatility and adaptability of this technology stack. This endeavour
underscores the potential impact of such integrations in revolutionizing various industries,
paving the way for enhanced automation, safety, and efficiency.
5|Page
CONTENTS
Page No
CHAPTER 1: INTRODUCTION
1.1 Defining ESP32 8
1.2 Main Components And Working 9
CHAPTER 2: LITERATURE SURVEY 10
CHAPTER 3: CHAPTER 3
OVERVIEW ON DISTANCE MEASUREMENT USING ULTRASONIC SENSOR
WITH ESP32 AND MICRO PYTHON
3.1 Architecture Of Project 13
3.2 Components Required 14
CHAPTER 4: MODELING OF DISTANCE MEASUREMENT USING ULTRASONIC
SENSOR WITH ESP32 AND MICRO PYTHON
4.1 Practical Implementation 17
4.2 Steps To Download Thonny 19
4.3 Problems Faced 19
4.4 Flow Chart And Block Diagram 22
4.5 Model Output 23
4.6 Thonny Code 23
APPLICATIONS OF DISTANCE MEASUREMENT USING ULTRASONIC SENSOR
WITH ESP32 AND MICRO PYTHON 28
CONCLUSION 29
FUTURE SCOPE 30
REFERENCES 31
6|Page
CHAPTER 1
INTRODUCTION
Distance measurement is a fundamental requirement in various domains, including
robotics, automation, and IoT applications. Accurate and reliable distance measurements play
a critical role in enabling autonomous navigation, obstacle detection, object positioning, and
environmental monitoring. One of the prominent technologies for distance measurement is
the use of ultrasonic sensors.
Ultrasonic sensors operate on the principle of emitting ultrasonic waves and measuring the
time it takes for these waves to bounce back after hitting an object. This project focuses on
integrating ultrasonic sensors with the ESP32 microcontroller, a versatile and powerful
platform known for its dual-core processing and built-in Wi-Fi and Bluetooth capabilities.
The integration is facilitated through Micro Python, a lightweight and efficient
implementation of Python optimized for microcontrollers.
In this project, we aim to demonstrate the seamless integration of ultrasonic sensors with the
ESP32 using Micro Python for precise distance measurement. The ESP32's robust
capabilities and the flexibility of Micro Python offer a conducive environment for accurate
distance measurement and pave the way for innovative applications in the domains of IoT,
robotics, and automation. This integration holds the promise of contributing to advancements
in technology and enhancing the functionality of various real-world applications.
7|Page
ULTRASONIC SENSOR
Distance L = 1/2 × T × C
Where L is the distance, T is the time between the emission and reception, and C is the sonic
speed .
8|Page
ESP32
This microcontroller comes equipped with a rich set of peripherals and interfaces, including
GPIO pins, SPI, I2C, UART, and more, enabling seamless integration with a variety of
sensors and devices. Its low power consumption features make it apt for battery-powered
applications and extend its applications into the realm of wearables and other energy-efficient
solutions. Additionally, the ESP32 provides secure boot, flash encryption, and secure storage,
ensuring data and code security.
What makes the ESP32 even more appealing is its flexibility in programming. Developers
can choose from multiple programming environments such as Arduino IDE, Micro Python,
Espresso IDF, and Platform IO. Its compatibility with Micro Python simplifies and
accelerates the development process, making it a popular choice for quick prototyping and
product development. The ESP32 has a vibrant and active community that continuously
contributes to its extensive libraries, tutorials, and projects, making it a favourite among both
beginners and experienced developers. Overall, the ESP32's amalgamation of robust
processing, integrated wireless connectivity, rich interfaces, and ease of programming make it
a preferred microcontroller for a diverse range of IoT and embedded applications.
9|Page
Fig 1.2 ESP32
10 | P a g e
CHAPTER 2
LITERATURE SURVEY
A radar system has a transmitter that emits radio waves called a radar signals
in :reflected or scattered in many directions Example:- let us take example for bat Bat
released the eco sound while travelling .if any object came in middle and it reflect back to the
bat
The development of the radar technology took place during the World War II in which
it was used for detecting the approaching aircraft and then later for many other purposes
which finally led to the development of advanced military radars being used these days.
Military radars have a highly specialized design to be highly mobile and easily transportable,
by air as well as ground. Military radar should be an early warning, altering along with
weapon control functions. It is specially designed to be highly mobile and should be such that
it can be deployed within minutes.
A literature survey involves reviewing existing research, publications, and academic work
related to a specific topic. In the context of "Distance Measurement using Ultrasonic Sensor
with ESP32 and Micro Python," the literature survey would encompass a review of relevant
research papers, articles, books, and online resources focusing on ultrasonic sensors, ESP32
microcontroller, Micro Python, and distance measurement applications. It aims to gather
11 | P a g e
knowledge, identify existing methodologies, understand challenges, and potentially discover
gaps or areas for further exploration. Below is a brief overview of what a literature survey for
this project could entail:
- Research papers and articles explaining the principles of ultrasonic sensors, signal
processing techniques, and methods for accurate distance measurement using ultrasonic
waves.
- Literature detailing the use of Micro Python for embedded programming, its advantages,
supported microcontrollers, and case studies demonstrating its efficacy in real-world
applications.
- Research articles illustrating how ultrasonic sensors are utilized in IoT devices, robotics,
automation, and other applications, with a focus on distance measurement and obstacle
detection.
- Reviews of practical projects similar to the intended project, where ultrasonic sensors are
integrated with microcontrollers for distance measurement, highlighting lessons learned, best
practices, and innovations.
7. Comparative Studies:
12 | P a g e
- Comparative analyses of various distance measurement techniques, including ultrasonic,
infrared, and laser-based methods, to understand the strengths and weaknesses of each
approach
CHAPTER 3
Components Required:
In this project we have used the Arduino and ultrasonic sensor along with the jumping
wires and the relay motors and details list of the hardware components are
This chapter initiates with a thorough introduction to ultrasonic sensors and their pivotal role
in measuring distances. We elucidate the underlying principles that govern the functioning of
ultrasonic waves, their propagation, reflection, and how this forms the basis for calculating
distances.
Delving deeper into the operational intricacies, we explain how ultrasonic sensors function,
elaborating on wave generation, transmission, reflection, and the crucial aspect of time-of-
flight measurement. This section sheds light on the fundamental science that enables
ultrasonic sensors to measure distances accurately.
13 | P a g e
ESP32 Microcontroller for Sensor Integration:
Next, we shift our focus to the ESP32 microcontroller, a vital component of this project. We
present a comprehensive overview of its features and capabilities, emphasizing its seamless
integration with ultrasonic sensors. Additionally, we discuss the necessary hardware
configurations and connections required for effective interfacing.
In this segment, we introduce Micro Python, a powerful tool for embedded programming. We
outline its advantages and suitability for ESP32-based projects. Furthermore, we provide
insights into essential programming constructs and syntax within Micro Python relevant to
our distance measurement application.
Moving on, we detail the practical steps for interfacing ultrasonic sensors with the ESP32
microcontroller. We emphasize the necessary wiring, connections, and configurations to
ensure a robust hardware setup. Furthermore, we showcase the software setup essential for
seamless communication between the ESP32 and the sensors.
14 | P a g e
Fig 3.1 ARCHITECTURE OF PROJECT
15 | P a g e
CHAPTER 4
PRACTICAL IMPLEMENTATION
For this project's hardware implementation, we outline the essential components required,
primarily focusing on the ultrasonic sensors, ESP32 microcontroller, and connecting wires.
The ultrasonic sensors, pivotal for distance measurement, are typically HC-SR04 or similar
models. Likewise, an ESP32 development board serves as the core microcontroller.
Connecting wires, particularly male-to-female jumper wires, are essential for establishing
electrical connections between components.
To guide through the physical assembly, we break down the process into a step-by-step
approach, aiming for clarity and ease of implementation. Firstly, it's crucial to understand the
pin configuration of the ultrasonic sensors, comprising VCC (Power), GND (Ground), TRIG
(Trigger), and ECHO (Echo). Connecting the VCC and GND of the ultrasonic sensors to the
appropriate pins on the ESP32 for power supply is the initial step. Subsequently, the TRIG
and ECHO pins of each ultrasonic sensor must be connected to specified GPIO pins on the
ESP32. For instance, the TRIG and ECHO pins of the first ultrasonic sensor could be
connected to GPIO pins 14 and 12, respectively, while those of the second sensor might be
connected to GPIO pins 26 and 27.
Double-checking the connections for accuracy and tightness is a prudent practice to prevent
loose connections during operation. The physical arrangement involves strategically placing
the ultrasonic sensors in a position conducive to accurate distance measurement according to
the project's requirements. Once the connections are validated, powering up the ESP32
microcontroller allows for a quick verification of the connectivity of the ultrasonic sensors.
16 | P a g e
reference point for further stages of the project. This meticulous hardware setup lays the
foundation for seamless software development and subsequent testing, culminating in an
accurately functioning distance measurement system.
https://thonny.org/: link
17 | P a g e
Fig 4.1 Tonny download.
To begin the process of installing Micro Python on an ESP32 microcontroller, the first step is
to acquire Thonny, a user-friendly Python Integrated Development Environment (IDE).
Thonny simplifies the installation and management of Micro Python firmware. After
downloading and installing Thonny from its official website, the ESP32 board is connected to
the computer via a USB cable. Once Thonny is opened, under the "Interpreter" menu,
selecting "Micro Python (ESP32)" prompts Thonny to guide the user through the firmware
installation on the ESP32.
Thonny automatically detects the connected ESP32 board and presents a list of available
boards to choose from. The appropriate ESP32 board is selected from the list, and the user
proceeds to install the Micro Python firmware. Thonny initiates the firmware flashing
process, and upon completion, the Micro Python firmware is successfully installed on the
ESP32. To verify the installation's success, Thonny displays a Read-Eval-Print Loop (REPL)
prompt, enabling the execution of Micro Python commands directly. A simple command,
such as printing a statement, confirms the successful installation. Subsequently, users can
develop and run Micro Python scripts on the ESP32 board using Thonny, opening doors for a
multitude of IoT and embedded projects.
18 | P a g e
STEPS FOR UPDATING MICROPYTHON FRAMEWORK IN ESP32
Connect your ESP32 development board to your computer using a USB cable.
In Thonny, go to the "Interpreter" menu and select "Micro Python (ESP32)" from the list of
available interpreters.
Thonny will prompt you to install Micro Python firmware on your ESP32.
Thonny will now proceed to flash the Micro Python firmware onto your ESP32 board.
7.Verify Installation:
After the firmware is successfully flashed, Thonny will display a prompt (REPL - Read-Eval-
Print Loop) where you can execute Micro Python commands directly.
19 | P a g e
Verify the installation by running a simple command, for example:
python
Copy code
You can now create, upload, and run Micro Python scripts on your ESP32 using Thonny.
By following these steps, you'll have Micro Python successfully installed on your ESP32
microcontroller using Thonny, enabling you to develop and run MicroPython scripts for
various IoT and embedded projects.
20 | P a g e
THONNY CODE
__version__ = '0.2.0'
class HCSR04:
"""
The timeouts received listening to echo pin are converted to OSError('Out of range')
"""
"""
echo_pin: Readonly pin to measure the distance. The pin should be protected with 1k
resistor
"""
self.echo_timeout_us = echo_timeout_us
21 | P a g e
self.trigger.value(0)
def _send_pulse_and_wait(self):
"""
We use the method `machine.time_pulse_us()` to get the microseconds until the echo is
received.
"""
time.sleep_us(5)
self.trigger.value(1)
time.sleep_us(10)
self.trigger.value(0)
try:
return pulse_time
raise ex
def distance_mm(self):
22 | P a g e
"""
"""
pulse_time = self._send_pulse_and_wait()
return mm
def distance_cm(self):
"""
It returns a float
"""
pulse_time = self._send_pulse_and_wait()
23 | P a g e
cms = (pulse_time / 2) / 29.1
return cms
24 | P a g e
Applications of Distance Measurement Using
Ultrasonic Sensor with Esp32 and Micro Python
- Employing ultrasonic sensors for real-time obstacle detection allows robots to navigate and
manoeuvre in their environment, enhancing their autonomy and safety.
- Implementing the project in automotive applications can aid drivers in parking by providing
accurate distance information, reducing the risk of collisions and enhancing parking
precision.
- Utilizing the distance measurement capabilities for object positioning and monitoring in
industrial automation enhances efficiency and optimizes manufacturing processes.
5. Healthcare:
- Integrating ultrasonic sensors in healthcare devices can assist in patient monitoring, fall
detection systems for the elderly, and contactless measurement of vital signs.
- Implementing the technology for measuring distances in agricultural settings helps optimize
plant positioning, automate irrigation systems, and enhance overall farm management.
- Utilizing the ESP32 and ultrasonic sensors for monitoring traffic flow and vehicle density in
smart city applications can aid in traffic management and planning.
25 | P a g e
- Incorporating the project in retail settings allows for automated customer traffic monitoring
and queue management, improving the overall shopping experience.=
CONCLUSION
The project focusing on distance measurement using ultrasonic sensors with the
ESP32 microcontroller and MicroPython has been successfully executed, showcasing the
effective integration of hardware and software to achieve accurate distance measurements.
Through this project, we have explored the fundamentals of ultrasonic technology, the
versatility of the ESP32 microcontroller, and the efficiency of MicroPython in embedded
programming.
26 | P a g e
Future Scope
Multi-Sensor Fusion:
Energy-Efficiency Optimization:
Further develop the project to create advanced obstacle avoidance algorithms for
robotic platforms, allowing for more dynamic and efficient navigation in complex
environments.
Integrate the system with mobile applications, allowing users to access distance
measurements and control devices remotely through their smartphones.
27 | P a g e
REFERENCES
[1] http://www.atmel.com/atmega328/
[2] http://en.wikipedia.org/wiki/File:16MHZ_Crystal.jpg
https://www.arduino.cc/en/software
Peruri , Sreenivasulu & Tarun, Boya. (2020). Radar detection using ultrasonic sensor.
5. 136-138.
28 | P a g e