ES Project Report Group 2
ES Project Report Group 2
For Partial Fulfilment of the Requirement for the Award of the Degree of
Bachelor of Technology
in
Information Technology
by
1
INTRODUCTION
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:
1. Components Required 5
and Methodology
3. Code 9
4. Snapshots of output 16
COMPONENTS REQUIRED
3
Hardware Requirements:
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..
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:
5
METHODOLOGY
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.
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.
- Sets the TRIG pin high for 10 microseconds to trigger the ultrasonic sensor.
- Clears the TRIG pin to stop the trigger.
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:
- Calculates the distance based on the speed of sound (0.0343 cm/microsecond) and the time it took
for the ultrasonic signal to travel.
sprintf(ans, " Dist: %.2f", distance); // ... (updating LCD with distance information)
- Formats the distance information as a string and updates the LCD with this information.
- 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)
REFERENCES
[1] http://www.ocfreaks.com
[3] https://www.electronicwings.com/mbed/ultrasonic-sensor-hc-sr04-interfacing-with-arm-mbed
16
PR
by Kirtenya Maheshwari
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