8051 – Ultrasonic Sensor Interfacing

Hi guys… Today we are going to see Ultrasonic Sensor Interfacing with 8051.

Ultrasonic Sensor Interfacing with 8051

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

  • 8051 Microcontroller
  • Ultrasonic Sensor [HC-SR04]
  • LCD Module (To print the Distance)

Measuring Echo Pulse

The measurement of Echo pulse can be done with the help of external Interrupt and Timer modules of 8051 Microcontroller. In capture mode when external pulse is fed to the interrupt the timer starts counting and will stop only after the state of pulse is changed. Steps to follow to program controller as a capture device.

  1. Initialize Timer0 module as 16 bit timer with Gate bit as 1.
  2. Making Gate bit as 1 will transfer the control of timer to external interrupt pin INT0.
  3. When there is an interrupt pulse timer 0 starts counting and stops as soon as the interrupt disappears.
  4. The value in the Timer 0 (TH0 and TL0) gives the time period or length of the pulse.

Operation

  1. Connect the Trigger pin of the HC-SR04 module to any I/O pins of 8051 controller.
  2. Connect the Echo pin of the module to interrupt pin INT0 of the Microcontroller.
  3. Send a pulse of minimal timer period 10us, this will make the Ultrasonic module to send burst of data.
  4. The reflected waves will be sensed by the module and it exhibits the output in Echo pin logic high or 1 to INT0 pin of Microcontroller.
  5. This creates an interrupt and Timer 0 will start counting.
  6. When the pulse from echo pin alters its state to logic 0 or low the timer 0 stops counting
  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 0 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 0/59

Distance in inch = Timer 0/148

Connection

LCD:

  • RS – P0.0
  • RW – P0.1 
  • EN – P0.2
  • Data Lines – P2 

Ultrasonic Sensor:

  • Trigger – P3.5
  • Echo – P3.2

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 _nop_() function. Each -nop_() function will generate 1us. For that we need to use intrins.h header file. 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). In this stage timer would be start.
  3. Again i’m waiting until that echo goes low (0). If it goes to zero, timer will OFF.
  4. Then I’m taking value from timer register. Here i’m using DPTR Register. Because in 8051 DPTR Register size is 16bits. so i’m using that.
  5. Then using formula i can calculate the Distance.

Full Code

So this code will print the distance in LCD module. You can use this module for any robot projects.

 

 

Download our new Android app. You can learn all Embedded Tutorials from your Android Phone easily.

Click Here to Download App!