# LPC2148 – Ultrasonic Sensor Interfacing

This article is a continuation of the series of tutorials on the LPC2148 Microcontroller (ARM7). The aim of this series is to provide easy and practical examples that anyone can understand. In the previous tutorial, we have interfaced the RFID reader with LPC2148 (ARM7). Now, we are going to see Ultrasonic Sensor interfacing with LPC2148.

Before we will start I would suggest you to read these topics. Then you can understand this strongly. If you already know, please go ahead.

## Components Required

• LPC2148 Development Board
• Ultrasonic Sensor [HC-SR04]
• LCD Module (To print the Distance)

## Operation

1. Connect the Trigger pin of the HC-SR04 module to any I/O pins of the LPC2148 controller and assign that Pin as an output using IODIR.
2. Connect the Echo pin of the module to any I/O pins of the LPC2148 controller and assign that Pin as an input using IODIR.
3. Send a pulse of minimal timer period 10us, this will make the Ultrasonic module to send a burst of data.
4. Now, wait until that Echo pin goes high. Then Start the timer.
5. The reflected waves will be sensed by the module and it exhibits the output in Echo pin logic low or 0.
6. When the pulse from the echo pin alters its state to logic 0 or low, then stop the timer.
7. The length of the pulse from the echo pin is proportional to the distance at which the object is located.
8. The value in the Timer gives the distance of course with some simple calculations.

CALCULATION

According to the data sheet, the distance can be given by the formula :

Distance in cm = Timer /59

Distance in inch = Timer /148

## Connection

LCD:

• RS – P1.16
• RW – P1.17
• EN – P1.18
• Data Lines – P1.24 – P1.31

Ultrasonic Sensor:

• Trigger – P0.8
• Echo – P0.9

## Code

### Sending Trigger Pulse

```void send_pulse()
{
T0TC=T0PC=0;
IO0SET=trig;                            //trig=1
timer1delay(10);                        //10us delay
IO0CLR=trig;                            //trig=0
}```

Here I’m making the timer value to zero. Then we are giving high to trigger pin (1). We have to wait for 10u Seconds. So I’m using generating 10us delay using timer 1. Then make the trigger pin to low (0).

### Distance Calculating

```unsigned int get_range()
{
unsigned int get=0;
send_pulse();
while(!echo);
T0TCR=0x01;
while(echo);
T0TCR=0;
get=T0TC;

if(get<38000)
get=get/59;
else
get=0;

return get;
}```

This is the full function to calculate the distance.

1. First, I’m sending a trigger pulse.
2. Then waiting until that echo pin goes high (1). Then we have to start the Timer 0.
3. Again I’m waiting until that echo goes low (0). If it goes to zero, we have to stop the Timer 0.
4. Then I’m taking value from the timer register.
5. Then using the above formula I can calculate the Distance.

## Full Code

Here I’m using Two timers. Timer 0 will be used to find the distance. And timer 1 will be used for Generating 10us Delay. Then I’m Printing the distance in LCD Module. Here PCLK will be 60MHz. You can download the full project here.

Main.c

```#include<lpc214x.h>
#include"TIMER.H"
#include"ULTRASONIC.H"
#include"LCD.H"

#define delay for(i=0;i<65000;i++);

unsigned int range=0,i;

int main()
{
VPBDIV=0x01;                 // PCLK = 60MHz
IO1DIR=0xffffffff;
ultrasonic_init();
lcd_init();
show("Distance : ");

while(1) {
cmd(0x8b);
range=get_range();
dat((range/100)+48);
dat(((range/10)%10)+48);
dat((range%10)+48);
show("cm");
delay;
delay;
}
}
```

ULTRASONIC.H

```#define trig (1<<8)             //P0.8
#define echo (IO0PIN&(1<<9))         //P0.9 as EINT3

void ultrasonic_init();
void send_pulse();
unsigned int get_range();

void ultrasonic_init()
{
IO0DIR|=(1<<8);
T0CTCR=0;
T0PR=59;
}

void send_pulse()
{
T0TC=T0PC=0;
IO0SET=trig;                            //trig=1
timer1delay(10);                        //10us delay
IO0CLR=trig;                            //trig=0
}

unsigned int get_range()
{
unsigned int get=0;
send_pulse();
while(!echo);
T0TCR=0x01;
while(echo);
T0TCR=0;
get=T0TC;

if(get<38000)
get=get/59;
else
get=0;

return get;
}

```

LCD.H

```#define bit(x) (1<<x)

void lcd_init();
void cmd(unsigned char a);
void dat(unsigned char b);
void show(unsigned char *s);
void lcd_delay();

void lcd_init()
{
cmd(0x38);
cmd(0x0e);
cmd(0x06);
cmd(0x0c);
cmd(0x80);
}

void cmd(unsigned char a)
{
IO1PIN&=0x00;
IO1PIN|=(a<<24);
IO1CLR|=bit(16);                   //rs=0
IO1CLR|=bit(17);                   //rw=0
IO1SET|=bit(18);                    //en=1
lcd_delay();
IO1CLR|=bit(18);                    //en=0
}

void dat(unsigned char b)
{
IO1PIN&=0x00;
IO1PIN|=(b<<24);
IO1SET|=bit(16);                   //rs=1
IO1CLR|=bit(17);                   //rw=0
IO1SET|=bit(18);               //en=1
lcd_delay();
IO1CLR|=bit(18);               //en=0
}

void show(unsigned char *s)
{
while(*s) {
dat(*s++);
}
}

void lcd_delay()
{
unsigned int i;
for(i=0;i<=1000;i++);
}
```

TIMER.H

```void timer0delay(unsigned int a);
void timer1delay(unsigned int b);

void timer0delay(unsigned int a)    //1ms
{
T0CTCR=0X0000;
T0PR=59999;
T0MR0=a;
T0MCR=0x00000004;
T0TCR=0X02;
T0TCR=0X01;
while(T0TC!=T0MR0);
T0TC=0;
}

void timer1delay(unsigned int b)   //1us
{
T1CTCR=0X0000;
T1PR=59;
T1MR0=b;
T1MCR=0x00000004;
T1TCR=0X02;
T1TCR=0X01;
while(T1TC!=T1MR0);
T1TC=0;
}
```

In our next tutorial, we will see how to interface the GPS with LPC2148 (ARM7). If you want to use FreeRTOS on LPC2148, then please refer FreeRTOS series.

You can also read the below tutorials.

Subscribe
Notify of

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Inline Feedbacks