Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe

How to interface Sharp’s distance sensor with 8051 microcontroller (AT89C51)- (Part 32/45)

By Himanshu Choudhary August 17, 2010

This project uses Sharp’s distance measuring sensors for exact distance measurement. Advanced measurement techniques make these sensors more reliable. They have higher precision since they are less influenced by the color and reflectivity of the reflected object. They use a focused beam to indicate the presence and location of an object in a particular range depending on the variant of the sensor. Typical ranges for different sharp sensors vary from 4 cm to 150 cm.

The project works on the same principle as that of simple IR sensor based distance measurement. The analog output of the sensor is directly converted to digital by employing an ADC0804 interfaced with AT89C51.  The measured distance is also displayed on an LCD screen. This project is used for detecting humans and objects in several devices, security systems and robotics applications. It can be employed for sanitary purposes in electronic faucets, flush levers, soap dispensers etc.

 


 

 

This project mainly consists of three units: a sensor unit, an ADC component and the LCD module.

The sensor, Sharp 2Y0A02 infrared ranger, gives an output voltage proportionate to the distance of an object from it. It has a detection range of 20cm – 150cm and the output characteristic can be obtained from the datasheet. The analog output of the sensor is connected to the input of the ADC0804.  

The input received at ADC is processed and its equivalent binary is sent to the microcontroller. The Vref/2 pin of the ADC is connected to a preset which can be adjusted to set the step size for relevant measurement.
 
The microcontroller AT89C51 is programmed to read the digital output from ADC and convert it into a decimal representation. This value corresponds to the distance of the object and is sent to be displayed on a 16×2 LCD.
 
Note: This value does not correctly represent the distance. For actual measurement, it has to be calibrated in accordance with the characteristic curve given in the datasheet, through microcontroller programming.

Project Source Code

###

// Program to interface Sharps distance sensor. Set vref =3v for ADC0804
#include<reg51.h>
#define port P3
#define adc_input P1
#define dataport P0
#define sec 100
sbit rs = port^0;
sbit rw = port^1;
sbit e = port^2;

sbit wr= port^3;
sbit rd= port^4;
sbit intr= port^5;

int test_final=0 ,shift=0;

void delay(unsigned int msec )
{
int i ,j ;
for(i=0;i<msec;i++)
  for(j=0; j<1275; j++);
}

void lcd_cmd(unsigned char item) // Function to send command to LCD
{
dataport = item;
rs= 0;
rw=0;
e=1;
delay(1);
e=0;
return;
}

void lcd_data(unsigned char item) // Function to send data to LCD
{
dataport = item;
rs= 1;
rw=0;
e=1;
delay(1);
e=0;
return;
}

void lcd_data_string(unsigned char *str) // Function to send string to LCD
{
int i=0;
while(str[i]!='')
{
  lcd_data(str[i]);
  i++;
  delay(1);
}
return;
}

void convert()
{
int s;
lcd_cmd(0x81);
delay(2);
lcd_data_string("output:");
s=test_final/100;
test_final=test_final%100;
lcd_cmd(0x8a);
if(s!=0)
lcd_data(s+48);
else
lcd_cmd(0x06);
s=test_final/10;
test_final=test_final%10;
lcd_data(s+48);
lcd_data(test_final+48);
lcd_data(' ');
if(shift>16)
{
lcd_cmd(0xc0+(shift-1));
lcd_data_string("       ");
shift=0; 
}
lcd_cmd(0xc0+(shift-1));
lcd_data(' ');
lcd_cmd(0xc0+shift);
lcd_data_string("CALIBRATE IT");
delay(30);
}

void main()
{
adc_input=0xff;
lcd_cmd(0x38);  //2 Line, 5X7 Matrix
lcd_cmd(0x0c);  //Display On, Cursor Blink
delay(2);
lcd_cmd(0x01);  // Clear Screen
delay(2);
lcd_cmd(0x81); 
delay(2);
lcd_data_string(" start");
delay(20);
while(1)
{
  shift++;
  delay(1);
  rd=1;
  wr=0;
  delay(1);
  wr=1;
  while(intr==1);
  rd=0;
  test_final=adc_input;
  delay(1);
  intr=1;
  convert();
}
}

###

 


Circuit Diagrams

Circuit-Diagram-Interface-Sharps-Distance-Sensor-8051-Microcontroller-AT89C51

Project Components

  • ADC0804
  • AT89C51 Microcontroller
  • LCD
  • Preset

Project Video


Filed Under: 8051 Microcontroller
Tagged With: 8051, distance, microcontroller, sensor
 

Next Article

← Previous Article
Next Article →

Questions related to this article?
👉Ask and discuss on EDAboard.com and Electro-Tech-Online.com forums.



Tell Us What You Think!! Cancel reply

You must be logged in to post a comment.

EE TECH TOOLBOX

“ee
Tech Toolbox: 5G Technology
This Tech Toolbox covers the basics of 5G technology plus a story about how engineers designed and built a prototype DSL router mostly from old cellphone parts. Download this first 5G/wired/wireless communications Tech Toolbox to learn more!

EE Learning Center

EE Learning Center
“engineers
EXPAND YOUR KNOWLEDGE AND STAY CONNECTED
Get the latest info on technologies, tools and strategies for EE professionals.

HAVE A QUESTION?

Have a technical question about an article or other engineering questions? Check out our engineering forums EDABoard.com and Electro-Tech-Online.com where you can get those questions asked and answered by your peers!


RSS EDABOARD.com Discussions

  • BF999 Input and output impedance
  • Control for DC motor
  • Industrial Relay Board Design for Motorcycle Use
  • Sendust vs Ferrite for SMPS
  • On/Off Slide Switch Reassembly Help

RSS Electro-Tech-Online.com Discussions

  • More fun with ws2812 this time XC8 and CLC
  • Wierd makita battery
  • I Wanna build a robot
  • using a RTC in SF basic
  • Is AI making embedded software developers more productive?

Featured – LoRa/LoRaWan Series

  • What is the LoRaWAN network and how does it work?
  • Understanding LoRa architecture: nodes, gateways, and servers
  • Revolutionizing RF: LoRa applications and advantages
  • How to build a LoRa gateway using Raspberry Pi
  • How LoRa enables long-range communication
  • How communication works between two LoRa end-node devices

Recent Articles

  • STMicroelectronics unveils SoC based on secure MCU
  • Nexperia’s 48 V ESD diodes support higher data rates with ultra-low capacitance design
  • Taoglas releases Patriot antenna with 18 integrated elements covering 600 to 6000 MHz
  • Amphenol RF introduces SMPM to SMPM assemblies on RG-178 cable
  • Infineon launches 3D magnetic sensors with ±50 mT to ±160 mT measurement ranges

EE ENGINEERING TRAINING DAYS

engineering

Submit a Guest Post

submit a guest post
Engineers Garage
  • Analog IC TIps
  • Connector Tips
  • Battery Power Tips
  • DesignFast
  • EDABoard Forums
  • EE World Online
  • Electro-Tech-Online Forums
  • EV Engineering
  • Microcontroller Tips
  • Power Electronic Tips
  • Sensor Tips
  • Test and Measurement Tips
  • 5G Technology World
  • Subscribe to our newsletter
  • About Us
  • Contact Us
  • Advertise

Copyright © 2025 WTWH Media LLC. All Rights Reserved. The material on this site may not be reproduced, distributed, transmitted, cached or otherwise used, except with the prior written permission of WTWH Media
Privacy Policy

Search Engineers Garage

  • Electronic Projects & Tutorials
    • Electronic Projects
      • Arduino Projects
      • AVR
      • Raspberry pi
      • ESP8266
      • BeagleBone
      • 8051 Microcontroller
      • ARM
      • PIC Microcontroller
      • STM32
    • Tutorials
      • Audio Electronics
      • Battery Management
      • Brainwave
      • Electric Vehicles
      • EMI/EMC/RFI
      • Hardware Filters
      • IoT tutorials
      • Power Tutorials
      • Python
      • Sensors
      • USB
      • VHDL
    • Circuit Design
    • Project Videos
    • Components
  • Articles
    • Tech Articles
    • Insight
    • Invention Stories
    • How to
    • What Is
  • News
    • Electronic Product News
    • Business News
    • Company/Start-up News
    • DIY Reviews
    • Guest Post
  • Forums
    • EDABoard.com
    • Electro-Tech-Online
    • EG Forum Archive
  • DigiKey Store
    • Cables, Wires
    • Connectors, Interconnect
    • Discrete
    • Electromechanical
    • Embedded Computers
    • Enclosures, Hardware, Office
    • Integrated Circuits (ICs)
    • Isolators
    • LED/Optoelectronics
    • Passive
    • Power, Circuit Protection
    • Programmers
    • RF, Wireless
    • Semiconductors
    • Sensors, Transducers
    • Test Products
    • Tools
  • Learn
    • eBooks/Tech Tips
    • Design Guides
    • Learning Center
    • Tech Toolboxes
    • Webinars & Digital Events
  • Resources
    • Digital Issues
    • EE Training Days
    • LEAP Awards
    • Podcasts
    • Webinars / Digital Events
    • White Papers
    • Engineering Diversity & Inclusion
    • DesignFast
  • Guest Post Guidelines
  • Advertise
  • Subscribe