0% found this document useful (0 votes)
24 views19 pages

ES Project Report Group 2

Uploaded by

mananbhatia243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views19 pages

ES Project Report Group 2

Uploaded by

mananbhatia243
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

Distance Measurement & Obstacle Detection

A project report submitted to

MANIPAL ACADEMY OF HIGHER EDUCATION

For Partial Fulfilment of the Requirement for the Award of the Degree of

Bachelor of Technology

in

Information Technology

by

Ayush Mishra 210911026

Kirtenya Maheshwari 210911029

Abhijeet Gupta 210911030

Under the guidance of

Dr. Rashmi N R Ms. Pooja S

Associate Professor Assistant Professor


Department of I&CT Department of I&CT
Manipal Institute of Technology Manipal Institute of Technology
Manipal, Karnataka, India Manipal, Karnataka, India

1
INTRODUCTION

This project focuses on the development of an Ultrasonic Distance


Measurement and Object Detection System using an LPC1768 micro controller. The
micro controller interfaces with an ultrasonic sensor to measure the distance between the
sensor and an object, providing valuable data for object detection.
The core functionality involves utilizing the ultrasonic sensor to measure distances
based on the time taken for the ultrasonic waves to travel to an object and back. The
Micro-controller processes this information, calculates the distance.If the distance is
within a certain range, it will be indicated using the LED.

2
PROBLEM STATEMENT
Interfacing the Ultrasonic distance sensor HC SR04 to LPC1768 micro controller to
measure the distance between the sensor and the object.

CONTENTS:

Sl.No Particulars Page No

1. Components Required 5
and Methodology

2. Detailed diagram with 8


pin details

3. Code 9

4. Snapshots of output 16

COMPONENTS REQUIRED
3
Hardware Requirements:

1. HC-SR04 Ultrasonic Distance Sensor - The HC-SR04 Ultrasonic Distance


Sensor operates on the principle of sending and receiving ultrasonic pulses to
measure distance accurately. It has a measuring range of 2 cm to 400 cm with
a resolution of around 3 mm. The sensor typically operates at 5V and has four
pins: VCC, Trig, Echo, and GND. To obtain a distance measurement, the Trig
pin is triggered, and the sensor calculates the time it takes for the ultrasonic
pulse to travel to an object and back.

Fig 1:Ultrasonic sensor

2. LPC 1768 micro controller kit- The LPC1768 is a 32-bit micro controller
from NXP Semiconductors, part of the LPC1700 series. It is based on the
ARM Cortex-M3 architecture, offering high performance and low power
consumption. With a clock speed of up to 100 MHz, it features a rich set of
peripherals, including multiple UARTs, SPI, I2C, and USB interfaces. The
LPC1768 is commonly used in embedded systems and applications requiring
a robust and versatile micro controller platform. It supports a wide range of
development tools, making it popular for both prototyping and production..

3. FRC Cables- Connect a 10 core FRC cable from CNA to CNA5.

4
Fig 2:FRC cable
4. Jumper Cables - These cables are used to make sophisticated connections
between the kit and the sensor. Four female to female jumper cables are used
in the setup.

Software Requirements:

1. Keil microvision4 simulator


2. Flash Magic

5
METHODOLOGY

• The distance measurement in this code is facilitated by the HC-SR04 Ultrasonic


Distance Sensor, which operates based on the principles of SONAR and
RADAR systems.
• The HC-SR04 sensor comprises four pins: Vcc, Gnd, Trig, and Echo. Trig is
triggered with a pulse of 10µsec or more, initiating the generation of 8 pulses at
40 kHz.
• Following the trigger, the Echo pin is raised high by the sensor's control circuit
and remains high until it receives the echo signal of the transmitted pulses back.
• The width of the Echo pulse corresponds to the time taken for the ultrasonic
sound to travel to the object and back.
• Using the measured time and the speed of sound in air, the code calculates the
distance of the object based on a simple formula for distance using speed and
time. Additionally, the code utilizes a stepper motor to respond to the calculated
distance, enhancing its functionality.

DETAILED DIAGRAM ALONG WITH PIN DETAILS

6
WORKING EXPLANATION

7
Ultrasonic Sensor Initialization:
• The code initializes the LPC1768 micro controller and sets up the
necessaryconfigurations for peripheral pins, including the trigger (TRIG)
and echo (ECHO) pins for the HC-SR04 ultrasonic sensor.

Timer Initialization:
• A timer (Timer 0) is configured to measure the time duration between sending an
ultrasonic pulse and receiving its echo. This involves setting the prescaler, match
register, and control registers for accurate timing.

GPIO Configuration:
● GPIO pins are configured for various components, including TRIG, ECHO, LED,
and stepper motor controls. The direction of these pins is set to either input or
output as needed.

Ultrasonic Distance Measurement:


• The main loop triggers the ultrasonic sensor by sending a 10µs pulse on the
TRIG pin. The code then measures the time the ECHO pin stays high,
representing the time taken for the ultrasonic pulse to travel to an object and
back.

Distance Calculation:
• Using the measured time and the speed of sound in air, the code calculates the
distance of the object from the sensor. The formula used is based on the speed
(0.0343 cm/µs) and the time measured.

Timeout Mechanism:
• The program includes a delay function (delay) that introduces a delay of 88,000
iterations, creating a timeout mechanism between successive distance
measurements.

CODE

#include <stdio.h>

8
#include <LPC17xx.h>
#include <math.h>
#include <string.h>
#define PRESCALE 29999999
#define LED_Pinsel 0xff // P0.4-0.11
#define RS_CTRL 0x08000000 // P0.27 To inform whether it is command or data
#define EN_CTRL 0x10000000 // P0.28 Enable Pin first goes high then low
#define DT_CTRL 0x07800000 // P0.23 to P0.26 data lines
#define TRIG (1 << 15) // P0.15
#define ECHO (1 << 16) // P0.16
char ans[20] = "";
int temp, temp1, temp2 = 0;
int flag = 0, flag1;
int i, j, k, l, r, echoTime = 5000;
float distance = 0;
void clear_ports(void);
void lcd_write(void);
void port_write(void);
void lcd_display(unsigned char *buf1);
void delay(unsigned int r1);
void clearDisplay(void);
void startTimer0(void);
float stopTimer0();
void initTimer0(void);
void delayUS(unsigned int microseconds);
void delayMS(unsigned int milliseconds);
void delayUS(unsigned int microseconds) // Using Timer0
{
LPC_SC->PCLKSEL0 &= ~(0x3 << 2); // Set PCLK_TIMER0 to divide by 1
LPC_TIM0->TCR = 0x02; // Reset timer
LPC_TIM0->PR = 0; // Set prescaler to 0
LPC_TIM0->MR0 = microseconds - 1; // Set match register for 10us
LPC_TIM0->MCR = 0x01; // Interrupt on match
LPC_TIM0->TCR = 0x01; // Enable timer
while ((LPC_TIM0->IR & 0x01) == 0); // Wait for interrupt flag
LPC_TIM0->TCR = 0x00; // Disable timer
LPC_TIM0->IR = 0x01;
}
void delayMS(unsigned int milliseconds) // Using Timer0
{
delayUS(milliseconds * 1000);
}

9
void initTimer0(void)
{
// Timer for distance
LPC_TIM0->CTCR = 0x0;
LPC_TIM0->PR = 11999999;
LPC_TIM0->TCR = 0x02; // Reset Timer
}
void startTimer0()
{
LPC_TIM0->TCR = 0x02; // Reset Timer
LPC_TIM0->TCR = 0x01; // Enable timer
}
float stopTimer0()
{
LPC_TIM0->TCR = 0x0;
return LPC_TIM0->TC;
}
void delay(unsigned int r1)
{
for (r = 0; r < r1; r++);
}
void clear_ports(void)
{
/* Clearing the lines at power on */
LPC_GPIO0->FIOCLR = DT_CTRL; // Clearing data lines
LPC_GPIO0->FIOCLR = RS_CTRL; // Clearing RS line
LPC_GPIO0->FIOCLR = EN_CTRL; // Clearing Enable line
delay(3200);
}
void port_write()
{
int j;
LPC_GPIO0->FIOPIN = temp2 << 23;
if (flag1 == 0)
{
LPC_GPIO0->FIOCLR = 1 << 27;
}
else
{
LPC_GPIO0->FIOSET = 1 << 27;
}
LPC_GPIO0->FIOSET = 1 << 28;

10
for (j = 0; j < 50; j++);
LPC_GPIO0->FIOCLR = 1 << 28;
for (j = 0; j < 10000; j++);
}
void lcd_write()
{
temp2 = (temp1 >> 4) & 0xF;
port_write();
temp2 = temp1 & 0xF;
port_write();
}
int main()
{
int ledflag = 0;
int command[] = {3, 3, 3, 2, 2, 0x01, 0x06, 0x0C, 0x80};
char message1[] = "Obstacle Present!";
char message2[] = "No Obstacle.";
float rounded_down;
SystemInit();
SystemCoreClockUpdate();
initTimer0();
LPC_PINCON->PINSEL0 &= 0xfffff00f; // Interface LEDs P0.4-P0.11
LPC_PINCON->PINSEL0 &= 0x3fffffff; // Interface TRIG P0.15
LPC_PINCON->PINSEL1 &= 0xfffffff0; // Interface ECHO P0.16
LPC_GPIO0->FIODIR |= TRIG | 1 << 17; // Direction for TRIGGER pin
LPC_GPIO1->FIODIR |= 0 << 16; // Direction for ECHO PIN
LPC_GPIO0->FIODIR |= LED_Pinsel << 4; // Direction for LED
LPC_PINCON->PINSEL1 |= 0;
LPC_GPIO0->FIODIR |= 0XF << 23 | 1 << 27 | 1 << 28;
flag1 = 0;
for (i = 0; i < 9; i++)
{
temp1 = command[i];
lcd_write();
for (j = 0; j < 30000; j++);
}
flag1 = 1;
i = 0;
flag = 1;
LPC_GPIO0->FIOCLR |= TRIG;
while (1)
{

11
LPC_GPIO0->FIOSET = 0x00000800;
// Output 10us HIGH on TRIG pin
LPC_GPIO0->FIOMASK = 0xFFFF7FFF;
LPC_GPIO0->FIOPIN |= TRIG;
delayUS(10);
LPC_GPIO0->FIOCLR |= TRIG;
LPC_GPIO0->FIOMASK = 0x0;
while (!(LPC_GPIO0->FIOPIN & ECHO)) { // Wait for a HIGH on ECHO pin
}
startTimer0();
//LPC_GPIO0->FIOSET = LED_Pinsel << 4;
//echoTime--;
while (LPC_GPIO0->FIOPIN & ECHO) {
// Wait for a LOW on ECHO pin
}
echoTime = stopTimer0(); // Stop Counting
//LPC_GPIO0->FIOCLR = LED_Pinsel << 4;
distance = (0.0343 * echoTime) / 40;
sprintf(ans, "Dist: %.2f", distance);
flag1 = 1;
i = 0;
flag1 = 0;
temp1 = 0x01;
lcd_write();
flag1 = 1;
while (ans[i] != '\0')
{
temp1 = ans[i];
lcd_write();
for (j = 0; j < 30000; j++);
i++;
}
if (distance < 20) {
LPC_GPIO0->FIOSET = LED_Pinsel << 4;
LPC_GPIO0->FIOSET = 1 << 17;
} else {
LPC_GPIO0->FIOCLR = LED_Pinsel << 4;
LPC_GPIO0->FIOCLR = 1 << 17;
}
delay(88000);
}
}

12
Brief Description about different parts of code and specifications

Interfacing with an ultrasonic sensor for distance measurement. The key components include the
initialization of GPIO pins, the use of Timer0 for precise timing, and the interaction with an LCD
display for output.

Ultrasonic Sensor Interfacing:

● Ultrasonic Sensor Pins:

#define TRIG (1 << 15) // P0.15


#define ECHO (1 << 16) // P0.16

- `TRIG`: Pin connected to the trigger input of the ultrasonic sensor.


- `ECHO`: Pin connected to the echo output of the ultrasonic sensor.

● Triggering the Ultrasonic Sensor:

LPC_GPIO0->FIOSET = 0x00000800; // Set TRIG high


delayUS(10); // Wait for 10 microseconds
LPC_GPIO0->FIOCLR = TRIG; // Clear TRIG

- Sets the TRIG pin high for 10 microseconds to trigger the ultrasonic sensor.
- Clears the TRIG pin to stop the trigger.

● Measuring Echo Time:

while (!(LPC_GPIO0->FIOPIN & ECHO)) { // Wait for a HIGH on ECHO pin


}
startTimer0(); // Start Timer0 to measure echo time
while (LPC_GPIO0->FIOPIN & ECHO) {
// Wait for a LOW on ECHO pin
}

13
echoTime = stopTimer0(); // Stop Timer0 and get echo time

- Waits for a HIGH on the ECHO pin, starts Timer0 to measure time until the signal returns, and stops
Timer0 when the signal goes LOW.
- The `echoTime` variable holds the time it takes for the ultrasonic signal to travel to the object and
back.

● Calculating Distance:

distance = (0.0343 * echoTime) / 40;

- Calculates the distance based on the speed of sound (0.0343 cm/microsecond) and the time it took
for the ultrasonic signal to travel.

● LCD Display Update:

sprintf(ans, " Dist: %.2f", distance); // ... (updating LCD with distance information)

- Formats the distance information as a string and updates the LCD with this information.

● LED Control Based on Distance:

if (distance < 20) {


LPC_GPIO0->FIOSET = LED_Pinsel << 4; // Set LED pins high
LPC_GPIO0->FIOSET = 1 << 17;
} else {
LPC_GPIO0->FIOCLR = LED_Pinsel << 4; // Clear LED pins
LPC_GPIO0->FIOCLR = 1 << 17;
}

- Controls LEDs based on the calculated distance. If the distance is less than 20 cm, certain LED pins
are set high; otherwise, they are cleared.

SNAPSHOTS OF OUTPUT

14
Fig: When object is placed at 5.22 cm distance.
(When the distance is less than 20 cm the LED lights up)

Fig: When object is placed at 61.81 cm distance.


(When the distance is more than 20 cm the LED switches off)
15
CONCLUSION
In conclusion, this project successfully utilizes an LPC1768 micro controller and an
HC-SR04 ultrasonic sensor to implement an obstacle detection system. The code
efficiently manages GPIO configurations, timer functions, allowing the system to
measure real-time distances.The project showcases the microcontroller's capability in
interfacing with peripherals and dynamically responding to environmental conditions.

REFERENCES
[1] http://www.ocfreaks.com

[2] What is Ultrasonic Sensor: Working Principle & Applications – Robocraze

[3] https://www.electronicwings.com/mbed/ultrasonic-sensor-hc-sr04-interfacing-with-arm-mbed

16
PR
by Kirtenya Maheshwari

Submission date: 23-Nov-2023 03:41PM (UTC+0800)


Submission ID: 2236280667
File name: Es_projrctfinal_B1.docx (5.55M)
Word count: 1540
Character count: 8663
PR
ORIGINALITY REPORT

13 %
SIMILARITY INDEX
13%
INTERNET SOURCES
3%
PUBLICATIONS
%
STUDENT PAPERS

PRIMARY SOURCES

1
github.com
Internet Source 4%
2
www.ocfreaks.com
Internet Source 4%
3
elartu.tntu.edu.ua
Internet Source 2%
4
louisdl.louislibraries.org
Internet Source 2%
5
www.edaboard.com
Internet Source 1%
6
www.seruvenyayinevi.com
Internet Source 1%
7
dokumen.tips
Internet Source 1%
8
dronebotworkshop.com
Internet Source <1 %
Exclude quotes On Exclude matches < 3 words
Exclude bibliography On

You might also like