8051 – Ultrasonic Sensor Interfacing

Hi guys… In our previous tutorial, we have seen ADC0808 interfacing with 8051. Today we are going to see Ultrasonic Sensor Interfacing with 8051.

Ultrasonic Sensor Interfacing with 8051

Suggestion 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 the external pulse is fed to the interrupt the timer starts counting and will stop only after the state of the pulse is changed. Steps to follow to program controller as a capture device.

  1. Initialize Timer0 module as a 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 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 the 8051 controllers.
  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 a 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 the echo pin alters its state to logic 0 or low the timer 0 stops counting
  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 0 gives the distance of course with some simple calculations.

CALCULATION

According to the datasheet 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

void send_pulse(void) 
{
	TH0=0x00;TL0=0x00; 
	 trig=1;
	 _nop_();_nop_();_nop_();_nop_();_nop_();
	 _nop_();_nop_();_nop_();_nop_();_nop_();
	 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 the _nop_() function. Each -nop_() function will generate 1us. For that, we need to use intrins.h header file. Then make the trigger pin to low (0).

Distance Calculating

unsigned char ultrasonic()
{
	unsigned char get;
	send_pulse();
	while(!echo);
	while(echo);
	DPH=TH0;
	DPL=TL0;
	TH0=TL0=0xff;
	if(DPTR<38000)
		get=DPTR/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). In this stage, the timer would start.
  3. Again I’m waiting until that echo goes low (0). If it goes to zero, the timer will OFF.
  4. Then I’m taking value from the timer register. Here I’m using DPTR Register. Because in 8051 DPTR Register size is 16bits. so I’m using that.
  5. Then using the formula, I can calculate the Distance.

Full Code

#include<reg51.h>
#include<intrins.h>

#define lcd_data P2

sfr16 DPTR=0x82;

sbit trig=P3^5;
sbit echo=P3^2;

unsigned int range=0;

sbit rs=P0^0;
sbit rw=P0^1;
sbit en=P0^2;

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)
{
	lcd_data=a;
	rs=0;
	rw=0;
	en=1;
	lcd_delay();
	en=0;
}

void dat(unsigned char b)
{
	lcd_data=b;
	rs=1;
	rw=0;
	en=1;
	lcd_delay();
	en=0;
}

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

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


void send_pulse(void) 
{
	TH0=0x00;TL0=0x00; 
	 trig=1;
	 _nop_();_nop_();_nop_();_nop_();_nop_();
	 _nop_();_nop_();_nop_();_nop_();_nop_();
	 trig=0;
}

unsigned char ultrasonic()
{
	unsigned char get;
	send_pulse();
	while(!echo);
	while(echo);
	DPH=TH0;
	DPL=TL0;
	TH0=TL0=0xff;
	if(DPTR<38000)
		get=DPTR/59;
	else
		get=0;
	return get;
}


void main()
{
	TMOD=0x09;
	TH0=TL0=0;
	TR0=1;
	lcd_init();
	show("DIS");
	P3|=(1<<2);
	while(1) {
		cmd(0x84);
		range=ultrasonic();
		dat((range/100)+48);
		dat(((range/10)%10)+48);
		dat((range%10)+48);
	}
}

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

In our next tutorial, we will interface the GSM module with 8051.

You can also read the below tutorials.

Linux Device Driver TutorialsC Programming Tutorials
FreeRTOS TutorialsNuttX RTOS Tutorials
RTX RTOS TutorialsInterrupts Basics
I2C Protocol – Part 1 (Basics)I2C Protocol – Part 2 (Advanced Topics)
STM32 TutorialsLPC2148 (ARM7) Tutorials
PIC16F877A Tutorials8051 Tutorials
Unit Testing in C TutorialsESP32-IDF Tutorials
Raspberry Pi TutorialsEmbedded Interview Topics
Reset Sequence in ARM Cortex-M4BLE Basics
VIC and NVIC in ARMSPI – Serial Peripheral Interface Protocol
Bootloader TutorialsRaspberry PI Pico Tutorials

 

3 2 votes
Article Rating
Subscribe
Notify of
guest

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

8 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
8
0
Would love your thoughts, please comment.x