LPC2148 – Ultrasonic Sensor Interfacing

Hi guys… Today we are going to see Ultrasonic Sensor interfacing with LPC2148.

Suggest to Read

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 LPC2148 controller and assign that Pin as a output using IODIR.
  2. Connect the Echo pin of the module to any I/O pins of LPC2148 controller and assign that Pin as a input using IODIR.
  3. Send a pulse of minimal timer period 10us, this will make the Ultrasonic module to send 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 echo pin alters its state to logic 0 or low, then stop the timer.
  7. The length of pulse from 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.

Ultrasonic Sensor Interfacing with 8051

CALCULATION

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

Distance in cm = Timer /59

Distance in inch = Timer /148

Ultrasonic Sensor Interfacing with LPC2148

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

lpc2148 ultrasonic sensor

Code

Sending Trigger Pulse

Here I’m making 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 trigger pin to low (0).

Distance Calculating

This is the full function to calculate the distance.

  1. First i’m sending 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 timer register.
  5. Then using above formula i can calculate the Distance.

Full Code

Here I’m using Two timers. Timer 0 will be used for 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

ULTRASONIC.H

LCD.H

TIMER.H

 

0 0 vote
Article Rating
Subscribe
Notify of
guest

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

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x
%d bloggers like this: